color.js: Converting sRGB to LCH when: Red Green and Blue are Equal (Bug)
When attempting to convert an sRGB to LCH, where the chroma should be 0, produces unexpected values in the Chroma and Hue channels. The chroma value appears to have a floating point error when it should be 0, and the hue value is always NaN, which causes math problems.
Take the following examples:
new Color('srgb', [0.1, 0.1, 0.1]).to('lch').coords
(3) [9.010443527068848, 0.000006337680094534959, NaN]
new Color('srgb', [0.13333, 0.13333, 0.13333]).to('lch').coords
(3) [13.227498037447411, 0.000007406287423610401, NaN]
new Color('srgb', [0.4, 0.4, 0.4]).to('lch').coords
(3) [43.192291386569806, 0.000014999406423138985, NaN]
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 16
Commits related to this issue
- illuminants: Use D65 xy chromaticities from sRGB specification These are technically the "correct" values to use for sRGB, for both accuracy and compatibility. More info: https://github.com/LeaVerou... — committed to kdrag0n/colorkt by kdrag0n 3 years ago
- CieXyz: Optimize matrices to minimize chroma in neutral sRGB colors The new sRGB->XYZ matrix was calculated using coloraide's script [1] and D65 xy chromaticities from the sRGB specification [2]. Eac... — committed to kdrag0n/colorkt by kdrag0n 3 years ago
- CieXyz: Optimize matrices to minimize chroma in neutral sRGB colors The new sRGB->XYZ matrix was calculated using coloraide's script [1] and D65 xy chromaticities from the sRGB specification [2] [3].... — committed to kdrag0n/colorkt by kdrag0n 3 years ago
- CieXyz: Optimize matrices to minimize chroma in neutral sRGB colors The new sRGB->XYZ matrix was calculated using coloraide's script [1] and D65 xy chromaticities from the sRGB specification [2] [3].... — committed to kdrag0n/colorkt by kdrag0n 3 years ago
- illuminants: Use D65 xy chromaticities from sRGB specification These are technically the "correct" values to use for sRGB, for both accuracy and compatibility. More info: https://github.com/LeaVerou... — committed to kdrag0n/colorkt by kdrag0n 3 years ago
- CieXyz: Optimize matrices to minimize chroma in neutral sRGB colors The new sRGB->XYZ matrix was calculated using coloraide's script [1] and D65 xy chromaticities from the sRGB specification [2] [3].... — committed to kdrag0n/colorkt by kdrag0n 3 years ago
Unless I do the first-principles spectral conversion and get different results in the first 5 figures, it will be these ones going forward.
I ran your Python program with these XYZ values, and got the same results as my JavaScript program.
So I’ve gone through the math, I now understand why color.js is so off.
The sRGB matrix that color.js uses is based off a calculated white point for D65:
This gives us
But then we use the following D65 white point any time we do chromatic adaption:
This gives us quite a bit of error.
With the following code, doing nothing tricky, I follow the algorithm calculating the XYZ conversion matrix based on this algorithm: http://www.brucelindbloom.com/index.html?Math.html.
I will output the first results based on using a consistent white point with what is used in chromatic adaption and then I’ll output the same matrix with a calculated white point.
The first results match what is used in the brucelindbloom calculator and give much better results. The second matches the matrix that is currently used.
Based on this, I am much more confident with my opinion on this. My recommendation would be to use the matrix that is consistent with the white point that is being used everywhere else or update the white point in chromatic adaption to match what is used in the matrix calculations.