RGB and HSL Color Conversion
Cheungmine
//// The following code completes color conversion: RGB <---> HSL. Pay attention to the value range of their components. // Cheungmine collection and sorting // # include "stdafx. H" # define min3v (V1, V2, V3) (V1)> (V2 )? (V2)> (V3 )? (V3) :( V2) :( (V1)> (V3 )? (V3) :( V2) # define max3v (V1, V2, V3) (V1) <(V2 )? (V2) <(V3 )? (V3) :( V2) :( (V1) <(V3 )? (V3) :( V1) typedef struct {byte red; // [0,255] Byte green; // [0,255] Byte blue; // [0,255]} color_rgb; typedef struct {float hue; // [0,360] float saturation; // [0,100] float luminance; // [0,100]} color_hsl; // converts RGB to hslstatic void rgbtohsl (/* [in] */const color_rgb * RGB,/* [out] */color_hsl * HSL) {float h = 0, S = 0, L = 0; // normalizes red-green-blue valuesfloat r = RGB-> Red/venture f; float G = RGB-> green/venture f; Float B = RGB-> blue/255.f; float maxval = max3v (R, G, B); float minval = min3v (R, G, B ); // hueif (maxval = minval) {H = 0; // undefined} else if (maxval = R & G> = B) {H = 60366f * (G-B)/(maxval-minval);} else if (maxval = R & G <B) {H = 601_f * (G-B)/(maxval-minval) + 360.0f;} else if (maxval = g) {H = 601_f * (B-r) /(maxval-minval) + 1200000f;} else if (maxval = B) {H = 600000f * (r-g)/(maxval-minval) + 240.0f ;} // luminancel = (Maxval + minval)/2.0f; // saturationif (L = 0 | maxval = minval) {S = 0 ;} else if (0 <L & L <= 0.5f) {S = (maxval-minval)/(maxval + minval);} else if (L> 0.5f) {S = (maxval-minval)/(2-(maxval + minval); // (maxval-minval> 0 )?} HSL-> hue = (h> 360 )? 360: (h <0 )? 0: H); HSL-> saturation = (S> 1 )? 1: (S <0 )? 0: S) * 100; HSL-> luminance = (L> 1 )? 1: (L <0 )? 0: l) * 100;} // converts HSL to rgbstatic void hsltorgb (const color_hsl * HSL, color_rgb * RGB) {float H = HSL-> hue; // H must be [0,360] float S = HSL-> saturation/100.f; // s must be [0, 1] float L = HSL-> luminance/100.f; // l must be [0, 1] float R, G, B; If (HSL-> saturation = 0) {// achromatic color (gray scale) r = G = B = L * systf;} else {float q = (L <0.5f )? (L * (1.0f + S) :( L + S-(L * s); float P = (2.0f * l)-Q; float HK = H/360.0f; float T [3]; t [0] = HK + 0.33333f; // tr0.3333333f = 1.0/3.0 T [1] = HK; // TBT [2] = HK-0.3333333f; // tgfor (INT I = 0; I <3; I ++) {If (T [I] <0) T [I] + = 1.0f; if (T [I]> 1) T [I]-= 1.0f; If (T [I] * 6) <1) {T [I] = P + (Q-p) * 6.0f * t [I]);} else if (T [I] * 2.0f) <1) // (1.0/6.0) <= T [I] & T [I] <0.5 {T [I] = Q ;} else if (T [I] * 3.0f) <2) // 0.5 <= T [I] & T [I] <(2.0/3.0) {T [I] = P + (Q-p) * (2.0f/3.0f)-T [I]) * 6.0f ;} else t [I] = P;} r = T [0] * 255.0f; G = T [1] * 255.0f; B = T [2] * 255.0f ;} RGB-> Red = (byte) (r> 255 )? 255: (r <0 )? 0: R); RGB-> Green = (byte) (G> 255 )? 255: (g <0 )? 0: G); RGB-> Blue = (byte) (B> 255 )? 255: (B <0 )? 0: B);} int _ tmain (INT argc, _ tchar * argv []) {color_rgb RGB = {254,216,166}; color_hsl HSL; rgbtohsl (& RGB, & HSL); printf ("H = %. 0f; S = %. 0f; L = %. 0f/N ", HSL. hue, HSL. saturation, HSL. luminance); hsltorgb (& HSL, & RGB); rgbtohsl (& RGB, & HSL); printf ("H = %. 0f; S = %. 0f; L = %. 0f/N ", HSL. hue, HSL. saturation, HSL. luminance); getchar (); Return 0 ;}// in windows, the range of HSL is [0,240]. Refer to the "paint brush" program to see the connection between RGB (Red | green | blue) // and HSL (color | saturation | brightness. // The following code changes the color_hsl component value to the Windows HSL component. The value ranges from [1, 0,240] and must be: color_hsl HSL = {300, 50, 82 }; // The conversion from color_hsl to Windows HSL is as follows: win_h = 240 * HSL. hue/360.f; win_s = 240 * HSL. saturation/100.f; win_l = 240 * HSL. luminance/100.f;
Reference:
http://www.codeproject.com/KB/recipes/colorspace1.aspx#rgb2