Convert Color Spaces (HSL->RGB)
December 3rd, 2009
Math
C/C++
pixeltype HSLtoRGB(pixeltype palette_color){
//0.0f <= h < 360.0f
//0.0f <= s <= 1.0f
//0.0f <= l <= 1.0f
double h = (double)(HUE_OF(palette_color));
double s = (double)(SATURATION_OF(palette_color));
double l = (double)(LIGHTNESS_OF(palette_color));
double q, p, h_k;
double rgb[3];
if(l < 0.5f){
q = l * (1 + s);
}else{
q = l + s - (l * s);
}
p = 2 * l - q;
h_k = h / 360.0f;
rgb[0] = h_k + (1 / 3);
rgb[1] = h_k;
rgb[2] = h_k - (1 / 3);
for(size_t i = 0; i < 3; i++){
if(rgb[i] < (1 / 6)){
rgb[i] = p + ((q - p) * 6.0f * rgb[i]);
}else if(rgb[i] >= (1 / 6) && rgb[i] < (1 / 2)){
rgb[i] = q;
}else if(rgb[i] >= (1 / 2) && rgb[i] < (2 / 3)){
rgb[i] = p + ((q - p) * 6.0f * ((2 / 3) - rgb[i]));
}else{
rgb[i] = p;
}
}
//0.0f <= rgb[0] <= 1.0f
//0.0f <= rgb[1] <= 1.0f
//0.0f <= rgb[2] <= 1.0f
return RGB_OF(rgb[0], rgb[1], rgb[2]);
}
) Your Reply...