HSL color mode with RGB color mode conversion code for RGB color mode
HSL Color Mode
HSL color mode and RGB color mode conversion algorithm
HSL and RGB Conversion
publicclass ColorRGB { publicint r; publicint g; publicint b;}
publicclass ColorHSL { publicint h; publicfloat s; publicfloat l;}
A) The algorithm description of the RGB→HSL.
Step 1: Turn the RGB values into "0,1" values.
Step 2: Find the maximum value in R,g and B.
Step 3: Set l= (Maxcolor + mincolor)/2
Step 4: If the maximum and minimum color values are the same, which means gray, then S is defined as 0, and H is undefined and is usually written in the program as 0.
Step 5: Otherwise, test L:
If l<0.5, s= (maxcolor-mincolor)/(Maxcolor + mincolor)
If l>=0.5, s= (maxcolor-mincolor)/(2.0-maxcolor-mincolor)
Step 6:if R=maxcolor, h= (g-b)/(Maxcolor-mincolor)
If G=maxcolor, h=2.0+ (b-r)/(Maxcolor-mincolor)
If B=maxcolor, h=4.0+ (r-g)/(Maxcolor-mincolor)
Step 7: From the calculation of the 6th step, H is divided into the 0~6 area. The RGB color space is a cube and the HSL color space is two hexagon cones, where l is the main diagonal of the RGB cube. Therefore, the vertices of the RGB cube: Red, yellow, green, cyan, blue, and magenta become the vertices of the HSL hexagonal, and the numerical 0~6 tells us which part H is. h=h*60.0, if H is negative, add 360.
Private StaticCOLORHSL COLORRGBTOHSL (Colorrgb RGB) {COLORHSL HSL =NewCOLORHSL ();floatR, G, B, H, S, l; R = RGB.R/255.0f; g = RGB.G/255.0f; b = rgb.b/255.0f;floatMaxcolor = Math.max (R, Math.max (g, b));floatMincolor = Math.min (R, Math.min (g, b));if(Maxcolor = = Mincolor) {h =0.0f; s =0.0f; L = r; }Else{L = (Mincolor + maxcolor)/2;if(L <0.5) s = (Maxcolor-mincolor)/(Maxcolor + mincolor);Elses = (float) ((Maxcolor-mincolor)/(2.0-Maxcolor-mincolor));if(r = = Maxcolor) H = (g-b)/(Maxcolor-mincolor);Else if(g = = Maxcolor) H = (float) (2.0+ (B-R)/(Maxcolor-mincolor));ElseH = (float) (4.0+ (R-G)/(Maxcolor-mincolor)); H/=6;if(H <0) h++; } hsl.h = (int) Math.Round (H *360.0); Hsl.s = s; HSL.L = l;returnHSL;}
b) The algorithm description of the HSL→RGB.
Step 1:if s=0, which is gray, defines r,g and B as L.
Step 2: Otherwise, test L:
If l<0.5,temp2=l* (1.0+s)
If l>=0.5,temp2=l+s-l*s
Step 3:TEMP1=2.0*-TEMP2
Step 4: Convert H to 0~1.
Step 5: For R,g,b, calculate an additional temporary value of Temp3. Here's how:
For R, temp3=h+1.0/3.0
For G, Temp3=h
For B, temp3=h-1.0/3.0
If temp3<0, temp3=temp3+1.0
If temp3>1, temp3=temp3-1.0
Step 6: Do the following tests for R,g,b:
If 6.0*temp3<1,color=temp1+ (TEMP2-TEMP1) *6.0*temp3
Else if 2.0*TEMP3<1,COLOR=TEMP2
Else if 3.0*temp3<2,
color=temp1+ (TEMP2-TEMP1) * ((2.0/3.0)-temp3) *6.0
Else COLOR=TEMP1
Private StaticColorrgbColorhsltorgb(COLORHSL HSL) {COLORRGB RGB =NewColorrgb ();floatR, G, B, H, S, l;floatTemp1, Temp2, TEMPR, Tempg, TEMPB; h = hsl.h/360.0F s = hsl.s; L = HSL.L;if(s = =0) {r = G = b = l; }Else{if(L <0.5) Temp2 = L * (1+ s);ElseTemp2 = (L + s)-(L * s); Temp1 =2* L-TEMP2; TEMPR = (float) (H +1.0/3.0);if(Tempr >1) tempr--; Tempg = h; TEMPB = (float) (H-1.0/3.0);if(TEMPB <0) tempb++;//Red if(Tempr <1.0/6.0) R = (float) (Temp1 + (TEMP2-TEMP1) *6.0* TEMPR);Else if(Tempr <0.5) R = Temp2;Else if(Tempr <2.0/3.0) R = (float) (Temp1 + (TEMP2-TEMP1) * (2.0/3.0)-TEMPR) *6.0);ElseR = Temp1;//Green if(Tempg <1.0/6.0) G = (float) (Temp1 + (TEMP2-TEMP1) *6.0* Tempg);Else if(Tempg <0.5) g = Temp2;Else if(Tempg <2.0/3.0) G = (float) (Temp1 + (TEMP2-TEMP1) * (2.0/3.0)-Tempg) *6.0);Elseg = Temp1;//Blue if(TEMPB <1.0/6.0) B = (float) (Temp1 + (TEMP2-TEMP1) *6.0* TEMPB);Else if(TEMPB <0.5) b = Temp2;Else if(TEMPB <2.0/3.0) B = (float) (Temp1 + (TEMP2-TEMP1) * (2.0/3.0)-TEMPB) *6.0);Elseb = Temp1; } RGB.R = (int) Math.Round (R *255.0); RGB.G = (int) Math.Round (G *255.0); rgb.b = (int) Math.Round (b *255.0);returnRGB;}
Android Camera Live Filter (vii)