public static void hsl2rgb(int hue, int saturation, int lightness, int[] rgb)
{
int m1, m2;
if(saturation == 0)
{
rgb[0] = rgb[1] = rgb[2] = lightness;
}
else
{
if(lightness < 128)
m2 = lightness + (lightness * saturation >> 8);
else
m2 = lightness + saturation - (lightness * saturation >> 8);
m1 = lightness * 2 - m2;
rgb[0] = hue2color(hue + 120, m1, m2);
rgb[1] = hue2color(hue, m1, m2);
rgb[2] = hue2color(hue - 120, m1, m2);
}
}
private static int hue2color(int hue, int m1, int m2)
{
int h, v = 0;
h = (hue + 360) % 360;
if(h < 60) /*60 = 360 / 6;*/
v = m1 + (m2 - m1) * h / 60;
else if(h < 180) /*180 = 360 / 2;*/
v = m2;
else if(h < 240) /*240 = 360 * 2 / 3;*/
v = m1 + (m2 - m1) * (240 - h) / 60;
else
v = m1;
return v;
}
{
int m1, m2;
if(saturation == 0)
{
rgb[0] = rgb[1] = rgb[2] = lightness;
}
else
{
if(lightness < 128)
m2 = lightness + (lightness * saturation >> 8);
else
m2 = lightness + saturation - (lightness * saturation >> 8);
m1 = lightness * 2 - m2;
rgb[0] = hue2color(hue + 120, m1, m2);
rgb[1] = hue2color(hue, m1, m2);
rgb[2] = hue2color(hue - 120, m1, m2);
}
}
private static int hue2color(int hue, int m1, int m2)
{
int h, v = 0;
h = (hue + 360) % 360;
if(h < 60) /*60 = 360 / 6;*/
v = m1 + (m2 - m1) * h / 60;
else if(h < 180) /*180 = 360 / 2;*/
v = m2;
else if(h < 240) /*240 = 360 * 2 / 3;*/
v = m1 + (m2 - m1) * (240 - h) / 60;
else
v = m1;
return v;
}