In the computer, RGB (red, green, blue), red, green, blue, and HSL (hue, saturation, lightness) colors, saturation, and brightness are our two most commonly used color formats, in some cases, we need to convert them accordingly. The principle of conversion between them is as follows: (more color format conversion can be viewed: http://www.easyrgb.com/index.php? X = math)
RGB ------ HSL code
Var_r = (R / 255 ) // RGB from 0-255
Var_g = (G / 255 )
Var_ B = (B / 255 )
Var_min = Min (var_r, var_g, var_ B) // Min. Value of RGB
Var_max = Max (var_r, var_g, var_ B) // Max. Value of RGB
Del_max = Var_max - Var_min // Delta RGB Value
L = (Var_max + Var_min) / 2
If (Del_max = 0 ) // This is a gray, no chroma...
{
H = 0 // HSL results from 0 to 1
S = 0
}
Else // Chromatic data...
{
If (L < 0.5 ) S = Del_max / (Var_max + Var_min)
Else S = Del_max / ( 2 - Var_max - Var_min)
Del_r = (Var_max - Var_r) / 6 ) + (Del_max / 2 )) / Del_max
Del_g = (Var_max - Var_g) / 6 ) + (Del_max / 2 )) / Del_max
Del_ B = (Var_max - Var_ B) / 6 ) + (Del_max / 2 )) / Del_max
If (Var_r = Var_max) h = Del_ B - Del_g
Else If (Var_g = Var_max) h = ( 1 / 3 ) + Del_r - Del_ B
Else If (Var_ B = Var_max) h = ( 2 / 3 ) + Del_g - Del_r
If(H< 0); H+ = 1
If(H> 1); H-= 1
}
HSL ----- RGB code
If (S = 0 ) // HSL from 0 to 1
{
R = L * 255 // RGB results from 0 to 255
G = L * 255
B = L * 255
}
Else
{
If (L < 0.5 ) Var_2 = L * ( 1 + S)
Else Var_2 = (L + S) - (S * L)
Var_1= 2 *L-Var_2
R = 255 * Hue_2_rgb (var_1, var_2, h + ( 1 / 3 ))
G = 255 * Hue_2_rgb (var_1, var_2, H)
B = 255 * Hue_2_rgb (var_1, var_2, h - ( 1 / 3 ))
}
--------------------------------------------------------------------------------
Hue_2_rgb (V1, V2, or otherwise) // Function hue_2_rgb
{
If (FLAC < 0 ) VL + = 1
If (FLAC > 1 ) VL -= 1
If (( 6 * VL) < 1 ) Return (V1 + (V2 - V1) * 6 * VL)
If (( 2 * VL) < 1 ) Return (V2)
If (( 3 * VL) < 2 ) Return (V1 + (V2 - V1) * (( 2 / 3 ) - VL) * 6 )
Return (V1)
}