// This function converts the "color" object to the equivalent RGB values of // the hue, saturation, and luminance passed as h, s, and l respectively unsigned int HSLtoRGB(const unsigned int& h, const unsigned int& s, const unsigned int& l) { unsigned int r = 0; unsigned int g = 0; unsigned int b = 0; float L = ((float)l)/100; float S = ((float)s)/100; float H = ((float)h)/360; if(s == 0) { r = l; g = l; b = l; } else { float temp1 = 0; if(L < .50) { temp1 = L*(1 + S); } else { temp1 = L + S - (L*S); } float temp2 = 2*L - temp1; float temp3 = 0; for(int i = 0 ; i < 3 ; i++) { switch(i) { case 0: // red { temp3 = H + .33333f; if(temp3 > 1) temp3 -= 1; HSLtoRGB_Subfunction(r,temp1,temp2,temp3); break; } case 1: // green { temp3 = H; HSLtoRGB_Subfunction(g,temp1,temp2,temp3); break; } case 2: // blue { temp3 = H - .33333f; if(temp3 < 0) temp3 += 1; HSLtoRGB_Subfunction(b,temp1,temp2,temp3); break; } default: { } } } } r = (unsigned int)((((float)r)/100)*255); g = (unsigned int)((((float)g)/100)*255); b = (unsigned int)((((float)b)/100)*255); return RGB(r,g,b); }
// This function converts the "color" object to the equivalent RGB values of // the hue, saturation, and luminance passed as h, s, and l respectively rgb_color hsl_to_rgb(hsl_color hsl) { uint r = 0; uint g = 0; uint b = 0; hsl.l /= 100; hsl.s /= 100; hsl.h /= 360; if(hsl.s == 0) { r = hsl.l; g = hsl.l; b = hsl.l; } else { double temp1 = 0; if(hsl.l < .50) { temp1 = hsl.l*(1 + hsl.s); } else { temp1 = hsl.l + hsl.s - (hsl.l*hsl.s); } double temp2 = 2*hsl.l - temp1; double temp3 = 0; for(int i = 0 ; i < 3 ; i++) { switch(i) { case 0: // red { temp3 = hsl.h + .33333; if(temp3 > 1) temp3 -= 1; HSLtoRGB_Subfunction(r,temp1,temp2,temp3); break; } case 1: // green { temp3 = hsl.h; HSLtoRGB_Subfunction(g,temp1,temp2,temp3); break; } case 2: // blue { temp3 = hsl.h - .33333; if(temp3 < 0) temp3 += 1; HSLtoRGB_Subfunction(b,temp1,temp2,temp3); break; } default: { break; } } } } rgb_color rgb; rgb.r = ((((double)r)/100)*255); rgb.g = ((((double)g)/100)*255); rgb.b = ((((double)b)/100)*255); return rgb; }
/** * credit: <http://www.duatiu.com/HSL2RGB.cpp> * This function converts the "color" object to the equivalent RGB values of * the hue, saturation, and luminance passed as h, s, and l respectively */ static void HSLtoRGB(const unsigned int h, const unsigned int s, const unsigned int l, unsigned int *rr, unsigned int *rg, unsigned int *rb) { unsigned int r = 0; unsigned int g = 0; unsigned int b = 0; float L = ((float)l)/100; float S = ((float)s)/100; float H = ((float)h)/360; float temp1, temp2, temp3; int i; if(s == 0) { r = l; g = l; b = l; } else { temp1 = 0; if(L < .50) { temp1 = L*(1 + S); } else { temp1 = L + S - (L*S); } temp2 = 2*L - temp1; temp3 = 0; for(i = 0 ; i < 3 ; i++) { switch(i) { case 0: // red { temp3 = H + .33333f; if(temp3 > 1) temp3 -= 1; r = HSLtoRGB_Subfunction(temp1,temp2,temp3); break; } case 1: // green { temp3 = H; g = HSLtoRGB_Subfunction(temp1,temp2,temp3); break; } case 2: // blue { temp3 = H - .33333f; if(temp3 < 0) temp3 += 1; b = HSLtoRGB_Subfunction(temp1,temp2,temp3); break; } default: { } } } } *rr = (unsigned int)((((float)r)/100)*255); *rg = (unsigned int)((((float)g)/100)*255); *rb = (unsigned int)((((float)b)/100)*255); }