css4j / css4j

CSS parser with Event and Object Model APIs, a DOM wrapper and a CSS-aware DOM implementation. Written in the Java™ language.

Home Page:https://css4j.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Clamping out-of-gamut lab/lch colors

carlosame opened this issue · comments

The current procedure to clamp out-of-gamut colors when converting from lab() and lch() to RGB (sRGB color space) in toRGBColorValue() and CSSColorValue.toRGBColorValue(boolean) is inadequate, and a more elaborated process should be used.

This issue is opened as a reminder that whatever comes out from CSSWG's (csswg-drafts) issue 5191 should be looked upon.

I want to ship Lab/LCh with version 3.4.0, so I implemented a procedure close to the spirit of what is being discussed in CSSWG's issue 5191: perform a chromaticity reduction until the clipped value is within a ΔE2000 of 2 from the non-clipped RGB.

Here are some figures from the unit tests, where ΔE2000 is the distance from the original to the clamped color:

Original RGB ΔE2000
lab(54.2324% 80.9288 70.8493) #f00 0.334
lab(72% 15.9649 130.0235) rgb(85.74%, 65.89%, 0%) 9.948
lab(59% -100.8654 78.8047) rgb(0%, 65.42%, 16.8%) 9.727
lch(54.2324% 107.5597 41.2006) #f00 0.334
lch(72% 131 83) rgb(85.74%, 65.89%, 0%) 9.948
lch(59% 128 142) rgb(0%, 65.42%, 16.8%) 9.727

Closing the issue.