You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For the background and explanation of why to add this feature, please see this Color Workshop presentation which has a video presentation, plus associated slides and transcript. You can just skim the slides/transcript if you are pressed for time.
Having done so, and given the rationale presented, the specific proposal is to add OKLab as follows: Edit: items re-ordered so highest priority is first.
CSS Color 4
in the gamut mapping section, require gamut reduction to use OKLCH chroma reduction and deltaE OK, rather than CIE LCH chroma reduction and deltaE 2000. This is both better and faster. If we don't do this, I am not confident of being able to write a satisfactory gamut mapping section in CSS Color 4. See examples below
in the interpolation section, for non-legacy color formats, if the host syntax does not specify a colorspace, change lab to oklab as the default. If we don't do this, color interpolation in gradients, animations and transitions will not be as good, and will sometimes give surprising hue shifts.
add oklab() and oklch() (as well as the existing lab() and lch() which are still useful) and add their implementation to the sample code. Not doing this means we have a colorspace of proven usefulness, implemented inside the browser, but we deny users access to it.
CSS Color 5
add oklab and oklch as defined color spaces which can be used in color-mix(). Depends on 3.
add oklab() and oklch() to Relative Color Syntax. Depends on 3.
Note - the gamut mapping section does not yet exist, because gamut mapping in CIE LCH gave unacceptable results in the 270-330 degree hue range and also because deltaE 2000 is complex, but is needed for good results and deltaE 76 is fast but not good enough.
Using OKLCH and deltaEOK (which is the simple root-rum-of-squares) gives better results and is computationally simple.
The new Lr lightness estimator brings in a dependency on viewing conditions, which the original OK L did not have; and which is an advantage of the original OK L especially if it allows extension to HDR or at least EDR. Lr also adds a little computational complexity.
I would like to examine it some more and also see how it impacts deltaE, but my initial impression is to stick with the original OK L. I'm very open to thoughts on which would be the better option though.
I would like to examine it some more and also see how it impacts deltaE, but my initial impression is to stick with the original OK L. I'm very open to thoughts on which would be the better option though.
Makes sense. I've only played with Lr to experiment with Okhsl and Okhsv, but use it in no other way. So there may be good reasons to avoid it if using it for distancing. I also hadn't thought of implications in regards to HDR. Something to experiment with though.
I quickly read thru the slides, and didn't catch this detail - can you elaborate on why we'd want to add these in addition to the existing lab()/lch(), rather than just changing those functions to use the OKLab space? Is there a good reason to keep the existing Lab functions?
why we'd want to add these in addition to the existing lab()/lch(), rather than just changing those functions to use the OKLab space? Is there a good reason to keep the existing Lab functions?
Good question, and the main reason I suggest keeping them around is because there is lots of hardware (instrumentation, like spectrophotometers) and software which generates Lab (or LCH, or both) values. The secondary reason is that Lab and LCH have decades of user experience with them, while OKLab/OKLCH have less than a year.
So:
to specify a color, people can continue to use lab() and lch() because that is convenient for them. They can read out a value from Photoshop or Krita or whatever and use it directly. In much the same way they can continue to use a98rgb() despite display-p3 being a more popular choice nowadays.
for general interpolation, if the host syntax doesn't specify, then the default changes from Lab to oklab because that will generally give better results; but you can still get lab if you want it, by specifying explicitly.
for gamut mapping, we just go straight to oklch because it is strictly better.
My issue is just that, given a choice between "lab" and "oklab", where the two look practically identical in most cases, people will naturally reach for "lab". We should bless the shortest, most convenient names with the most preferred syntaxes, even if we allow other types in other ways. (And in particular, we've accidentally set a precedent of all the specialized color functions have three-letter names. That probably can't persist forever, but for now, at least, "oklab" would join "color" as looking a little out-of-place and possibly "advanced".
Presumably we'd be able to offer the old Lab via color()? (Not LCH, tho.)
So, I am curious. It seems there is talk about using chroma reduction using Oklch for gamut mapping/reduction. I was playing around with this and was noticing some interesting things when gamut mapping using this method. Granted, maybe I'm doing something wrong here. This is based on the same algorithm used in color.js for Lch chroma and using the currently specified ∆Eok in that library.
When using something like display-p3, things get mapped very similar as they do with Lch vs Oklch, granted I think their blue range is very similar. But when using something like rec2020 which has a wider blue range, the mapping gets dull blues. (oklch chroma reduction on top).
This doesn't happen as much with greens and lesser with reds. Now granted there doesn't seem to be an official algorithm posted yet, and so I'm more curious if all these edge cases have been evaluated. Maybe the actual algorithm that is to be used compensates for this. I'm more curious if it improves in some places, but lesser in others, or if there is a better algorithm (different from the CIELCH one used on colorjs.io) that is planned to be used that works well with Oklch?
It's possible I'm just not implementing correctly, and in that case, I will wait patiently to better understand how the reduction is actually done, or keep plugging away to see what I'm doing wrong 🙂.
Thanks for investigating. Any chance you could post your test code?
In the diagram, are the colors being displayed in sRGB? Is it just the portion of the line from neutral to the sRGB gamut boundary, being displayed? (so Rec2020 blue to sRGB gamut boundary is not on that diagram)? It would be good to know what colors are at the boundary, for the two methods.
@svgeesus I only just became aware of these efforts to improve the behaviour of the OKLab matrices when used with reference white of xy=(0.3127, 0.3290). This is work I have also done independently, so I thought I should share my results.
I have adjusted the four matrices such that they are accurate to float64 (IEEE 754 double precision), see code fragment below.
As far as I can tell the XYZtoLMS/LMStoXYZ/LMStoOKLab/OKLabtoLMS matrices currently in use (see here) are accurate to float32 precision. You'll notice that they match my numbers to somewhere between 6 and 8 significant figures, i.e. the expected precision of float32, or thereabouts.
You should find that that my proposed values are slightly better behaved due to their additional precision, i.e. a D65 grey will map to LMS with components L=M=S, and OKLab with components a=0 and b=0.
The M1 matrix fix (which relates XYZtoLMS and LMStoXYZ) involves applying gains in LMS space such that a chromaticity of xy=(0.3127, 0.3290) is exactly mapped to LMS=(1,1,1).
The M2 matrix fix (which relates to LMStoOKLab and OKLabtoLMS) involves interpreting the originally published numbers as float32 numbers, and then emitting the resulting matrix and its inverse in float64 precision. While this might sound like a bad idea, it is the correct approach because Bjorn's original optimisation emitted M1 and M2 at float32 precision, printed to 10 d.p. . Therefore it is correct to interpret them as float32 values. The result of handling the numbers in their native datatype is that my OKLabtoLMS matrix ends up with exact values (1.0, 1.0, 1.0) for its first column, and the rows of LMStoOKLab sum to (1, 0, 0). Note that due to the inherent precision of float64 there may be discrepencies in the order of 1e-15 when summing matrix rows, which is unavoidable and of no real world consequence.
Note that my knowledge of M1 and M2 matrices being defined to float32 precision comes from private communications with Bjorn in February 2021.
Note that all my intermediate calculations were carried out in "infinite precision" using a rational number library before being rounded to float64. The numeric values in the code snippet below have sufficient decimal places to exactly map to the intended float64 value, generally 16 or 17 significant figures.
functionXYZ_to_OKLab(XYZ){// Given XYZ relative to D65, convert to OKLabvarXYZtoLMS=[[0.819022442647055,0.3619062604571286,-0.12887378595068213],[0.0329836558550745,0.9292868602921457,0.03614466586101669],[0.04817719382823899,0.2642395276821537,0.6335478283072317]];varLMStoOKLab=[[0.21045426824930336,0.7936177747759865,-0.00407204302528986],[1.977998539071675,-2.4285922502018127,0.4505937111301374],[0.025904030547901084,0.7827717270900503,-0.8086757576379514]];varLMS=multiplyMatrices(XYZtoLMS,XYZ);returnmultiplyMatrices(LMStoOKLab,LMS.map(c=>Math.cbrt(c)));// L in range [0,1]. For use in CSS, multiply by 100 and add a percent}functionOKLab_to_XYZ(OKLab){// Given OKLab, convert to XYZ relative to D65varLMStoXYZ=[[1.226879868224423,-0.5578149934498884,0.281391052277137],[-0.04057574728813353,1.1122868053350754,-0.07171105804694196],[-0.0763729441641797,-0.42149332236303205,1.5869240172870902]];varOKLabtoLMS=[[1.0,0.3963377773761749,0.21580375730991364],[1.0,-0.10556134581565857,-0.0638541728258133],[1.0,-0.08948418498039246,-1.2914855480194092]];varLMSnl=multiplyMatrices(OKLabtoLMS,OKLab);returnmultiplyMatrices(LMStoXYZ,LMSnl.map(c=>c**3));}
I can share my workings if desired, but it will take a bit of time to make it work as a standalone code fragment that is decoupled from proprietary libraries. In the mean time please feel free to try out these refined numbers.
@JamesEggleton I'd love to see the work! One of the issues with the current matrices is that Oklab and OkLCh just don't get chroma as close to zero when colors are achromatic. That leads to messy results when assuming zero for chroma or zero for a and b in Oklab, especially as lightness increases. This also makes the hue have more influence in good conversions as chroma is not quite small enough to completely drown out its influence.
But your matrices actually yield some pretty good results and have a "tightness" in the conversion that is more inline with the current Lab and LCh conversions:
@facelessuser I'm happy to see you achieved decent results using these matrices. I'll clean up and share my workings as soon as I get a chunk of free time, hopefully in the next week or so...
Objectively, as someone that supports Oklab in a library outside of CSS, I can generally say these matrices seem to give values that you would expect while giving a better response for chroma and pretty much eliminating the effect of hue on achromatic colors.
This has nothing to do with CSS
With that said, and this would not specifically be a problem for CSS, I also support Okhsl and Okhsv. Using both the XYZtoLMS and LMStoOKLab matrix seems to have a noticeable impact on those two color spaces. I've tried using the workbook provided by Björn in his blog, updating the values to give better Okhsl and Okhsv support with these matrices, but there seems to be some tuning in the translation algorithm's not quite explained. Blue was pushed further out of the gamut, and then the algorithm would generate values too large for the language I was in and cause an overflow (probably due to the high precision of the values).
What I did find was that by simply using the more accurate LMStoOKLab matrix, I was able to get the same great response driving chroma to zero without causing a disparity between Oklab and Okhsl/Okhsv. I did not increase the precision of matrix calculations in Okhsl/Okhsv. Round trip was excellent between all these spaces.
I am most interested in getting the float 64 representation of this matrix.
I wonder if the fact that I use a non-rational XYZtoRGB matrix everywhere else and that the calculation as you suggested used a rational number calculated XYZtoRGB matrix causes some disparity. Or if the Okhsl and Okhsv algorithm just isn't tuned well for these high-precision matrices and a slight shift to the response.
Regardless, using the high-precision LMStoOKLab matrix is more than sufficient to fix achromatic issues and provide good translations.
Okay, I was more systematic and was able to get Okhsl/Okhsv working with higher precision matrices but only if using the newly calculated LMStoOKLab matrix. I was able to use the Okhsl/Okhsv notebook provided in the related blog to update the tuning and no longer get any of the issues I did previously. It even gave better conversions for these spaces as well.
When I added the newly calculated LMS matrix in addition to the Oklab matrix, it seems to throw things a little off (pushing blue further out of gamut as an example), so for now, I'll probably stick with just the improved Oklab matrix until I can verify the LMS matrix. Considering how much better the calculations are with just the OKLAB matrix, I think the LMS matrix at slightly lower accuracy wasn't the driving force in bad chroma calculations.
I do wonder if creating the matrices with rational numbers (which also I assume includes the white point) causes the calculation to be different as everywhere else in my code, the white point is applied with floating point math, which likely leads to a disparity in white points causing calculations to not be quite right. It's also very likely that the Okhsl/Okhsv algorithm just needs some more manual tuning at whatever extreme the new matrix puts it at. I'll look into this more once the work is posted.
Regardless, even with just the improved Oklab matrix, things are actually no worse than when including the LMS matrix. And great conversion back to sRGB:
But it was never posted. My personal interest was to see the approach and verify it. I had noticed an issue using Jame's matrices when applied to Okhsl as well, so I was hesitant to use them without being able to see the process and understand it more. I won't speculate as to exactly what the issue is, only that I had no further insight without being able to evaluate what was actually done. This, of course, does not specifically affect CSS as it does not currently use Okhsl, but it was a concern for me.
I make no claims on the correctness or incorrectness of what James provided. It is likely that there is absolutely nothing wrong with the approach and that Okhsl simply did not handle the 64 bit version of the XYZ_to_LMS portion of the algorithm.
With that out of the way, I can give details as to what is done in Color.js and provide a reproducible example of how it was generated.
The problem with the Oklab matrices as previously defined is that they simply do not resolve oklab(1 0 0) to LMS [1, 1, 1] in 64 bit. This is due to how the 32 bit matrices are applied in a 64 bit data type environment, which Color.js uses. Because of this, as lightness increases, achromatic values for a and b deviate further from zero. When trying to apply CSS logic for powerless values, a larger and larger threshold would need to be used as lightness increases.
CSS simply took the LMS_to_Oklab matrix (the forward transform) and generated the 64 bit inverse matrix for the reverse transform. The problem is that the forward transform does not give you oklab(1 0 0) == LMS [1, 1, 1] in a 64 bit environment.
It turns out the 32 bit inverse matrix does support oklab(1 0 0) == LMS [1, 1, 1], so if you use reverse transform to generate the forward transform for LMS_to_Oklab, you resolve the disparity for achromatic values. You can further emit the reverse transform as 64 bit before inverting the matrix which cleaned up the results a bit more. That is all that was needed and all that was done.
These matrices also did not cause adverse effects when used in Okhsl.
Activity
svgeesus commentedon Sep 20, 2021
Note - the gamut mapping section does not yet exist, because gamut mapping in CIE LCH gave unacceptable results in the 270-330 degree hue range and also because deltaE 2000 is complex, but is needed for good results and deltaE 76 is fast but not good enough.
Using OKLCH and deltaEOK (which is the simple root-rum-of-squares) gives better results and is computationally simple.
argyleink commentedon Sep 20, 2021
lgtm, nice attention to detail
facelessuser commentedon Sep 20, 2021
Curious, would it use the new lightness discussed here? https://bottosson.github.io/posts/colorpicker/#intermission---a-new-lightness-estimate-for-oklab Or would it be based on the original Oklab work that doesn't adjust the lightness?
svgeesus commentedon Sep 24, 2021
The new Lr lightness estimator brings in a dependency on viewing conditions, which the original OK L did not have; and which is an advantage of the original OK L especially if it allows extension to HDR or at least EDR. Lr also adds a little computational complexity.
I would like to examine it some more and also see how it impacts deltaE, but my initial impression is to stick with the original OK L. I'm very open to thoughts on which would be the better option though.
facelessuser commentedon Sep 24, 2021
Makes sense. I've only played with Lr to experiment with Okhsl and Okhsv, but use it in no other way. So there may be good reasons to avoid it if using it for distancing. I also hadn't thought of implications in regards to HDR. Something to experiment with though.
tabatkins commentedon Sep 27, 2021
I quickly read thru the slides, and didn't catch this detail - can you elaborate on why we'd want to add these in addition to the existing lab()/lch(), rather than just changing those functions to use the OKLab space? Is there a good reason to keep the existing Lab functions?
svgeesus commentedon Sep 28, 2021
Good question, and the main reason I suggest keeping them around is because there is lots of hardware (instrumentation, like spectrophotometers) and software which generates Lab (or LCH, or both) values. The secondary reason is that Lab and LCH have decades of user experience with them, while OKLab/OKLCH have less than a year.
So:
lab()andlch()because that is convenient for them. They can read out a value from Photoshop or Krita or whatever and use it directly. In much the same way they can continue to usea98rgb()despitedisplay-p3being a more popular choice nowadays.Labtooklabbecause that will generally give better results; but you can still getlabif you want it, by specifying explicitly.oklchbecause it is strictly better.tabatkins commentedon Sep 28, 2021
My issue is just that, given a choice between "lab" and "oklab", where the two look practically identical in most cases, people will naturally reach for "lab". We should bless the shortest, most convenient names with the most preferred syntaxes, even if we allow other types in other ways. (And in particular, we've accidentally set a precedent of all the specialized color functions have three-letter names. That probably can't persist forever, but for now, at least, "oklab" would join "color" as looking a little out-of-place and possibly "advanced".
Presumably we'd be able to offer the old Lab via
color()? (Not LCH, tho.)Crissov commentedon Sep 29, 2021
Three letters you say?
oklab=>okl(<percentage> <number> <number>)oklch=>okl(<percentage> <percentage> <angle>)But this idea was already dismissed for
cie(). #4481svgeesus commentedon Sep 29, 2021
I don't think any precedent has been set.
facelessuser commentedon Sep 29, 2021
So, I am curious. It seems there is talk about using chroma reduction using Oklch for gamut mapping/reduction. I was playing around with this and was noticing some interesting things when gamut mapping using this method. Granted, maybe I'm doing something wrong here. This is based on the same algorithm used in color.js for Lch chroma and using the currently specified ∆Eok in that library.
When using something like display-p3, things get mapped very similar as they do with Lch vs Oklch, granted I think their blue range is very similar. But when using something like rec2020 which has a wider blue range, the mapping gets dull blues. (oklch chroma reduction on top).
This doesn't happen as much with greens and lesser with reds. Now granted there doesn't seem to be an official algorithm posted yet, and so I'm more curious if all these edge cases have been evaluated. Maybe the actual algorithm that is to be used compensates for this. I'm more curious if it improves in some places, but lesser in others, or if there is a better algorithm (different from the CIELCH one used on colorjs.io) that is planned to be used that works well with Oklch?
It's possible I'm just not implementing correctly, and in that case, I will wait patiently to better understand how the reduction is actually done, or keep plugging away to see what I'm doing wrong 🙂.
svgeesus commentedon Sep 29, 2021
Thanks for investigating. Any chance you could post your test code?
In the diagram, are the colors being displayed in sRGB? Is it just the portion of the line from neutral to the sRGB gamut boundary, being displayed? (so Rec2020 blue to sRGB gamut boundary is not on that diagram)? It would be good to know what colors are at the boundary, for the two methods.
70 remaining items
JamesEggleton commentedon Mar 30, 2023
@svgeesus I only just became aware of these efforts to improve the behaviour of the OKLab matrices when used with reference white of xy=(0.3127, 0.3290). This is work I have also done independently, so I thought I should share my results.
I have adjusted the four matrices such that they are accurate to float64 (IEEE 754 double precision), see code fragment below.
As far as I can tell the XYZtoLMS/LMStoXYZ/LMStoOKLab/OKLabtoLMS matrices currently in use (see here) are accurate to float32 precision. You'll notice that they match my numbers to somewhere between 6 and 8 significant figures, i.e. the expected precision of float32, or thereabouts.
You should find that that my proposed values are slightly better behaved due to their additional precision, i.e. a D65 grey will map to LMS with components L=M=S, and OKLab with components a=0 and b=0.
My methodology has much in common to those proposed by @bottosson (see #6642 (comment)) and @facelessuser (see #6642 (comment)).
The M1 matrix fix (which relates XYZtoLMS and LMStoXYZ) involves applying gains in LMS space such that a chromaticity of xy=(0.3127, 0.3290) is exactly mapped to LMS=(1,1,1).
The M2 matrix fix (which relates to LMStoOKLab and OKLabtoLMS) involves interpreting the originally published numbers as float32 numbers, and then emitting the resulting matrix and its inverse in float64 precision. While this might sound like a bad idea, it is the correct approach because Bjorn's original optimisation emitted M1 and M2 at float32 precision, printed to 10 d.p. . Therefore it is correct to interpret them as float32 values. The result of handling the numbers in their native datatype is that my OKLabtoLMS matrix ends up with exact values (1.0, 1.0, 1.0) for its first column, and the rows of LMStoOKLab sum to (1, 0, 0). Note that due to the inherent precision of float64 there may be discrepencies in the order of 1e-15 when summing matrix rows, which is unavoidable and of no real world consequence.
Note that my knowledge of M1 and M2 matrices being defined to float32 precision comes from private communications with Bjorn in February 2021.
Note that all my intermediate calculations were carried out in "infinite precision" using a rational number library before being rounded to float64. The numeric values in the code snippet below have sufficient decimal places to exactly map to the intended float64 value, generally 16 or 17 significant figures.
I can share my workings if desired, but it will take a bit of time to make it work as a standalone code fragment that is decoupled from proprietary libraries. In the mean time please feel free to try out these refined numbers.
facelessuser commentedon Mar 30, 2023
@JamesEggleton I'd love to see the work! One of the issues with the current matrices is that Oklab and OkLCh just don't get chroma as close to zero when colors are achromatic. That leads to messy results when assuming zero for chroma or zero for a and b in Oklab, especially as lightness increases. This also makes the hue have more influence in good conversions as chroma is not quite small enough to completely drown out its influence.
But your matrices actually yield some pretty good results and have a "tightness" in the conversion that is more inline with the current Lab and LCh conversions:
JamesEggleton commentedon Mar 31, 2023
@facelessuser I'm happy to see you achieved decent results using these matrices. I'll clean up and share my workings as soon as I get a chunk of free time, hopefully in the next week or so...
facelessuser commentedon Mar 31, 2023
Objectively, as someone that supports Oklab in a library outside of CSS, I can generally say these matrices seem to give values that you would expect while giving a better response for chroma and pretty much eliminating the effect of hue on achromatic colors.
This has nothing to do with CSS
With that said, and this would not specifically be a problem for CSS, I also support Okhsl and Okhsv. Using both the
XYZtoLMSandLMStoOKLabmatrix seems to have a noticeable impact on those two color spaces. I've tried using the workbook provided by Björn in his blog, updating the values to give better Okhsl and Okhsv support with these matrices, but there seems to be some tuning in the translation algorithm's not quite explained. Blue was pushed further out of the gamut, and then the algorithm would generate values too large for the language I was in and cause an overflow (probably due to the high precision of the values).What I did find was that by simply using the more accurate
LMStoOKLabmatrix, I was able to get the same great response driving chroma to zero without causing a disparity between Oklab and Okhsl/Okhsv. I did not increase the precision of matrix calculations in Okhsl/Okhsv. Round trip was excellent between all these spaces.I am most interested in getting the float 64 representation of this matrix.
I wonder if the fact that I use a non-rational XYZtoRGB matrix everywhere else and that the calculation as you suggested used a rational number calculated XYZtoRGB matrix causes some disparity. Or if the Okhsl and Okhsv algorithm just isn't tuned well for these high-precision matrices and a slight shift to the response.
Regardless, using the high-precision LMStoOKLab matrix is more than sufficient to fix achromatic issues and provide good translations.
facelessuser commentedon Mar 31, 2023
Okay, I was more systematic and was able to get Okhsl/Okhsv working with higher precision matrices but only if using the newly calculated
LMStoOKLabmatrix. I was able to use the Okhsl/Okhsv notebook provided in the related blog to update the tuning and no longer get any of the issues I did previously. It even gave better conversions for these spaces as well.When I added the newly calculated LMS matrix in addition to the Oklab matrix, it seems to throw things a little off (pushing blue further out of gamut as an example), so for now, I'll probably stick with just the improved Oklab matrix until I can verify the LMS matrix. Considering how much better the calculations are with just the OKLAB matrix, I think the LMS matrix at slightly lower accuracy wasn't the driving force in bad chroma calculations.
I do wonder if creating the matrices with rational numbers (which also I assume includes the white point) causes the calculation to be different as everywhere else in my code, the white point is applied with floating point math, which likely leads to a disparity in white points causing calculations to not be quite right. It's also very likely that the Okhsl/Okhsv algorithm just needs some more manual tuning at whatever extreme the new matrix puts it at. I'll look into this more once the work is posted.
Regardless, even with just the improved Oklab matrix, things are actually no worse than when including the LMS matrix. And great conversion back to sRGB:
jessestricker commentedon Oct 13, 2023
@JamesEggleton Did you find some time to publish your work yet? Looking forward to it! 🙂
Update LMS matrices
Update LMS matrices
nex3 commentedon Oct 2, 2024
What are considered the best set of XYZD65/LMS matrices to use now? The ones from #6642 (comment) or the ones from color-js/color.js#357?
facelessuser commentedon Oct 2, 2024
@nex3 The CSS spec has been updated with the ones from color.js, I would just follow the spec.
nex3 commentedon Oct 2, 2024
What's the reasoning behind the differences between the spec's values and @JamesEggleton's?
facelessuser commentedon Oct 2, 2024
There's no specific insight into exactly what was done with the James'. There was a claim to have a 64 bit version of the initial matrix of
But it was never posted. My personal interest was to see the approach and verify it. I had noticed an issue using Jame's matrices when applied to Okhsl as well, so I was hesitant to use them without being able to see the process and understand it more. I won't speculate as to exactly what the issue is, only that I had no further insight without being able to evaluate what was actually done. This, of course, does not specifically affect CSS as it does not currently use Okhsl, but it was a concern for me.
I make no claims on the correctness or incorrectness of what James provided. It is likely that there is absolutely nothing wrong with the approach and that Okhsl simply did not handle the 64 bit version of the XYZ_to_LMS portion of the algorithm.
With that out of the way, I can give details as to what is done in Color.js and provide a reproducible example of how it was generated.
The problem with the Oklab matrices as previously defined is that they simply do not resolve
oklab(1 0 0)to LMS[1, 1, 1]in 64 bit. This is due to how the 32 bit matrices are applied in a 64 bit data type environment, which Color.js uses. Because of this, as lightness increases, achromatic values foraandbdeviate further from zero. When trying to apply CSS logic for powerless values, a larger and larger threshold would need to be used as lightness increases.CSS simply took the LMS_to_Oklab matrix (the forward transform) and generated the 64 bit inverse matrix for the reverse transform. The problem is that the forward transform does not give you
oklab(1 0 0)== LMS[1, 1, 1]in a 64 bit environment.It turns out the 32 bit inverse matrix does support
oklab(1 0 0)== LMS[1, 1, 1], so if you use reverse transform to generate the forward transform for LMS_to_Oklab, you resolve the disparity for achromatic values. You can further emit the reverse transform as 64 bit before inverting the matrix which cleaned up the results a bit more. That is all that was needed and all that was done.These matrices also did not cause adverse effects when used in Okhsl.
You can recreate them yourself with this script: https://github.com/color-js/color.js/blob/main/scripts/oklab_matrix_maker.py. This makes the changes transparent.
nex3 commentedon Oct 3, 2024
Ah thanks, that's really helpful! I can re-run that with my own lossless numerics to get even more precise intermediate matrices.
facelessuser commentedon Oct 3, 2024
Sure! Having something reproducible makes such things possible.
Update LMS matrices (#2374)