Skip to content

Fix lch() chroma percentage reference range (100% should be 150, not 125) - #238

Open
amalsaidov wants to merge 2 commits into
linebender:mainfrom
amalsaidov:fix-lch-chroma-percentage
Open

amalsaidov wants to merge 2 commits into
linebender:mainfrom
amalsaidov:fix-lch-chroma-percentage

Conversation

@amalsaidov

Copy link
Copy Markdown

While adding some tests around the CSS color parser I noticed that lch() chroma given as a percentage comes out too low.

CSS Color 4 defines the percentage reference ranges per function (§ "lab(), lch(), oklab(), oklch()"):

function component 100% maps to
lab() a, b 125
lch() Chroma 150
oklab() a, b 0.4
oklch() Chroma 0.4

The dispatch in parse_color_prefix passes the same scale (1.25) to both lab() and lch(). That is right for lab() a/b (125), but lch() chroma should use 1.5 (150). As a result:

// before this change
parse_color("lch(50% 100% 40)").unwrap().components[1]  // == 125.0, should be 150.0

The ok* variants are unaffected because oklab a/b and oklch chroma share the same 0.4 reference (0.004).

This passes 1.5 for lch() and adds a regression test checking the percentage reference range for all four functions. cargo test passes.

Spec: https://www.w3.org/TR/css-color-4/

CSS Color 4 defines the percentage reference range for lch() chroma as
100% = 150, but the parser reused the lab() a/b range (100% = 125), so
chroma percentages were parsed ~17% too low (e.g. lch(50% 100% 40) gave
a chroma of 125 instead of 150).

Pass the correct 1.5 scale for lch() and add a regression test covering
the percentage reference ranges of lab(), lch(), oklab() and oklch().

Signed-off-by: karma <inskarma0101@gmail.com>
Signed-off-by: karma <inskarma0101@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant