package csscolorparserimport// D50 reference white (CIE xy = 0.3457, 0.3585), as used by CSS Color 4.const ( d50X = 0.3457 / 0.3585 d50Y = 1.0 d50Z = (1.0 - 0.3457 - 0.3585) / 0.3585)// CIE constants for Lab.const ( epsilon = 216.0 / 24389.0// (6/29)^3 kappa = 24389.0 / 27.0// (29/3)^3)// linearRGBToXYZD50 converts linear-light sRGB (components in 0..1, no gamma)// to CIE XYZ relative to the D50 white point.//// The matrix is sRGB primaries (D65) chromatically adapted to D50 with the// Bradford transform.func linearRGBToXYZD50(, , float64) (, , float64) { = 0.4360747* + 0.3850649* + 0.1430804* = 0.2225045* + 0.7168786* + 0.0606169* = 0.0139322* + 0.0971045* + 0.7141733*return}// xyzD50ToLinearRGB converts CIE XYZ (D50) to linear-light sRGB.// It is the inverse of linearRGBToXYZD50. Results are not clamped, so// out-of-gamut colors yield components below 0 or above 1.func xyzD50ToLinearRGB(, , float64) (, , float64) { = 3.1338564* - 1.6168668* - 0.4906148* = -0.9787686* + 1.9161416* + 0.0334541* = 0.0719452* - 0.2289913* + 1.4052427*return}// xyzD50ToLab converts CIE XYZ (D50) to CIE Lab (D50).// L is in 0..100; a and b are roughly in -125..125.func xyzD50ToLab(, , float64) (, , float64) { := labF( / d50X) := labF( / d50Y) := labF( / d50Z) = 116* - 16 = 500 * ( - ) = 200 * ( - )return}func labF( float64) float64 {if > epsilon {returnmath.Cbrt() }return (kappa* + 16) / 116}// labToXYZD50 converts CIE Lab (D50) to CIE XYZ relative to the D50 white point.func labToXYZD50(, , float64) (, , float64) { := ( + 16) / 116 := /500 + := - /200 := * * := * * if > epsilon { = } else { = (116* - 16) / kappa }if > kappa*epsilon { = * * } else { = / kappa }if > epsilon { = } else { = (116* - 16) / kappa }return * d50X, * d50Y, * d50Z}// linearRGBToLab is a convenience wrapper chaining linear RGB -> XYZ D50 -> Lab.func linearRGBToLab(, , float64) (, , float64) { , , := linearRGBToXYZD50(, , )returnxyzD50ToLab(, , )}// labToLinearRGB converts CIE Lab (D50) to linear-light sRGB by chaining// Lab -> XYZ D50 -> linear RGB. Results are not clamped.func labToLinearRGB(, , float64) (, , float64) { , , := labToXYZD50(, , )returnxyzD50ToLinearRGB(, , )}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.