Source: http://www.easyrgb.com/math.html
// ******************************************************** ******************************** XYZ -> RGB
REF_X = 95.047 // Observer = 2 °, Illuminant = D65Ref_y = 100.000Ref_z = 108.883
VAR_X = x / 100 // x = from 0 to ref_xvar_y = y / 100 // y = from 0 to ref_yvar_z = z / 100 // z = from 0 to Ref_y
VAR_R = var_x * 3.2406 var_y * -1.5372 var_z * -0.4986VAR_G = var_x * -0.9689 var_y * 1.8758 var_z * 0.0415VAR_B = var_x * 0.0557 var_y * -0.2040 var_z * 1.0570
if (var_R> 0.0031308) var_R = 1.055 * (var_R ^ (1 / 2.4)) - 0.055else var_R = 12.92 * var_Rif (var_G> 0.0031308) var_G = 1.055 * (var_G ^ (1 / 2.4)) - 0.055else var_G = 12.92 * var_gif (var_b> 0.0031308) VAR_B = 1.055 * (var_b ^ (1 / 2.4)) - 0.0555ELSE var_b = 12.92 * var_b
R = var_r * 255g = var_g * 255b = var_b * 255 // ******************************************* *********************************** RGB -> XYZ
VAR_R = (R / 255) // r = from 0 to 255var_g = (g / 255) // g = from 0 to 255VAR_B = (b / 255) // b = from 0 to 255
IF (var_r> 0.04045) VAR_R = ((var_r 0.055) / 1.055) ^ 2.4ELSE var_r = var_r / 12.92IF (var_g> 0.04045) var_g = ((var_g 0.055) / 1.055) ^ 2.4ELSE var_g = var_g / 12.92 IF (var_b> 0.04045) var_b = ((var_b 0.055) / 1.055) ^ 2.4ELSE VAR_B = var_b / 12.92
var_R = var_R * 100var_G = var_G * 100var_B = var_B * 100 // Observer. = 2 °, Illuminant = D65X = var_R * 0.4124 var_G * 0.3576 var_B * 0.1805Y = var_R * 0.2126 var_G * 0.7152 var_B * 0.0722Z = var_r * 0.0193 var_g * 0.1192 var_b * 0.9505 // *************************************************** ******************************************* XYZ -> YXY
// x = from 0 to 95.047 Observer. = 2 °, Illuminant = D65 // y = from 0 to 100.000 // z = from 0 to 108.883
Y = yx = x / (x y z) y = y / (x y z)
// ******************************************************** *********************** YXY -> XYZ
// y = from 0 to 100 // x = from 0 to 1 // y = from 0 TO 1
X = x * (y / y) y = yz = (1 - x - y) * (Y / Y) // ******************** *********************************************************** XYZ -> Hunter-Lab
(H) L = 10 * SQRT (Y) (h) A = 17.5 * ((1.02 * x) - Y) / SQRT (Y)) (h) b = 7 * ((Y - (0.847 * z) ) / SQRT (Y)) // *************************************************** ****************************** HUNTER-LAB -> XYZ
VAR_Y = (h) L / 10Var_x = (h) A / 17.5 * (h) L / 10Var_z = (h) B / 7 * (H) L / 10
Y = var_y ^ 2x = (var_x out_y) / 1.02z = - (var_z - out_y) / 0.847 // *************************** **************************************************************************************************************************** XYZ -> CIE-L * AB
VAR_X = X / 95.047 // Observer = 2 °, Illuminant = D65VAR_Y = Y / 100.000Var_z = z / 108.883
IF (var_x> 0.008856) var_x = var_x ^ (1/3) Else var_x = (7.787 * var_x) (16/116) IF (var_y> 0.008856) var_y = var_y ^ (1/3) else var_y = (7.787 * VAR_Y) (16/116) IF (var_z> 0.008856) var_z = var_z ^ (1/3) else var_z = (7.787 * var_z) (16/116) CIE-L * = (116 * var_y) - 16CIE- A * = 500 * (var_x - var_y) CIE-B * = 200 * (var_y - var_z) // ********************************** ******************************************** CROE-L * AB -> XYZ
VAR_Y = (CIE-L * 16) / 116var_x = CIE-A * / 500 var_yvar_z = var_y - CIE-B * / 200
IF (var_y ^ 3> 0.008856) var_y = var_y ^ 3ELSE VAR_Y = (VAR_Y - 16/116) / 7.787IF (var_x ^ 3> 0.008856) var_x = var_x ^ 3ELSE VAR_X = (VAR_X - 16/116) / 7.787IF ( VAR_Z ^ 3> 0.008856) VAR_Z = var_z ^ 3ELSE VAR_Z = (VAR_Z - 16/116) / 7.787
X = Ref_x * var_x // ref_x = 95.047 Observer = 2 °, Illuminant = D65Y = Ref_y * var_y // REF_Y = 100.000Z = Ref_z * var_z // = 108.883 // ********** *********************************************************** *********** CIE-L * AB -> CIE-L * CH ° VAR_H = arc_tangent (CIE-B *, CIE-A *) // Quadrant by Signs
IF (var_h> 0) var_h = (var_h / pi) * 180ELSE VAR_H = 360 - (ABS (VAR_H) / PI) * 180
CIE-L * = CIE-L * CIE-C * = SQRT (CIE-A * ^ 2 CIE-B * ^ 2) CIE-H ° = var_h // *********** *********************************************************** ********** XYZ -> CIE-L * UV var_u = (4 * x) / (x (15 * y) (3 * z)) VAR_V = (9 * Y) / ( X (15 * Y) (3 * z)) VAR_Y = Y / 100IF (var_y> 0.008856) var_y = var_y ^ (1/3) else var_y = (7.787 * var_y) (16/116)
REF_X = 95.047 // Observer = 2 °, Illuminant = D65Ref_y = 100.000Ref_z = 108.883
REF_U = (4 * ref_x) / (Ref_x (15 * REF_Y) (3 * REF_Z)) REF_V = (9 * REF_Y) / (Ref_x (15 * REF_Y) (3 * Ref_z))
CIE-L * = (116 * var_y) - 16CIe-U * = 13 * CIE-L * * (var_u - ref_u) CIE-V * = 13 * CIE-L * * (var_v - ref_v) // *** *********************************************************** ****************** CIE-L * UV -> XYZ
VAR_Y = (CIE-L * 16) / 116IF (var_y ^ 3> 0.008856) VAR_Y = var_y ^ 3ELSE VAR_Y = (VAR_Y - 16/116) / 7.787
REF_X = 95.047 // Observer = 2 °, Illuminant = D65Ref_y = 100.000Ref_z = 108.883
REF_U = (4 * ref_x) / (Ref_x (15 * REF_Y) (3 * REF_Z)) REF_V = (9 * REF_Y) / (Ref_x (15 * REF_Y) (3 * Ref_z))
VAR_U = CIE-U * / (13 * CIE-L *) Ref_uvar_v = CIE-V * / (13 * CIE-L *) Ref_v
Y = var_y * 100x = - (9 * y * var_u) / (var_u - 4) * var_v - var_u * var_v) z = (9 * y - (15 * var_v * y) - (var_v * x)) / (3 * VAR_V) // ************************************************** ***************************** RGB -> HSL
VAR_R = (R / 255) // RGB VALUES = from 0 to 255VAR_G = (g / 255) var_b = (b / 255) var_min = min (var_r, var_g, var_b) // min. Value of RGBVAR_MAX = Max (var_r , var_g, var_b) // max. Value of rgbdel_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 ... {= 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 IF (s == 0) // HSL VALUES = 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, VH) // Function Hue_2_RGB {IF (VH <0) VH = 1 IF (VH> 1) VH - = 1 IF ((6 * VH) <1) Return (V1 (V2 - V1) * 6 * VH) IF ((2 * vH) <1) RETURN (V2) IF ((3 * vH) <2) RETURN (V1 (V2 - V1) * ((2/3) - VH) * 6) Return (V1)} // ************************************************** ******************************** RGB -> HSV VAR_R = (R / 25) // RGB VALUES = from 0 TO 255VAR_G = (g / 255) var_b = (b / 255)
VAR_MIN = min (var_r, var_g, var_b) // min. Value of rgbvar_max = max (var_r, var_g, var_b) // max. Value of rgbdel_max = var_max - var_min // delta RGB Value
V = var_max
IF (del_max == 0) // this is a gray, no chroma ... {h = 0 // hsv results = from 0 to 1 s = 0} else // chromatic data ... {s = del_max / var_max
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} // ************************ ******************************************** HSV -> RGB IF (s == 0) // hsv value = from 0 to 1 {r = v * 255 // RGB Results = from 0 to 255 g = v * 255 b = v * 255} else {var_h = h * 6 VAR_I = INT (var_h) // or ... var_i = floor var_1 = v * (1 - s) var_2 = v * (1 - s * (var_h - var_i)) var_3 = v * (1 - s * (1 - (var_h - var_i)))) f (var_i == 0) {var_r = v; var_g = var_3; var_b = var_1} else if (var_i == 1) {var_r = var_2; var_g = v; var_b = VAR_1} else if (var_i == 2) {var_r = var_1; var_r = V; var_b = var_3} else if (var_i == 3) {var_r = var_1; var_g = var_2; var_b = v} else if (var_i == 4) {var_r = var_3; var_g = var_1; var_b = v} else {var_r = v; var_g = var_1; var_b = var_2}
R = var_r * 255 // RGB Results = from 0 to 255 g = var_g * 255 b = var_b * 255}} // ********************** *********************************************************** Range of HSL, HSB and HSV in Popular Applications Space H Range S Range L / V / B Range Paint Shop Pro HSL 0 - 255 0 - 255 L 0 - 255 GIMP HSV 0 - 360 ° 0 - 100 V 0 - 100Photoshop HSV 0 - 360 ° 0 - 100% B 0 - 100% Windows HSL 0 - 240 0 - 240 L 0 - 240Linux / KDE HSV 0 - 360 ° 0 - 255 V 0 - 255GTK HSV 0 - 360 ° 0 - 1.0 V 0 - 1.0Java (AWT.COLOR) HSV 0 - 1.0 0 - 1.0 B 0 - 1.0Apple HSV 0 - 360 ° 0 - 100% L 0 - 100% // *************** *********************************************************** ******* RGB -> CMY / / RGB VALUES = from 0 to 255C = 1 - (R / 255) M = 1 - (g / 255) Y = 1 - (B / 255)
// ******************************************************** *********************** CMY -> RGB // CMY VALUES = from 0 to 1
R = (1 - c) * 255g = (1 - m) * 255B = (1 - y) * 255 // ********************** ***************************************************************************** CMY -> CMYK // CMY VALUES = from 0 to 1
VAR_K = 1
IF (c C = (c - var_k) / (1 - var_k) m = (m - var_k) / (1 - var_k) y = (y - var_k) / (1 - var_k) k = var_k // ****** *********************************************************** **************** CMYK -> CMY // CMYK VALUES = from 0 TO 1 C = (C * (1 - k) K) m = (M * (1 - k) K) y = (Y * (1 - k) K) // ********* *********************************************************** ************* XYZ (Tristimulus) Reference values of a perfect reflecting diffuserObserver 2 ° (CIE 1931) 10 ° (CIE 1964) Illuminant X2 Y2 Z2 X10 Y10 Z10A (Incandescent) 109.850 100 35.585 111.144 100 35.200C 98.074 100 118.232 97.285 100 116.145D50 96.422 100 82.521 96.720 100 81.427D55 95.682 100 92.149 95.799 100 90.926D65 (Daylight) 95.047 100 108.883 94.811 100 107.304D75 94.972 100 122.638 94.416 100 120.641 F2 (Fluorescent) 99.187 100 67.395 103.280 100 69.026 F7 95.044 100 108.755 95.792 100 107.687F11 100.966 100 64.370 103.866 100 65.627 // ******************************************************** ***********************