Re: [csswg-drafts] [css-color-4] Channel clipping breaks author expectations, especially when using 'perceptually uniform' spaces (#9449)

I hope it is the right place to share, excuse me if it is not. I was working on optimizing an OKLCH gamut mapping algorithm for a while. After some experimenting, I took a geometrical approach with a look-up table and it works nicely.

### Results according to my benchmarks

- It works ~30x faster than binary search in detecting the maximum ะก for given L and H.
- The quality of results is also great. The maximum error in chroma is just 0.0172 (`#feffe6` vs `#ffffda`)
- It even detects out-of-gamut OKLCH color ~2x faster than converting it to RGB and checking values
- It works with any RGB-based gamut

### The algorithm

The slice of OKLCH looks almost like a triangle. If we could estimate the shape of the slice we could easily find the maximum chroma. To approximate the shape well enough we need just two values:
1. Position of the most chromatic color (the tip of the "triangle")
2. Radius of the arc to describe the bright side of the "triangle"

![image](https://github.com/w3c/csswg-drafts/assets/20913587/94cad6aa-509e-436a-816a-caa9314c73e5)

Knowing these values is enough to approximate the shape:
- The dark side from 0 luminance to the most chromatic color is always a triangle by definition
- And the bright side is approximated with the arc.

All that is left is generating a look-up table with those values and we are ready to go.

The current version of the code is [here](https://github.com/ardov/color-lab/tree/main/src/pages/calculations/algorithms/curvature). It is still a work in progress, just wanted to share the idea of the algorithm.

-- 
GitHub Notification of comment by ardov
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/9449#issuecomment-1945045761 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Wednesday, 14 February 2024 23:45:08 UTC