So I've been trying to cycle through colors within a certain degree range using HSV, but I can't seem to transition smoothly between the colors. Ideally I would like to be able to move within a specific degree range (80-140) and cycle the saturation and value between .5 - 1. I honestly have no idea where to go from here and I'm eager to learn how.
int[] colors = HsvToRgb(h,s ,v );
The methods I used:
public static int[] HsvToRgb(double h, double S, double V) {
double H = h;
while (H < 0) {
H += 360;
}
while (H >= 360) {
H -= 360;
}
double R = 0, G = 0, B = 0;
if (V <= 0) {
R = G = B = 0;
} else if (S <= 0) {
R = G = B = V;
} else {
double hf = H / 60.0;
int i = (int) Math.floor(hf);
double f = hf - i;
double pv = V * (1 - S);
double qv = V * (1 - S * f);
double tv = V * (1 - S * (1 - f));
switch (i) {
case 0:
R = V;
G = tv;
B = pv;
break;
case 1:
R = qv;
G = V;
B = pv;
break;
case 2:
R = pv;
G = V;
B = tv;
break;
case 3:
R = pv;
G = qv;
B = V;
break;
case 4:
R = tv;
G = pv;
B = V;
break;
case 5:
R = V;
G = pv;
B = qv;
break;
case 6:
R = V;
G = tv;
B = pv;
break;
case -1:
R = V;
G = pv;
B = qv;
break;
}
}
int r = Clamp((int) (R * 255.0));
int g = Clamp((int) (G * 255.0));
int b = Clamp((int) (B * 255.0));
return new int[]{r, g, b};
}
public static int Clamp(int i) {
if (i < 0) {
return 0;
}
if (i > 255) {
return 255;
}
return i;
}
Check out HSL Color. Its API allows you to easily change the color using methods like:
adjustHue
– specify the degree of a coloradjustLuminance
– specify an absolute percentage of luminanceadjustSaturation
– specify an absolute percentage of saturationadjustShade
– adjusting the shade will give you a darker color. You control how much darker the color should be by specifying the adjustmnt percent.adjustTone
– adjusting the tone will give you a lighter color. You control how much lighter the color should be by specifying the adjustment percent.