RGB HSL Conversion Formulas

xiaoxiao2021-03-05  21

Comes from: http://www.paris-pc-gis.com/mi_enviro/colors/color_models.htm#6-4 R , G , B <> H , S , lor : http://130.113.54.154/~monger/hsl-rgb.html

RGB> HSL

1 - Convert the RBG VALUES TO The Range 0-1

Example: from The Video Colors Page, ColorBar Red Has r = 83%, B = 7%, G = 7%, or in this scale, r = .83, b = .07, g = .07

2 - Find min and max values ​​of r, b, g

In the example, maxcolor = .83, mincolor = .07

3 - l = (MaxColor MinColor) / 2

For the example, l = (.83 .07) / 2 = .45

4 - IF The max and min colors areic (I.E. The Color Is Some Kind of Grey), S Is Defined To BE 0, And H IS undefined But in Programs Usually Written As 0. (All IS DONE)

5 - OtherWise, Test L.

IF L <0.5, S = (MaxColor-minColor) / (MaxColor Mincolor)

IF L> = 0.5, s = (MaxColor-minColor) / (2.0-maxColor-minColor)

FOR THE EXAMPLE, L = 0.45 SO S = (. 83-.07) / (. 83 .07) = .84

6 - Compute

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)

For THE EXAMPLE, R = MaxColor SO H = (. 83-.07) = 0

7 - To Use the Scaling Shown in The Video Color Page, Convert L and S Back to Percentages, And H Into An Angle in Degrees (IE Scale IT from 0-360).

8 - From The Computation in Step 6, H Will Range from 0-6. RGB Space IS A Cube, And HSL Space IS A Double Hexacone, WHERE L Is The Principal Diagonal of The RGB Cube. Thus Corners of the RGB Cube; Red , YELLOW, GREEN, CYAN, Blue, And Magenta, Become The Vertices of The HSL Hexagon. The value 0-6 for h tells you which section of the hexagon you are in. h Is Most Commonly Given As in Degrees, SO To Converth = h * 60.0

9 - IF H Is Negative, Add 360 To Complete The Conversion.

HSL

1 - IF S = 0, Define R, G, And B All to L (All IS DONE)

2 - OtherWise, Test L.

IF L <0.5, TEMP2 = L * (1.0 S)

IF L> = 0.5, TEMP2 = L S - L * S

In The Colorbar Example for ColorBar Green, H = 120, L = 52, S = 79, SO Converting To the Range 0-1, L = .52, S = .79, SO TEMP2 = (. 52 .79) - (.52 * .79) = .899

3 - Compute

TEMP1 = 2.0 * L - TEMP2

In The Example, Temp1 = 2.0 * .52 - .899 = .141

4 - Convert H to the Range 0-1

In The Example, H = 120/360 = .33

5 - for Each of R, G, B, Compute Another Temporary Value, Temp3, As Follows:

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

In the example, RTEMP3 = .33 .33 = .66, gtemp3 = .33, btemp3 = .33- .33 = 0

7 - for Each of R, G, B, DO The Following Test:

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

In The Example,

3.0 * RTEMP3 <2 SO r = .141 (((2.0 / 3.0-.66) * 6.0 = .141

2.0 * gtemp3 <1 so g = .899

6.0 * Btemp3 <1 so b = .141 (. 899-.141) * 6.0 * 0 = .1418 - Scale Back to the Range 0-100 to use the scaling shown in the video color Page

For the example, r = 14, g = 90, b = 14 color in video

转载请注明原文地址:https://www.9cbs.com/read-39280.html

New Post(0)