Skip to content

[css-color-4] Channel clipping breaks author expectations, especially when using 'perceptually uniform' spaces #9449

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

Open
mirisuzanne opened this issue Oct 9, 2023 · 211 comments
Labels
css-color-4 Current Work

Comments

@mirisuzanne
Copy link
Contributor

mirisuzanne commented Oct 9, 2023

This is not an issue in the css-color-4 spec, but in all the implementations. While issues have been filed on the individual bug trackers, I wanted to raise the issue with the CSSWG since it seems like this was an intentional decision agreed to by the browser vendors.

Here are the individual bug reports:

And, as I understand, the decision was made in these CSSWG issues:

I'm opening a separate issue because I don't have strong feelings about all the details of a gamut mapping algorithm, but I'm pretty frustrated about the state of what browsers shipped here, and I think we need to do something to fix it asap. From an authoring perspective it's entirely unusable, and it breaks the fundamental promise of the format: providing perceptually-uniform lightness.

  • Here's a codepen demo showing two colors with the same hue and lightness values, but vastly different perceptual lightness in the results.
  • Here's a tool for comparing gamut-mapping options - set the lightness low (eg 0.25) and clip gives colors which are over-saturated/too light, set the lightness high (eg .85) and clip gives over-saturated and too dark.

This is the format that authors were most excited about, and it doesn't do what we told them it does. I really wish this feature hadn't shipped at all, since it clearly wasn't ready to ship. Adding agenda+ because I think this deserves more eyes on it, and more urgency in fixing it.

@romainmenke
Copy link
Member

I really wish this feature hadn't shipped at all, since it clearly wasn't ready to ship.

An important aspect is that there is no feature detection for gamut mapping.
Authors can't write a supports query that will make it possible to progressively use (extremely) wide gamut colors safely in browser versions that do not support gamut mapping.

@facelessuser
Copy link

It's probably important to note that there likely isn't a perfect gamut mapping approach, each will probably have some quirks and can be useful if their limits are understood, but anything is better than clipping, which currently is what all browsers do.

OkLCh generally does well for gamut mapping when colors are within the model's ideal range. OkLCh seems to do decent up through rec2020 as the color space maintains a reasonable geometric shape. It also avoids the purple shift that occurs when gamut mapping with LCh.

rec200_in_oklch
https://facelessuser.github.io/coloraide/demos/3d_models.html?space=oklch&gamut=rec2020&edges=false&aspect=false&ortho=false

But we can see that the geometry of the OkLCh space becomes quite distorted for a space like ProPhoto RGB which extends past the visible gamut. This distortion helps contribute to issues like #7071.

prophoto_in_oklch
https://facelessuser.github.io/coloraide/demos/3d_models.html?space=oklch&gamut=prophoto-rgb&edges=false&aspect=false&ortho=false

Gamut mapping with LCh has its own issues, purple shift in the blue range as an example, but the space does hold its shape much better with extreme gamuts allowing for more consistent mapping, but still, some corner cases exists, like with bright yellows due to the geometry in that hue region.

prophoto_in_lch

https://facelessuser.github.io/coloraide/demos/3d_models.html?space=lch&gamut=prophoto-rgb&edges=false&aspect=false&ortho=false

Clipping is still probably worse than either of these options:

Screenshot 2023-10-09 at 6 08 20 PM

@jamesnw
Copy link

jamesnw commented Oct 11, 2023

The same perceptual lightness shift is also present in (ok)lab. Here's a Codepen demo showing (ok)lab and (ok)lch with consistent lightness values, and changing only the a or chroma channels, respectively.

Ok(lab) bug reports-

Both these browser bug reports and in the issue description itself also talk about the separate but connected issue around powerless components. No browsers have implemented this portion of the spec (which is also present on LCH, Oklab, and Oklch specs).

If the lightness of a Lab color is 0%, or 100% both the a and b components are [powerless](https://www.w3.org/TR/css-color-4/#powerless-color-component) and the color represents black, or white, respectively.

These all should be black and white-

There is also related conversation here- #8794

@svgeesus
Copy link
Contributor

I'm pretty frustrated about the state of what browsers shipped here, and I think we need to do something to fix it asap. From an authoring perspective it's entirely unusable, and it breaks the fundamental promise of the format: providing perceptually-uniform lightness.

I agree. After all the work that went into finding a good Gamut Mapping Algorithm that was hue-preserving, lightness-preserving, and thus allowed the closest approximation to a specified color that was out of gamut of the display device, we end up with naive clip shipping in browsers which gives massive hue shifts and even bigger lightness shifts.

And this was done out of a misguided attempt to make 2D Canvas (which is drawing millions of pixels) align with displayed images (which will be using a perceptual gamut mapping, to preserve overall look and image detail) and with CSS (where you have maybe a hundred or so colors in all the stylesheets on a page). Trading off authoring complexity and frustration for minimal gains in computing efficiency of the implementation,

Here is an example: the CSS Color 4 GMA with Oklch on the left, the (old) CSS Color 4 GMA with CIE LCH and DeltaE2000 on the right, and in the middle naive clip which, as cal clearly be seen, for these light colors gives a much darker result quite unlike the requested color.

oklab-gma-clip

Its a screen shot from this demo with OK lightness set to 0.95.

Which is why we see preprocessor plugings like this which take your CSS and auto-generate sRGB fallbacks (using the CSS Color 4 GMA)

Browsers don't support this part of CSS Color 4 yet.
So if you want to have correct colors on all displays you should include both narrow and wide gamut color values. This new plugin helps you to do just that.https://t.co/FyYQOElo90 pic.twitter.com/JSMD2ddEzT

— CSS Tools (@css_tools_) October 9, 2023

image

@svgeesus
Copy link
Contributor

Here is that PostCSS GMA plugin btw

@svgeesus
Copy link
Contributor

svgeesus commented Oct 11, 2023

If the lightness of a Lab color is 0%, or 100% both the a and b components are powerless and the color represents black, or white, respectively.

That portion of the spec has changed because of

it now says:

If the lightness of a Lab color (after clamping) is 0%, or 100% the color will be displayed as black, or white, respectively due to gamut mapping to the display.

which is more correct - the specified color does have chroma, but because of the lightness it will be out of gamut of any SDR display (where the brightest color that can be displayed is media white).

@jamesnw
Copy link

jamesnw commented Oct 12, 2023

Sorry- I was looking at an outdated version of the spec.

I see that L=0 is black and L=1 is white is now covered in the CSS Gamut Mapping to an RGB Destination section-

For colors which are out of range on the Lightness axis, white is returned in the destination color space if the Lightness is greater than or equal to 1.0, while black is returned in the destination color space if the Lightness is less than or equal to 0.0.

@svgeesus
Copy link
Contributor

Sorry- I was looking at an outdated version of the spec.

No problem, we should update the official TR version more often (I keep meaning to but then there is always more to do). But the Editor's Draft is the right place to look for the latest version.

@svgeesus
Copy link
Contributor

@jamesnw wrote:

Here's a Codepen demo showing (ok)lab and (ok)lch with consistent lightness values, and changing only the a or chroma channels, respectively.

So that demo has oklab(90% 0.36 0) which is out of gamut for all RGB colorspaces (even prophoto-rgb) and is rgb(152.937% 10.3745% 83.2625%).

Because Chrome and Firefox do naive clipping, that becomes rgb(100% 10.3745% 83.2625%) which is a much darker fuchsia and is oklch(0.6836 0.29009 338.36). Lightness changed from 90% to 68% because of the clip; oklab(90% 0.36 0) is oklch(0.9 0.36 0) so we also see the hue shifted 21.64 degrees because of the clip.

A CSS gamut mapped version of oklab(90% 0.36 0) to the sRGB gamut is rgb(100% 73.3771% 82.2121%) and taking that back to oklch is oklch(0.861 0.08294 357.323). We still have a lightness shift (to avoid excessive chroma loss), but less so: 90% became 86.1% and a small hue shift too, 2.677 degrees. Much better than the naive clip though.

On a P3 screen, we start from color(display-p3 1.40598 0.3464 0.8253) which CSS gamut mapped to P3 is color(display-p3 1 0.72344 0.82079) and taking that back to Oklch it is oklch(0.86331 0.10669 357.684). Lightness and he shifts similar to the sRGB case, but a better chroma due to the wider gamut P3 screen.

@mirisuzanne wrote:

Here's a codepen demo showing two colors with the same hue and lightness values, but vastly different perceptual lightness in the results.

Similarly this has [oklch(90% 90% 0deg)] which is rgb(152.937% 10.3745% 83.2625%) so naive clip gives rgb(100% 10.3745% 83.2625%) which is oklch(0.6836 0.29009 338.36), a change in lightness from 90% to 68.36% and a change in hue of 21.64deg.

TLDR; clip is a terrible gamut mapping replacement (unless the colors to be clipped are barely out of gamut)

@jamesnw
Copy link

jamesnw commented Nov 29, 2023

So that demo has oklab(90% 0.36 0) which is out of gamut for all RGB colorspaces (even prophoto-rgb) and is rgb(152.937% 10.3745% 83.2625%).

Thanks for the walkthrough of the issue here. I made a Codepen that compares the CSS Algorithm outputs for sRGB and display-p3 with a naive clip (and a comparison with the browser's adjustment, so we can compare when that is fixed).

@ccameron-chromium
Copy link
Contributor

I agree that the oklab and oklch spaces work best when paired with gamut mapping, and I think that the CSS gamut mapping algorithm produces good results for these spaces.

However, I do believe that the CSS gamut mapping algorithm can be inappropriate to apply to other things like display-p3 colors, because doing so can produce results that are undesirable (e.g, this example with reds). The CSS gamut mapping algorithm is really built for oklab and oklch (I might be tempted to call it something like "okl gamut mapping")

I think that the best way forward would be to "bake" CSS gamut mapping in to the definitions of oklab and oklch.

The difficulty is to define exactly what "baking" to do. Mapping to the display's gamut might be okay, but on sRGB-ish devices, it might do surprising things. When drawing to a canvas, the mapping cannot depend on the device's color space (ignoring fingerprinting, we just wouldn't want the non-determinism), and I don't think that mapping to the canvas' space would be that good (sRGB is the default and is very narrow).

One scheme would be something where we bake a well-known gamut into oklab and oklch, so we end up in effect having oklab-srgb or oklab-p3 or oklab-rec2020 (and the vanilla oklab defaults to one of those). I'm not a huge fan of this. The resulting geometries in oklab are very nonconvex and can have some sharp edges.

A better scheme could be to define a standard polyhedron to always do gamut mapping to, then I think that would be a really good way forward. This polyhedron should be big -- maybe as big as the spectral colors. And it could be made to be convex. (And maybe we could define it as being smooth).

I've been using this tool to visualize some of these options.

@svgeesus
Copy link
Contributor

The CSS gamut mapping algorithm is really built for oklab and oklch (I might be tempted to call it something like "okl gamut mapping")

No, it really isn't. It doesn't care whether the out of gamut color came from prophoto-rgb() or whatever. It is built to use oklch as the color space in which gamut mapping happens, yes. And so your conclusion that

I think that the best way forward would be to "bake" CSS gamut mapping in to the definitions of oklab and oklch.

is entirely unjustified.

@mirisuzanne
Copy link
Contributor Author

mirisuzanne commented Nov 30, 2023

The linked ('red/redder') example demonstrates to me is that rgb clipping works well when there is only one rgb channel in use. That seems like the extreme special case to me. Maybe there could be special handing of single-channel rgb in a gamut mapping algorithm? But as soon as you start combining channels in any color space, channel-clipping will cause hue-shift. That's the 99% case, and the case that gamut mapping is designed to solve.

(and while it may be better to have the 'redder' red in that case, even a slightly desaturated red is a much closer to user-intent than we get from the channel-clipping failure cases. At least it's still red!)

@romainmenke
Copy link
Member

romainmenke commented Nov 30, 2023

I think the red/redder example is a sidetrack because it starts from an incorrect assumption.

It makes the assumption that color(display-p3 1 0 0) is a redder red but that is untrue.

  • red is color(display-p3 0.92 0.2 0.14) (notice that the green and blue channels have different non-zero values)
  • color(display-p3 1 0 0) is a slightly different hue and is also brighter than red

Gamut mapping from color(display-p3 1 0 0) to srgb color space does not result in red because it never was a redder red. It will contain traces of the source being a slightly different hue and being brighter.


  • red is the purest "red" in the srgb color space
  • color(display-p3 1 0 0) is the purest "red" in the display-p3 color space

These both happen to have the maximum value in a single channel and zero values in the others in their respective color space.

Connecting these two values and assuming that one gamut maps to the other is incorrect.
It is seeing a pattern where there is none.

@ccameron-chromium
Copy link
Contributor

ccameron-chromium commented Dec 1, 2023

With respect to the "red redder", it is not obvious to me that this (the gamut mapped result) is a desirable representation of this (the original, needs a P3 monitor). I don't think that projecting along constant luminance is the right thing to do in the general case, but we can drop that for now.

I would really like to apply gamut mapping to oklab and oklch spaces.

The difficulty with the oklab and oklch space is that, when used correctly, they produce extremely out-of-gamut colors. For example, suppose I like this particular blue (I think it's oklab(46.64% 0 -0.32)), and I want to do a gradient from black to white through that blue. Here's that blue in oklab (sorry, I wrote oklch in the images...).

Visualization of the P3 gamut in oklab space, with a blue color circled

This is what the gradient looks like, with CSS gamut mapping, going from oklab(0% 0 -0.32) through oklab(100% 0 -0.32). It looks great!

Gamut with slicing plane showing gradient from black to white through the previous blue

But there is a problem here! There is a real mapping from points in this 3D space to chromaticity values, and this gradient does not accurately represent those colors.

At the top, at L=99%, the true color is a brilliant blue, but what we see here is almost-white. In the demo app, set gamut mapping to "none" (and enable the various flags), and you'll see this true color.

At the bottom, at L=1%, the chromaticity of the colors is a physically impossible color. In this picture I've added the spectral colors -- everything outside of the convex hull of the spectral colors is not a physically realizable color.

Gamut with spectral colors

The problem with oklab and oklch is that they can produce extremely out of gamut and physically impossible colors. A designer working with these colors, say, on a modern P3 display, may like what they see, but:

  • The author of the content has no way to visualize what they've truly specified.
  • The author may not like the representation on a more powerful display than the one they authored the content on.
  • The author has no way to constrain what they've authored to be exactly what they see on their authoring display.

The simplest solution that I see to this problem is to bake gamut mapping into the definitions of oklab and oklch.

@romainmenke
Copy link
Member

romainmenke commented Dec 1, 2023

@ccameron-chromium I am sorry, but I am not really following.

To me it seems that there are some misconceptions leading to incorrect conclusions about the new color notations, interpolation and gamut mapping.

I am not sure if this particular issue is the best place to answer these questions.

The focus of this issue is that browsers shipped color notations that can express wide gamut colors without implementing gamut mapping.

New issues for your specific questions would be easier to resolve without causing noise in this issue :)

@facelessuser
Copy link

facelessuser commented Dec 1, 2023

I am sorry, but I am not really following.

@romainmenke I think what is being suggested is just to map the points more 3 dimensionally instead of just mapping by reducing chroma in one dimension. It's mapping the geometry of a wider gamut surface down to a smaller gamut surface. This sacrifices preserving lightness, chroma, and hue to gain something the suggester thinks would be more intuitive. What is better can be subjective based on what your intent is.

CSS has chosen to preserve as much of the original color intent as possible by only reducing chroma (though some minor hue and lightness are sacrificed with the clipping via MINDE).

It is like what is expressed in this Oklab gamut mapping article. Do you just project along the chroma dimension? Or do you project in the lightness dimension as well, and if so, then by what degree do you project in the lightness dimension, or how much original lightness are you willing to sacrifice to get what you think is a "better" color? The suggestion being made is to do this more in 3 dimensions, most likely sacrificing more hue and lightness than CSS currently does.

EDIT: I do realize I am oversimplifying the 3-dimensional transform being suggested.

The focus of this issue is that browsers shipped color notations that can express wide gamut colors without implementing gamut mapping.

New issues for your specific questions would be easier to resolve without causing noise in this issue :)

I agree, this probably deserves a separate topic if a different algorithm wants to be discussed.

@svgeesus
Copy link
Contributor

svgeesus commented Dec 1, 2023

The difficulty with the oklab and oklch space is that, when used correctly, they produce extremely out-of-gamut colors.

This is factually incorrect. Highly out of gamut colors can be produced in pretty much any colorspace, including sRGB.

@w3c w3c deleted a comment from sobotka Dec 15, 2023
@ccameron-chromium
Copy link
Contributor

The difficulty with the oklab and oklch space is that, when used correctly, they produce extremely out-of-gamut colors.

This is factually incorrect. Highly out of gamut colors can be produced in pretty much any colorspace, including sRGB.

I think that the difference is whether or not it is obvious to the content author when they are entering the danger zone.

If specifying colors in an RGB color space, there's the clear signal that "if the parameters are outside of the [0,1] interval, then I'm playing with fire". True, one can specify color(srgb 0 -1 999), but it's clear that that is to set oneself up for a bad day.

Meanwhile, in something like oklab and oklch, the guardrails are less obvious. In the example from the codepen, the endpoints are oklch(90% 10% 0deg) and oklch(90% 90% 0deg). One of these is in the sRGB gamut and the other one is way outside of the the gamut of any existing monitor. Which is which? It's not obvious just by reading them.

It turns out that oklch(90% 90% 0deg) is the one that is way far outside of the gamut of what any existing monitor can produce. It's equivalent to color(rec2020 1.295 0.434 0.797).

For this reason, I think that we should provide content authors with a space that is perceptually uniform but does not suffer from this problem. A cylindrical space something like okhsl would be nice. (That particular formulation is very tightly tied to the sRGB gamut, so it won't do as a verbatim drop-in). The CSS gamut mapping algorithm has the effect of cylinder-ifying the oklab and oklch spaces.

@mirisuzanne
Copy link
Contributor Author

mirisuzanne commented Jan 24, 2024

Sure, there might be an even more perfect color space down the road. It's totally theoretical, but the theory is great. And if that perfect space ever ships in CSS, colors will continue to go out-of-gamut. And authors should get better behavior than random clipping when that happens.

We can't put this on authors, and expect them to just keep all their colors in the gamut. Because the web (by design) is an unreliable context. A cylindrical space with integer boundaries won't change that. We can't expect authors to carefully manage their colors across a web for everyone, on everything. Even the most carefully crafted rec2020 colors will continue to go outside some remaining sRGB monitors. When that happens, CSS should try and help provide a 'close match' for the majority of use-cases, rather than throwing up our hands.

When clipping is anywhere close to author intent, it's pure luck. That's not a solution, it's a stopped clock. We need an approach to out-of-gamut colors that attempts to maintain author intent. Now that browsers have shipped color-mix() and 'perceptually uniform' spaces, that need is even more urgent.

The default behavior should help get 90% of use-cases close-enough. And then we can provide additional tools for authors that need additional precision around the edges.

@mirisuzanne mirisuzanne changed the title [css-color-4] (ok)lch implementations break the entire purpose for authors [css-color-4] Channel clipping breaks author expectations, especially when using 'perceptually uniform' spaces Jan 24, 2024
@ccameron-chromium
Copy link
Contributor

We can't put this on authors, and expect them to just keep all their colors in the gamut.

I agree with this statement. It shouldn't be expected that an author ensure that all colors be in the gamut of the target device.

What concerns me is something slightly different: Should an author specify a color when they do not (or physically cannot) know what that color actually looks like?

Should an author specify colors that are very far outside of the gamut of any existing display (including the one that the author is using)?

Should an author specify colors that do not physically exist?

@eeeps
Copy link
Contributor

eeeps commented Jan 24, 2024

Should an author specify a color when they do not (or physically cannot) know what that color actually looks like?

"How do we help authors make sensible choices?" is a good question, but it's not the question that's being posed here, which is "how do we best adapt arbitrary content for users on varied hardware"?

And doing worse things for users – especially users on less-capable devices – is not a good answer to the "how do we help authors" question.

@mirisuzanne
Copy link
Contributor Author

If the concern is about color formats that give easy access to a very wide gamut, rather than a concern with adapting those colors for display, then browsers shipped the wrong half of the spec. Now authors are encouraged to use the fully interop/supported wide gamut formats, and there's absolutely no safety net in place to ensure those formats work for people on the other end. This is what has me confused.

We can't put this on authors, and expect them to just keep all their colors in the gamut.

I agree with this statement. It shouldn't be expected that an author ensure that all colors be in the gamut of the target device.

In that case, a solution that is specific to (ok)l** formats is not a solution to this issue. We can't 'bake gamut mapping' into a few wide-gamut formats and be done with it. We would still need gamut mapping for other wide-gamut formats, which may still render on narrower-gamut displays. If we want to also change how a few formats work, that should be a separate issue for discussion.

Trying to move gamut mapping forward, I see a few options on the table.

  • If the priority is to preserve lightness and hue, at the expense of chroma (this matches my experience in the field, but my experience is not universal) the current spec does that decently well using oklch and ΔEOK.
    • In the thread above, @facelessuser points out some tradeoffs with oklch vs lch as the base model, which would warrant a a side-by-side comparison and decision.
  • Is there a solid counter-proposal for a multi-channel mapping algorithm? Can we bring examples to a telecon, and discuss the tradeoffs that we're willing to make?
  • The other hinted-at suggestion, which I may be reading incorrectly, is that different formats represent different authors intents in some heuristic way? Which might lead us to different mapping for different color formats? I can see some logic to that - I would reach for different formats to achieve different goals, but it leads to some strange outcomes:
    • The same out-of-gamut color, on the same narrower-gamut device, is mapped to a different result for reasons that may not be obvious.
    • On the plus side, we're providing tools to change the format of a color, so authors would have an escape hatch?
    • Browsers have to maintain several mapping algorithms? Is that reasonable?

Did I miss something? Can we narrow in on a path forward?

@svgeesus
Copy link
Contributor

CSS and images/videos have had color-matching in sRGB and P3 for many years. (This wasn't always true, but we were able to pull it off in CSS2.) This is still a common, important use-case for authors, and\ as images are now being deployed in the wild using P3-compatible gamuts or wider, we want to preserve it.

* We should ensure that, _by default_, CSS colors and image/video colors map 
   in the same way so color-matching works when both are using the same gamut, 
   at least within realistic image/video gamuts.

Pushing some more on this assertion that in-gamut colors in images never change, here are a few useful diagrams from Ján Morovič "Color Gamut Mapping". These are all using typical RGB to CMYK gamuts so absolutely no "unreasonable" colors.

The first shows a variety of input-to-output transfer functions used in gamut mapping; only one (here, labelled clipping) has a straight line where output=input for in-gamut colors. The others use a soft knee, or a sigmoidal function, to ease the transition and thus, some or all in-gamut colors change.

CGM-54

Next, a division of gamut volume into core colors which will not change, destination colors (the intersection of source and destination, minus core) where gamut compression is applied, and source (outside destination; all colors will change):

CGM-1011

And lastly, a constant hue and lightness-compressing GMA which uses that core/destination division to perform gamut mapping into the destination:

CGM-1018

@romainmenke
Copy link
Member

Wasn't clear from the proposal or the minutes, but is gamut mapping still the last step?

Current steps around interpolation, color space conversions, relative colors,... are quite complex and depend on gamut mapping happening at a later stage, right before display.

Would this still be true? Or would it happen earlier?


Has the prototype in Chrome advanced enough to be able to test color mixing, relative colors, gradients, ...?

Simple color declarations aren't the best way to validate the proposed solution, more advanced values allows us to stress test this a bit more.

@eeeps
Copy link
Contributor

eeeps commented Jun 12, 2024

@tabatkins:

(2) We propose that, when mapping a used color into the monitor gamut, by default it is done by naive r/g/b channel clipping, to match existing image/video gamut reduction behavior.

Clipping Rec.2020 colors to, e.g., sRGB sometimes produces significant lightness shifts vs doing what's in the spec now (which tries to preserve lightness above all else). Here's a ~worst-case, real-world example of how different results can be, even post-mapping-to-Rec.2020: https://codepen.io/eeeps/pen/zYQPqKM

@LeaVerou:

I think it's quite important to give authors control of what to prioritize: hue, chroma, lightness, relationships between colors, etc. since use cases have drastically different demands. For accessibility, I think preserving lightness should probably be the default.

I agree; "match the image processing pipeline" should be an option, but I would prefer if "preserve lightness" were the default – not only for the proposed first stage, where wildly out-of-gamut OkLCH colors are mapped to Rec.2020, but also when doing the proposed second stage: mapping Rec.2020 colors to physical display gamuts.

@jamesnw
Copy link

jamesnw commented Jun 13, 2024

@eeeps:

Clipping Rec.2020 colors to, e.g., sRGB sometimes produces significant lightness shifts vs doing what's in the spec now (which tries to preserve lightness above all else). Here's a ~worst-case, real-world example of how different results can be, even post-mapping-to-Rec.2020: https://codepen.io/eeeps/pen/zYQPqKM

An alternative could be that we map to either rec2020 or p3, choosing the smallest that has a gamut larger than the user's monitor, and then clip from there. Here's a fork of your codepen, with the addition of an example where we first map to p3, then clip to sRGB. https://codepen.io/jamessw/pen/abrVLvQ

I added the Lightness Delta in oklab, and for that specific super red, this method would reduce the lightness shift by about half. For colors starting outside of rec2020, the lightness preservation by mapping to p3 before clipping would be even more drastic.

@jamesnw
Copy link

jamesnw commented Jun 13, 2024

@romainmenke:

Has the prototype in Chrome advanced enough to be able to test color mixing, relative colors, gradients, ...?

Simple color declarations aren't the best way to validate the proposed solution, more advanced values allows us to stress test this a bit more.

There is a port of the Chromium algorithm in the Color.js Gamut Mapping apps at https://apps.colorjs.io/gamut-mapping/ and https://apps.colorjs.io/gamut-mapping/gradients which may help with some exploration.

@mirisuzanne
Copy link
Contributor Author

The goal of the two-step process (as I understand) was to "bake step 1 into the format" so it doesn't change over time. So I don't think a contextual first step (either rec2020 or p3) meets the requirement.

@romainmenke
Copy link
Member

romainmenke commented Jul 10, 2024

colorjs doesn't support relative color syntax.

What I am mostly interested in is how colors behave depending on the notation.

With relative color syntax you can change the notation.
A value first described with oklch might end up being used in color(from ... srgb r g b).

https://codepen.io/romainmenke/pen/GRbgMPj

Screenshot 2024-07-10 at 16 55 09

I would still prefer that gamut mapping was applied to all CSS color values and that it doesn't depend on how a value is declared.

With another way to opt-in/out of clipping/gamut mapping for all CSS colors.


Also concerned about mapping to rec2020 and then clipping.

Is there any clipping or gamut mapping that happens before any other interpolation?
Or is clipping/gamut mapping always the last step as intended by the current specification text?

Clipping literally throws away information and in a way that does not preserve the balance between channels.

i.e. these gradients should render differently as they should render the hue circle in opposite directions.

#a {
  background: linear-gradient(to right in oklch longer hue, oklch(from color(srgb 1 -0.01 0) l c h), red 100%);
}

#b {
  background: linear-gradient(to right in oklch longer hue, oklch(from color(srgb 1 0 -0.01) l c h), red 100%);
}

Chrome (with the flag) shows pure red, but maybe not by design given that the implementation is experimental?

https://codepen.io/romainmenke/pen/dyBPZoG?editors=1100

@ccameron-chromium
Copy link
Contributor

  • ACTION ccameron: review gamut mapping algorithms

I've opened #10579 on this topic (with a request for algorithms to consider).

@romainmenke
Copy link
Member

Are we losing the window where we can still make changes?

Given that there is pretty good interop for most of css-color-4 and css-color-5 I am worried that authors are increasingly likely to depend on the current clipping behavior.

It also seems that the experimental implementation was removed from Chrome?

@LeaVerou
Copy link
Member

Chatting with @jyasskin today, it looks like there has been a miscommunication: @ccameron-chromium has been expecting a gamut mapping algorithm from the CSS WG, and the CSS WG has been waiting for him to review existing algorithms per action item in #9449 (comment) .

We need to resolve this ASAP and move forwards before the window for changes closes. @astearns could we slot this in the TPAC agenda somewhere? 🙏🏼

@astearns
Copy link
Member

@LeaVerou is there a set of existing algorithms that could be listed in #10579?

@LeaVerou
Copy link
Member

@astearns not in a comment, but here: https://apps.colorjs.io/gamut-mapping/
@svgeesus are there any others? Is this an exhaustive list?

@astearns
Copy link
Member

@LeaVerou are all the options in https://apps.colorjs.io/gamut-mapping/ still in play, or could the list be reduced further? A comment in 10579 instead of this one would be useful.

@argyleink
Copy link
Contributor

here's a newer one that may not be tracked https://github.com/texel-org/color

@facelessuser
Copy link

here's a newer one that may not be tracked https://github.com/texel-org/color

IIRC, that is just implementing Björn's gamut mapping algorithm with constant lightness as described here: https://bottosson.github.io/posts/gamutclipping/.

You can see it compared against all the others here: https://deploy-preview-7--color-apps.netlify.app/gamut-mapping/gradients?from=oklch%2890%25+.4+250%29&to=oklch%2840%25+.1+20%29.

I've had a PR to merge it open for a while, but I don't have the power to merge it officially, so the preview is the best way to view it currently.

@svgeesus
Copy link
Contributor

Slightly off topic but

I've had a PR to merge it open for a while, but I don't have the power to merge it officially, so the preview is the best way to view it currently.

Not seeing it?

@svgeesus
Copy link
Contributor

If we give the comparison a bit more work to do then Edge Seeker is the fastest at 2ms, (same speed as clip!) while CSS_Rec2020 is slowest at 10ms (in Firefox on my machine). Times in Chrome are similar, Edge Seeker at 0.6ms while CSS_Rec2020 is 3.3ms.

@LeaVerou
Copy link
Member

here's a newer one that may not be tracked texel-org/color

IIRC, that is just implementing Björn's gamut mapping algorithm with constant lightness as described here: bottosson.github.io/posts/gamutclipping.

You can see it compared against all the others here: deploy-preview-7--color-apps.netlify.app/gamut-mapping/gradients?from=oklch%2890%25+.4+250%29&to=oklch%2840%25+.1+20%29.

I've had a PR to merge it open for a while, but I don't have the power to merge it officially, so the preview is the best way to view it currently.

Merged now, sorry for the delay!

@mirisuzanne
Copy link
Contributor Author

What's the status of this, and is there any chance it will move forward?

@matuzo
Copy link

matuzo commented Apr 7, 2025

Recently I've tried to use oklch() and the relative colour syntax to create a super simple colour scale in CSS. I took two colours from an existing website and tried to create a scale from 100% lightness to 0% lightness and the results were very disappointing.
Here is an example how an almost white colour looks like with the two colours I picked. This gamut clipping technique that browsers are using makes oklch() unreliable and practically unusable.

input color oklch(99% .12 182.55) input color oklch(99% .20 9.96)

https://apps.colorjs.io/gamut-mapping/?color=oklch%2899%25+.12+182.55%29&color=oklch%2899%25+.20+9.96%29

Is there any chance we can get a method that gives us expectable results? Because this clipping technique has real negative impact already. Like I said, I picked existing colours. I didn't deliberatly try to find exceptionally bad examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
css-color-4 Current Work
Projects
Status: Wednesday morning
Development

No branches or pull requests