Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css-color] Add OKLab, OKLCH #6642

Closed
svgeesus opened this issue Sep 20, 2021 · 46 comments
Closed

[css-color] Add OKLab, OKLCH #6642

svgeesus opened this issue Sep 20, 2021 · 46 comments

Comments

@svgeesus
Copy link
Contributor

svgeesus commented Sep 20, 2021

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

  1. 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
  2. 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.
  3. 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

  1. add oklab and oklch as defined color spaces which can be used in color-mix(). Depends on 3.
  2. add oklab() and oklch() to Relative Color Syntax. Depends on 3.

@argyleink @una @tabatkins @LeaVerou

@svgeesus svgeesus added css-color-4 Current Work Agenda+ F2F css-color-5 Color modification labels Sep 20, 2021
@svgeesus
Copy link
Contributor Author

svgeesus commented 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
Copy link
Contributor

argyleink commented Sep 20, 2021

lgtm, nice attention to detail

@facelessuser
Copy link

facelessuser commented 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
Copy link
Contributor Author

svgeesus commented 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
Copy link

facelessuser commented Sep 24, 2021

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.

@astearns astearns moved this from Second to Color in EUR Sep 29 2021 TPAC Meeting Sep 27, 2021
@tabatkins
Copy link
Member

tabatkins commented 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
Copy link
Contributor Author

svgeesus commented Sep 28, 2021

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.

@tabatkins
Copy link
Member

tabatkins commented 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
Copy link
Contributor

Crissov commented 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(). #4481

cie() = cie( /* Lab */ [<percentage> && <number>{2}] 
           | /* Luv */  <percentage>{3} 
           | /* LCH */ [<number>{2} && <angle>] 
           [ / <alpha-value> ]? 
           ) 

okl() = okl( /* Lab */ [<percentage> && <number>{2}] 
           | /* LCH */ [<number>{2} && <angle>] 
           [ / <alpha-value> ]? 
           ) 

@svgeesus
Copy link
Contributor Author

svgeesus commented Sep 29, 2021

we've accidentally set a precedent of all the specialized color functions have three-letter names

I don't think any precedent has been set.

@facelessuser
Copy link

facelessuser commented 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).

Screen Shot 2021-09-29 at 7 16 57 AM

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
Copy link
Contributor Author

svgeesus commented 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.

@facelessuser
Copy link

facelessuser commented Sep 29, 2021

Yep, let me throw something together publicly that you can play around with.

@tabatkins
Copy link
Member

tabatkins commented Sep 29, 2021

we've accidentally set a precedent of all the specialized color functions have three-letter names

I don't think any precedent has been set.

rgb(), hsl(), hwb(), lab(), lch(). I specifically said it was accidental, but it's still there, and people pattern-match. color() is the exception, and it is an exceptional function compared to the others. This doesn't mean we're forced into this, but it does, I think, imply that people will reach for the 3-latter lab() over oklab().

Regardless, tho, that wasn't my point. My point is just that when having two names that are variants of each other, one of which is longer than the other, people will naturally reach for the shorter one by default; this is a relatively standard principle of language-design UX. If we think that OKLab is better than Lab for authors, then we'll be doing them a disservice by having lab() and oklab() as the naming. Having lab() be OKLab, and letting people still access Lab with color(cielab, ...) would enable people to have access to both when needed, but give authors the more preferable functionality with the easier syntax.

@svgeesus
Copy link
Contributor Author

svgeesus commented Sep 29, 2021

I do think that rec2020 gamut mapping needs more investigation (as noted on the next steps slide) and in particular I want to do some rec2020 to display-p3 mapping (with swatches output in display-p3).

I notice that OKLab and CIE Lab have different lightness estimation for high-chroma blues.

Here is rec2020 blue with OKLCH chroma reduced to zero without clip and with clip on deltaEOK < 0.02. In both, the colors on the upper part are the sRGB color (if in gamut) or salmon (if in display-p3) or red (if outside display-p3) while the lower part shows the linear-light display-p3 component values.

I can easily put the same thing together where the upper part is display-p3 and the gamut clip is to the display-p3 gamut (but it will currently only display on Safari TP and BFO Publisher)

@svgeesus
Copy link
Contributor Author

svgeesus commented Sep 29, 2021

Having lab() be OKLab, and letting people still access Lab with color(cielab, ...) would enable people to have access to both when needed, but give authors the more preferable functionality with the easier syntax.

oof. That would be super confusing, surely.

However, renaming lab() to cielab() and lch() to cielch() would be doable, and more self-describing, and would make oklab() the shorter one. I guess. (I'm not a fan of bikeshedding churn, which hurts early adopters).

@facelessuser
Copy link

facelessuser commented Sep 29, 2021

So, here is a link to a working example with code. You can edit it and play around with it: https://facelessuser.github.io/coloraide/playground/?source=https%3A%2F%2Fgist.githubusercontent.com%2Ffacelessuser%2F26ff61052f1e4a180d2f98a499d08595%2Fraw%2F454c4a9911e626e537f184b281f51382e83025aa%2Fokfit.py

Basically, I am just interpolating between black and blue in the rec2020 space. Then I convert it to sRGB and fit it in that space and display it directly.

I scale the ∆Eok to by 100 as the difference between black and white is ~0 - 1 and ∆E2000 is ~0-100. I've done it without the scaling, adjusting the limits and such, and the results are basically the same. It seems green starts getting mixed in as chroma reduces.

Again. Maybe there are some assumptions here that just aren't right. This is the exact same algorithm used for the CIELCH fitting.

@LeaVerou
Copy link
Member

LeaVerou commented Oct 6, 2021

I have been debating this with myself for a good while, hence the lack of comment.

I did watch @svgeesus' talk (and I really recommend watching it to anyone who wants to have an opinion on this), and I do agree there are significant advantages in Oklab over Lab. However, Lab has been established for 45 years. It has a ton of tooling around it. It has drawbacks, yes, but those are widely studied. Same as all the color formats currently in CSS, they all have drawbacks, but are all old and established, and their drawbacks studied and known. Oklab on the other hand is very new. It has not yet been fully explored, and it has almost no tooling that supports it. Its advantages are known, but its drawbacks are still being researched. It seems a little premature to add it to CSS, especially as a named function. I’m not saying we shouldn't, but it gives me pause.

As a secondary point, we need to decide when we add things as a named function and when as a color() keyword. Right now we have both, and some color spaces can only be expressed via color() and others via named functions. From an author point of view, the distinction is unclear. The spec lists color() as "Profiled, device dependent colors". Does that mean that any color space we add with a restricted gamut is under color() and anything else is not?

I disagree that authors will always reach for the shortest syntax. Firstly, a lot of the time colors are reused via variables, not re-specified. Secondly, if that were true, #RGB would be the most popular color syntax, which is not the case.

One issue with Oklab is that it uses different coordinates than Lab, namely 0-1 ranges instead of 0-100. If we do add such a syntax in CSS, I wonder if there is value in trying to harmonize the two a little.

@tabatkins
Copy link
Member

tabatkins commented Oct 6, 2021

Secondly, if that were true, #RGB would be the most popular color syntax, which is not the case.

I made my point pithily in the middle of a longer argument, but to be more specific: given two seemingly identical or very similar pieces of functionality, people will tend to reach for the one with shorter name/syntax, both because it's easier, and because (partially due to this effect) API designers usually give the preferred solution a shorter name. Hex notation, especially 3-char hex, is substantially different in both abilities and syntax to any other color function, so it doesn't necessarily fit into this. On the other hand, lab() and oklab() functions, which take essentially identical arguments and output approximately identical colors, are exactly the pattern I'm talking about.

(Also who would choose "oklab" over "lab"? If it was betterlab(), sure, but just ok? Pass. ^_^)

(Also, for the generic case of "I need a quick color and I know very roughly what it should be but don't care about the details right now", I personally do reach for 3-char hex first precisely because it is the shortest color syntax that satisfies that use-case.)

One issue with Oklab is that it uses different coordinates than Lab, namely 0-1 ranges instead of 0-100. If we do add such a syntax in CSS, I wonder if there is value in trying to harmonize the two a little.

Our lab() doesn't use a 0-100 syntax anyway, it uses 0%-100%. I don't see why we wouldn't use exactly the same thing here. Whoops I misremembered. Well anyway, yeah, if we did add an oklab() I'd match the coordinate ranges; whether a given paper uses 0-100, 0-1, or 0%-100% is completely arbitrary and up to the whims of the author, but CSS needs to be more predictable than that.

@LeaVerou
Copy link
Member

LeaVerou commented Oct 6, 2021

@svgeesus made the point that we need OkLab to be able to specify a reasonable gamut mapping algorithm in Color 4, and as an interpolation space for gradients etc, so it doesn't actually matter whether authors use it, because we can still use it as an interpolation and gamut mapping space.

For me that tips the scales towards yes, let's add this. My question regarding named functions vs color() still stands.

@svgeesus
Copy link
Contributor Author

svgeesus commented Oct 15, 2021

These edits have now done everything agreed, with the exception of Define gamut mapping which has it's own issue

@svgeesus
Copy link
Contributor Author

svgeesus commented Oct 16, 2021

In short, using the XYZ matrix as specified by the Oklab article assumes the white point that he used, but CSS uses a different white point, so this introduces noise. If the conversion was done from Linear sRGB, this noise is reduced.

Thanks! I tested it out and got much better results on neutrals (zeroes to 8dp).

Updated sample code (and also color.js)

@facelessuser
Copy link

facelessuser commented Oct 16, 2021

Sounds good, glad I could help!

@bottosson
Copy link

bottosson commented Oct 17, 2021

Exciting stuff! This would certainly increase the reach of Oklab significantly. Let me know if I can be of assistance in any way.

One thing to discuss is that I've been considering making a second revision of Oklab for a few reasons. The things I'm considering addressing are:

  1. Switching D65 whitepoint variant to the four-digit x,y D65 whitepoint.
  2. Changing the definition of the M1 matrix so that the whitepoint is exact and not dependent on the rounding of the M1 matrix, instead limiting accuracy only by the precision of the computations.
  3. Adjust the scaling of a&b to more accurately predict color distances. By more rigorously deriving the scale higher accuracy of color distance predictions could be achieved. This wouldn't affect interpolation, hue predictions and lightness predictions, just the scaling of a,b and C.
  1. and 2) I am fairly convinced are good ideas, that only minimally change Oklab and correct minor issues in the current definition. 3) I am less certain about, since it would make the resulting Oklab values incompatible and it has already seen somewhat widespread adoption. On the other hand it is mostly used for doing computations, not for communicating colors between applications, so the impact is a bit limited at least.

Do you have any thoughts around this? This would end up being the most significant usage of Oklab I think, so it makes sense to make sure it is fit for purpose!

Thanks!

@facelessuser
Copy link

facelessuser commented Oct 17, 2021

@bottosson

  • Switching D65 whitepoint variant to the four-digit x,y D65 whitepoint.
  • Changing the definition of the M1 matrix so that the whitepoint is exact and not dependent on the rounding of the M1 matrix, instead limiting accuracy only by the precision of the computations.

I believe the most recent M1 matrix in the CSS spec no longer uses the M1 matrix from your article, but now uses a recalculated M1 based on the XYZ to Linear sRGB transfer function which is based on the 4 digit whitepoint now. Or are you referring to some other aspect of the M1 calculation that also should be changed?

@svgeesus
Copy link
Contributor Author

svgeesus commented Oct 17, 2021

@bottosson wrote

Switching D65 whitepoint variant to the four-digit x,y D65 whitepoint.

I must admit that I already did this, in the sample code which is part of CSS Color 4. I had been troubled by the neutral colors non-zero Chroma in OKLCH, which was worst at white and thus likely due to whitepoint differences. It seems you had used the ASTM XYZ values, to 6 significant figures; while for the various predefined RGB spaces I was getting the best results with the 4-digit x,y values.

@facelessuser posted in this thread explaining how to derive a new M1 based on your linear-srgb to LMS C++ code and my (well, the sRGB standard) XYZ to sRGB, so that is what I used and it means roundtripping is now excellent.

Changing the definition of the M1 matrix so that the whitepoint is exact and not dependent on the rounding of the M1 matrix, instead limiting accuracy only by the precision of the computations.

Yes in general, rounding is a consistent source of problems and they compound.

Adjust the scaling of a&b to more accurately predict color distances. By more rigorously deriving the scale higher accuracy of color distance predictions could be achieved. This wouldn't affect interpolation, hue predictions and lightness predictions, just the scaling of a,b and C.

That would be a breaking change but I would be interested to know more.

@bottosson
Copy link

bottosson commented Oct 18, 2021

Yeah, exactly.

Definitely makes sense that that solution works, although would be good to define the matrix in a way that is independent of sRGB.

I think a good way to define M1 would be along these lines (here in python and numpy, but it is just matrix operations):

# The rows have been scaled to sum to 1, and contains no whitepoint information.
M0 = np.array([
  [  0.77849780,  0.34399940,  -0.12249720],
  [  0.03303601,  0.93076195,   0.03620204],
  [  0.05092917,  0.27933344,   0.66973739]
])

d65_xyz = np.array([0.3127, 0.3290, 1-0.3127-0.3290])
d65_XYZ = d65_xyz/d65_xyz[1]

# Calculate M1 by scaling scale each row if M0 so that d65_XYZ transforms exactly to 1,1,1
M1 = M0 / np.outer(M0.dot(d65_XYZ), np.ones(3)) 

This way the accuracy of white mapping to zero chroma is only dependent on the accuracy of the computations, not the rounding of the matrices.

Regarding the scaling of a&b:

Yes, this would certainly be a breaking change, so I am a bit hesitant about it as well. On the other hand the potential improvement is pretty large.

I unfortunately didn't spend that much time calculating and validating that scaling factor when I first derived Oklab since I was mostly focused on the orthogonality between L, C and h (and I didn't expect it to become so widespread so quickly), and it seems like it is off by quite a bit.

I've recently done some tests with color distance datasets as implemented in Colorio
and on both the Combvd dataset and the OSA-UCS dataset a scale factor of slightly more than 2 for a and b would give the best results (2.016 works best for Combvd and 2.045 for the OSA-UCS dataset).

@svgeesus
Copy link
Contributor Author

svgeesus commented Oct 18, 2021

(re-opening so that more people will notice this valuable discussion)

@svgeesus svgeesus reopened this Oct 18, 2021
@svgeesus
Copy link
Contributor Author

svgeesus commented Oct 18, 2021

For the scaling of a & b , this statement in the OKlab defining article did give me slight pause:

The a and b plane is scaled so that around 50% gray the ratio of color differences along the lightness axis and the a and b plane is the same as the ratio for color differences predicted by CIEDE2000

because the formula for deltaE2000 introduces a mean-chroma-dependent asymmetry between a and b, strongest right on the neutral axis (recalculated a is 1.5 times b) then fading off to below 1.1 at chroma 27 and below 1.01 at chroma 40.

// calculate a-axis asymmetry factor from mean Chroma
// this turns JND ellipses for near-neutral colors back into circles
let C7 = Math.pow(Cbar, 7);
const Gfactor = Math.pow(25, 7);
let G = 0.5 * (1 - Math.sqrt(C7/(C7+Gfactor)));
// scale a axes by asymmetry factor
// this by the way is why there is no Lab2000 color space
let adash1 = (1 + G) * a1;
let adash2 = (1 + G) * a2;
// calculate new Chroma from scaled a and original b axes
let Cdash1 = Math.sqrt(adash1 ** 2 + b1 ** 2);
let Cdash2 = Math.sqrt(adash2 ** 2 + b2 ** 2);

(For the curious, the values of Cbar, G, and adash are tabulated here)

This means that:

  • scaling OKLab a & b relative to L solely based on deltaE 2000 comparisons on the neutral axis picks the point of maximum CIELAB a & b asymmetry
  • scaling based on some dataset of color pairs will depend on the distribution of mean chroma in that data set

I was mostly focused on the orthogonality between L, C and h

Which is a very useful property of OKLab, for gamut mapping and gradient generation

@bottosson
Copy link

bottosson commented Oct 18, 2021

The a and b plane is scaled so that around 50% gray the ratio of color differences along the lightness axis and the a and b plane is the same as the ratio for color differences predicted by CIEDE2000

To be clear, this statement captures my intent, but I think it isn't quite how Oklab as published ended up being. I've only recently realized this as I started to look more closely at color distance predictions.

scaling based on some dataset of color pairs will depend on the distribution of mean chroma in that data set

This is in particularly true for small color distance datasets such as Combvd. For large scale distances this is less true, since there is less of a chroma compression effect.

More visually:

Small scale color distances show very strong elongation of spheres into ellipses:
image

OSA-UCS dataset models larger color distances and has much less chroma compression.
image

(It is also possible to much closer match the small color difference dataset as well, by introducing chroma compression, similar to what both CIEDE2000 and Ciecam02/16-UCS do, but that also adds quite a bit of complexity and assumptions on viewing conditions, this performs worse for tasks such as color interpolation)

@facelessuser
Copy link

facelessuser commented Oct 18, 2021

The new matrix calculation really is much closer when calculating the transform for white. This is super useful to know. It is also fairly easy to get the sRGB Linear -> LMS calculation (for anyone who needs it to calculate Okhsl and Okhsv), you just need to take your higher precision XYZ D65 to LMS matrix and dot it with your RGB to XYZ transform so you are left with the RGB to LMS matrix.

@svgeesus
Copy link
Contributor Author

svgeesus commented Nov 22, 2021

Closing again as the edits have been made and discussion seems to have died down.

webkit-commit-queue pushed a commit to WebKit/WebKit that referenced this issue Nov 28, 2021
https://bugs.webkit.org/show_bug.cgi?id=233507

Reviewed by Cameron McCormack.

LayoutTests/imported/w3c:

Add new tests for oklab() and oklch() based on the existing lab()
and lch() tests.

* web-platform-tests/css/css-color/oklab-001-expected.html: Added.
* web-platform-tests/css/css-color/oklab-001.html: Added.
* web-platform-tests/css/css-color/oklab-002-expected.html: Added.
* web-platform-tests/css/css-color/oklab-002.html: Added.
* web-platform-tests/css/css-color/oklab-003-expected.html: Added.
* web-platform-tests/css/css-color/oklab-003.html: Added.
* web-platform-tests/css/css-color/oklab-004-expected.html: Added.
* web-platform-tests/css/css-color/oklab-004.html: Added.
* web-platform-tests/css/css-color/oklab-005-expected.html: Added.
* web-platform-tests/css/css-color/oklab-005.html: Added.
* web-platform-tests/css/css-color/oklab-006-expected.html: Added.
* web-platform-tests/css/css-color/oklab-006.html: Added.
* web-platform-tests/css/css-color/oklab-007-expected.html: Added.
* web-platform-tests/css/css-color/oklab-007.html: Added.
* web-platform-tests/css/css-color/oklab-008-expected.html: Added.
* web-platform-tests/css/css-color/oklab-008.html: Added.
* web-platform-tests/css/css-color/oklch-001-expected.html: Added.
* web-platform-tests/css/css-color/oklch-001.html: Added.
* web-platform-tests/css/css-color/oklch-002-expected.html: Added.
* web-platform-tests/css/css-color/oklch-002.html: Added.
* web-platform-tests/css/css-color/oklch-003-expected.html: Added.
* web-platform-tests/css/css-color/oklch-003.html: Added.
* web-platform-tests/css/css-color/oklch-004-expected.html: Added.
* web-platform-tests/css/css-color/oklch-004.html: Added.
* web-platform-tests/css/css-color/oklch-005-expected.html: Added.
* web-platform-tests/css/css-color/oklch-005.html: Added.
* web-platform-tests/css/css-color/oklch-006-expected.html: Added.
* web-platform-tests/css/css-color/oklch-006.html: Added.
* web-platform-tests/css/css-color/oklch-007-expected.html: Added.
* web-platform-tests/css/css-color/oklch-007.html: Added.
* web-platform-tests/css/css-color/oklch-008-expected.html: Added.
* web-platform-tests/css/css-color/oklch-008.html: Added.

Source/WebCore:

Tests: imported/w3c/web-platform-tests/css/css-color/oklab-001.html
       imported/w3c/web-platform-tests/css/css-color/oklab-002.html
       imported/w3c/web-platform-tests/css/css-color/oklab-003.html
       imported/w3c/web-platform-tests/css/css-color/oklab-004.html
       imported/w3c/web-platform-tests/css/css-color/oklab-005.html
       imported/w3c/web-platform-tests/css/css-color/oklab-006.html
       imported/w3c/web-platform-tests/css/css-color/oklab-007.html
       imported/w3c/web-platform-tests/css/css-color/oklab-008.html
       imported/w3c/web-platform-tests/css/css-color/oklch-001.html
       imported/w3c/web-platform-tests/css/css-color/oklch-002.html
       imported/w3c/web-platform-tests/css/css-color/oklch-003.html
       imported/w3c/web-platform-tests/css/css-color/oklch-004.html
       imported/w3c/web-platform-tests/css/css-color/oklch-005.html
       imported/w3c/web-platform-tests/css/css-color/oklch-006.html
       imported/w3c/web-platform-tests/css/css-color/oklch-007.html
       imported/w3c/web-platform-tests/css/css-color/oklch-008.html

Adds support for oklab() and oklch() CSS colors and as interpolation
parameters for color-mix().

OKLab (and its polar form OKLCH) is a relatively new Lab-like colorspace that aims
to be an improved (improved hue linearity, hue uniformity, and chroma uniformity)
Lab. It was create by Björn Ottosson and is documented at https://bottosson.github.io/posts/oklab/.

* css/CSSValueKeywords.in:
Add 'oklab' and 'oklch' to the keyword list so they can be used as function
identifiers. Remove old mention of 'lab' in the color() function section,
since 'lab' is no longer a valid colorspace to use in the color() function
(rather, only lab() is supported).

* css/parser/CSSPropertyParserHelpers.cpp:
(WebCore::CSSPropertyParserHelpers::parseLabParameters):
(WebCore::CSSPropertyParserHelpers::parseRelativeLabParameters):
(WebCore::CSSPropertyParserHelpers::parseNonRelativeLabParameters):
(WebCore::CSSPropertyParserHelpers::parseLCHParameters):
(WebCore::CSSPropertyParserHelpers::parseRelativeLCHParameters):
(WebCore::CSSPropertyParserHelpers::parseNonRelativeLCHParameters):
(WebCore::CSSPropertyParserHelpers::parseColorFunction):
Generalize lab and lch function parsing to also support the oklab and
oklch variants (they have the same parsing rules).

(WebCore::CSSPropertyParserHelpers::consumeColorMixColorSpaceAndComma):
(WebCore::CSSPropertyParserHelpers::mixColorComponents):
Add support for using oklab and oklch as the interpolation space of a color-mix().
This was already generalized so all it meant doing was adding mappings of the
new identifiers to enums and mixColorComponentsInColorSpace calls.

* platform/graphics/ColorComponents.h:
(WebCore::ColorComponents::subset const):
Fix compile error (no one had used subset yet it seems). 'std::remove_const_t<decltype(T::Size)>'
was likely copied from mapColorComponents() where it is templatized and needs to deduce the loop
variable, but that is not needed here.

* platform/graphics/ColorConversion.cpp:
(WebCore::convertToPolarForm):
(WebCore::convertToRectangularForm):
Move conversion to/from polar/rectangular forms from the LCHA conversion
code here, so that it can be reused for OKLCHA.

(WebCore::OKLab<float>>::convert):
Add support for converting OKLab to/from XYZ D65. Matrix values come from https://bottosson.github.io/posts/oklab/
with updates from w3c/csswg-drafts#6642 (comment)

(WebCore::OKLCHA<float>>::convert):
Add support for converting OKLCHA. This is identical to the LCHA code above.

(WebCore::converColorComponents):
Add cases for new colorspaces.

* platform/graphics/ColorConversion.h:
Add converters for new colorspaces. Update diagram with them as well.

* platform/graphics/ColorMatrix.h:
(WebCore::ColorMatrix::transformedColorComponents const):
Generalize transformedColorComponents to work with any size ColorComponents object. This allows
the OKLab conversion code to be a bit simpler as it can operate on just the non-alpha components
in a more systematic way.

* platform/graphics/ColorModels.h:
Add new predicate template variables to help when needing to check what model a particular
color type uses.

* platform/graphics/ColorSerialization.cpp:
(WebCore::serialization):
(WebCore::serializationForCSS):
(WebCore::serializationForHTML):
(WebCore::serializationForRenderTreeAsText):
Add serialization support for new colorspaces. Also removes unused support for serializing lab
colors using the color(lab ...) syntax which has not been supported for some time.

* platform/graphics/ColorSpace.cpp:
* platform/graphics/ColorSpace.h:
* platform/graphics/cg/ColorSpaceCG.h:
Add OKLab and OKLCH to the list of enumerated colorspaces and add mappings to their
newly defined types OKLab<T> and OKLCHA<T>.

* platform/graphics/ColorTypes.h:
(WebCore::OKLab::OKLab):
(WebCore::OKLCHA::OKLCHA):
Add new types OKLab<T> and OKLCHA<T> (it looks like at some point an earlier version of this
must have partially landed as there were existing forward declarations). Like Lab<T> and LCHA<T>,
these new types use the LabModel<T> and LCHModel<T> models, but unlike them they use a whitepoint
of D65.

* platform/graphics/ColorUtilities.h:
Generalize isBlack and isWhite to have a variant that works with Lab, LCH, OKLab and OKLCH (as they
all are identical) using SFINAE, use the new model predicates to make this more clear.

LayoutTests:

Update existing tests for lab() and lch() to also test oklab() and oklch().
As they have the same parsing rules, this is mostly done by templatizing
the tests and running them in a loop.

* fast/css/parsing-color-mix-expected.txt:
* fast/css/parsing-color-mix.html:
* fast/css/parsing-lab-colors-expected.txt:
* fast/css/parsing-lab-colors.html:
* fast/css/parsing-relative-color-syntax-expected.txt:
* fast/css/parsing-relative-color-syntax.html:



Canonical link: https://commits.webkit.org/244573@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@286191 268f45cc-cd09-0410-ab3c-d52691b4dbfc
annulen pushed a commit to qtwebkit/qtwebkit that referenced this issue Nov 28, 2021
https://bugs.webkit.org/show_bug.cgi?id=233507

Reviewed by Cameron McCormack.

LayoutTests/imported/w3c:

Add new tests for oklab() and oklch() based on the existing lab()
and lch() tests.

* web-platform-tests/css/css-color/oklab-001-expected.html: Added.
* web-platform-tests/css/css-color/oklab-001.html: Added.
* web-platform-tests/css/css-color/oklab-002-expected.html: Added.
* web-platform-tests/css/css-color/oklab-002.html: Added.
* web-platform-tests/css/css-color/oklab-003-expected.html: Added.
* web-platform-tests/css/css-color/oklab-003.html: Added.
* web-platform-tests/css/css-color/oklab-004-expected.html: Added.
* web-platform-tests/css/css-color/oklab-004.html: Added.
* web-platform-tests/css/css-color/oklab-005-expected.html: Added.
* web-platform-tests/css/css-color/oklab-005.html: Added.
* web-platform-tests/css/css-color/oklab-006-expected.html: Added.
* web-platform-tests/css/css-color/oklab-006.html: Added.
* web-platform-tests/css/css-color/oklab-007-expected.html: Added.
* web-platform-tests/css/css-color/oklab-007.html: Added.
* web-platform-tests/css/css-color/oklab-008-expected.html: Added.
* web-platform-tests/css/css-color/oklab-008.html: Added.
* web-platform-tests/css/css-color/oklch-001-expected.html: Added.
* web-platform-tests/css/css-color/oklch-001.html: Added.
* web-platform-tests/css/css-color/oklch-002-expected.html: Added.
* web-platform-tests/css/css-color/oklch-002.html: Added.
* web-platform-tests/css/css-color/oklch-003-expected.html: Added.
* web-platform-tests/css/css-color/oklch-003.html: Added.
* web-platform-tests/css/css-color/oklch-004-expected.html: Added.
* web-platform-tests/css/css-color/oklch-004.html: Added.
* web-platform-tests/css/css-color/oklch-005-expected.html: Added.
* web-platform-tests/css/css-color/oklch-005.html: Added.
* web-platform-tests/css/css-color/oklch-006-expected.html: Added.
* web-platform-tests/css/css-color/oklch-006.html: Added.
* web-platform-tests/css/css-color/oklch-007-expected.html: Added.
* web-platform-tests/css/css-color/oklch-007.html: Added.
* web-platform-tests/css/css-color/oklch-008-expected.html: Added.
* web-platform-tests/css/css-color/oklch-008.html: Added.

Source/WebCore:

Tests: imported/w3c/web-platform-tests/css/css-color/oklab-001.html
       imported/w3c/web-platform-tests/css/css-color/oklab-002.html
       imported/w3c/web-platform-tests/css/css-color/oklab-003.html
       imported/w3c/web-platform-tests/css/css-color/oklab-004.html
       imported/w3c/web-platform-tests/css/css-color/oklab-005.html
       imported/w3c/web-platform-tests/css/css-color/oklab-006.html
       imported/w3c/web-platform-tests/css/css-color/oklab-007.html
       imported/w3c/web-platform-tests/css/css-color/oklab-008.html
       imported/w3c/web-platform-tests/css/css-color/oklch-001.html
       imported/w3c/web-platform-tests/css/css-color/oklch-002.html
       imported/w3c/web-platform-tests/css/css-color/oklch-003.html
       imported/w3c/web-platform-tests/css/css-color/oklch-004.html
       imported/w3c/web-platform-tests/css/css-color/oklch-005.html
       imported/w3c/web-platform-tests/css/css-color/oklch-006.html
       imported/w3c/web-platform-tests/css/css-color/oklch-007.html
       imported/w3c/web-platform-tests/css/css-color/oklch-008.html

Adds support for oklab() and oklch() CSS colors and as interpolation
parameters for color-mix().

OKLab (and its polar form OKLCH) is a relatively new Lab-like colorspace that aims
to be an improved (improved hue linearity, hue uniformity, and chroma uniformity)
Lab. It was create by Björn Ottosson and is documented at https://bottosson.github.io/posts/oklab/.

* css/CSSValueKeywords.in:
Add 'oklab' and 'oklch' to the keyword list so they can be used as function
identifiers. Remove old mention of 'lab' in the color() function section,
since 'lab' is no longer a valid colorspace to use in the color() function
(rather, only lab() is supported).

* css/parser/CSSPropertyParserHelpers.cpp:
(WebCore::CSSPropertyParserHelpers::parseLabParameters):
(WebCore::CSSPropertyParserHelpers::parseRelativeLabParameters):
(WebCore::CSSPropertyParserHelpers::parseNonRelativeLabParameters):
(WebCore::CSSPropertyParserHelpers::parseLCHParameters):
(WebCore::CSSPropertyParserHelpers::parseRelativeLCHParameters):
(WebCore::CSSPropertyParserHelpers::parseNonRelativeLCHParameters):
(WebCore::CSSPropertyParserHelpers::parseColorFunction):
Generalize lab and lch function parsing to also support the oklab and
oklch variants (they have the same parsing rules).

(WebCore::CSSPropertyParserHelpers::consumeColorMixColorSpaceAndComma):
(WebCore::CSSPropertyParserHelpers::mixColorComponents):
Add support for using oklab and oklch as the interpolation space of a color-mix().
This was already generalized so all it meant doing was adding mappings of the
new identifiers to enums and mixColorComponentsInColorSpace calls.

* platform/graphics/ColorComponents.h:
(WebCore::ColorComponents::subset const):
Fix compile error (no one had used subset yet it seems). 'std::remove_const_t<decltype(T::Size)>'
was likely copied from mapColorComponents() where it is templatized and needs to deduce the loop
variable, but that is not needed here.

* platform/graphics/ColorConversion.cpp:
(WebCore::convertToPolarForm):
(WebCore::convertToRectangularForm):
Move conversion to/from polar/rectangular forms from the LCHA conversion
code here, so that it can be reused for OKLCHA.

(WebCore::OKLab<float>>::convert):
Add support for converting OKLab to/from XYZ D65. Matrix values come from https://bottosson.github.io/posts/oklab/
with updates from w3c/csswg-drafts#6642 (comment)

(WebCore::OKLCHA<float>>::convert):
Add support for converting OKLCHA. This is identical to the LCHA code above.

(WebCore::converColorComponents):
Add cases for new colorspaces.

* platform/graphics/ColorConversion.h:
Add converters for new colorspaces. Update diagram with them as well.

* platform/graphics/ColorMatrix.h:
(WebCore::ColorMatrix::transformedColorComponents const):
Generalize transformedColorComponents to work with any size ColorComponents object. This allows
the OKLab conversion code to be a bit simpler as it can operate on just the non-alpha components
in a more systematic way.

* platform/graphics/ColorModels.h:
Add new predicate template variables to help when needing to check what model a particular
color type uses.

* platform/graphics/ColorSerialization.cpp:
(WebCore::serialization):
(WebCore::serializationForCSS):
(WebCore::serializationForHTML):
(WebCore::serializationForRenderTreeAsText):
Add serialization support for new colorspaces. Also removes unused support for serializing lab
colors using the color(lab ...) syntax which has not been supported for some time.

* platform/graphics/ColorSpace.cpp:
* platform/graphics/ColorSpace.h:
* platform/graphics/cg/ColorSpaceCG.h:
Add OKLab and OKLCH to the list of enumerated colorspaces and add mappings to their
newly defined types OKLab<T> and OKLCHA<T>.

* platform/graphics/ColorTypes.h:
(WebCore::OKLab::OKLab):
(WebCore::OKLCHA::OKLCHA):
Add new types OKLab<T> and OKLCHA<T> (it looks like at some point an earlier version of this
must have partially landed as there were existing forward declarations). Like Lab<T> and LCHA<T>,
these new types use the LabModel<T> and LCHModel<T> models, but unlike them they use a whitepoint
of D65.

* platform/graphics/ColorUtilities.h:
Generalize isBlack and isWhite to have a variant that works with Lab, LCH, OKLab and OKLCH (as they
all are identical) using SFINAE, use the new model predicates to make this more clear.

LayoutTests:

Update existing tests for lab() and lch() to also test oklab() and oklch().
As they have the same parsing rules, this is mostly done by templatizing
the tests and running them in a loop.

* fast/css/parsing-color-mix-expected.txt:
* fast/css/parsing-color-mix.html:
* fast/css/parsing-lab-colors-expected.txt:
* fast/css/parsing-lab-colors.html:
* fast/css/parsing-relative-color-syntax-expected.txt:
* fast/css/parsing-relative-color-syntax.html:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@286191 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

No branches or pull requests