Bug 255939 - (OK)LCH implementation is not according to spec
Summary: (OK)LCH implementation is not according to spec
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: CSS (show other bugs)
Version: Safari 16
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: BrowserCompat, GoodFirstBug, InRadar, WPTImpact
Depends on:
Blocks:
 
Reported: 2023-04-25 11:23 PDT by Vasilis
Modified: 2024-03-13 01:50 PDT (History)
10 users (show)

See Also:


Attachments
rendering in safari, firefox, chrome (323.08 KB, image/png)
2024-02-12 09:14 PST, Karl Dubost
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Vasilis 2023-04-25 11:23:46 PDT
The implementation of LCH and OKLCH is incorrect. According to the spec `oklch(1 0.4 240)` should be white, and `oklch(0 0.4 240)` should be black: 

> "If the lightness of an Oklch color is 0% or 0, or 100% or 1.0, the hue component is powerless and the chroma is 0." 

https://drafts.csswg.org/css-color-4/#specifying-oklab-oklch

Here’s a test URL:
https://vasilis.nl/dingen/bugs/oklch.html

You can see that all four squares are not black nor white. This is of course a bug. 
Plain LCH should behave the same of course and as you can see it is buggy in the same way.
Comment 1 Radar WebKit Bug Importer 2023-05-02 11:24:29 PDT
<rdar://problem/108799783>
Comment 2 Sebastian Zartner 2023-08-06 06:59:10 PDT
The following web platform tests are affected by this bug:

https://wpt.fyi/results/css/css-color/lch-009.html?label=master&label=experimental&aligned
https://wpt.fyi/results/css/css-color/lch-l-over-100-1.html?label=experimental&label=master&aligned *
https://wpt.fyi/results/css/css-color/oklch-009.html?label=experimental&label=master&aligned
https://wpt.fyi/results/css/css-color/oklch-010.html?label=experimental&label=master&aligned

* Also requires clamping the luminance value to 100 as required by the spec.

> Values less than 0% or 0.0 must be clamped to 0% at parsed-value time; values greater than 100% or 1.0 are clamped to 100% at parsed-value time.

Sebastian
Comment 3 Vasilis 2023-10-08 13:45:09 PDT
I’m not sure how the prioritization of bugs works exactly, but I think this should have a higher priority: once this bug is fixed, websites that use (ok)lch will look different, in some cases even completely different. They may turn unusable depending on the use. 
Bright colors may turn white, and subtle dark colors may turn completely black. All colors will probably change. Some a bit, others dramatically. The more websites use this color function the bigger the problem will be. 

It should be fixed.
Comment 4 Vasilis 2023-10-09 01:06:01 PDT
There’s a javascript implementation of LCH that seems to be accurate. I’m not sure if it’s 100% accurate, but looking at the authors it could very well be. Maybe that helps?

https://css.land/lch/
Comment 5 Miriam Suzanne 2023-10-09 12:10:57 PDT
Authors have been very excited about this feature, as a way to achieve more perceptually-uniform lightness. That was sold to us as the entire reason for this color format in the first place. It seems pretty shocking that all browsers shipped the format in a way that is not even close to perceptually uniform. This is not a small bug - it fundamentally breaks the entire purpose of the color format.
Comment 6 Karl Dubost 2024-02-12 09:14:53 PST
Created attachment 469827 [details]
rendering in safari, firefox, chrome

rendering of https://vasilis.nl/dingen/bugs/oklch.html
Safari Technology Preview  188           19619.1.2.1.1
Firefox Nightly            124.0a1       12424.2.9
Google Chrome Canary       123.0.6290.0  6290.0
Comment 7 Philip Jägenstedt 2024-03-12 03:04:37 PDT
Note that implementing this as a special case for 0 and 100 in Chrome has resulting in an unfortunate discontinuity when moving a tiny distance away from 0 or 100. I've filed https://issues.chromium.org/issues/329106317.

It would be good to coordinate a bit to resolve the interoperability issue we now have, while also shipping behavior that is reasonable for developers.
Comment 8 Matthieu Dubet 2024-03-12 10:57:01 PDT
Indeed, this special case for lightness being <= 0 or >= 100 give some surprising discontinuity.

There are ongoing discussions in the CSS working group around gamut mapping like https://github.com/w3c/csswg-drafts/issues/9449 which would certainly change this behaviour. 

Implementing this change in the meantime might be counter productive from a browser compatibility point of view.
Comment 9 Philip Jägenstedt 2024-03-13 01:50:46 PDT
I agree that making changes right now is a bit risky because discussions are still very active.

In the meantime I've sent https://github.com/web-platform-tests/wpt/pull/45073 to guard against the discontinuity between 99.9999% and 100% lightness.