gka / chroma.js

JavaScript library for all kinds of color manipulations
https://gka.github.io/chroma.js/
Other
10.13k stars 545 forks source link

color returned by the `oklch()` function does not match input color space CSS #332

Closed shipurjan closed 2 months ago

shipurjan commented 6 months ago

I'm not sure if it's actually a problem, maybe they can't be converted between each other perfectly, but I'm adding this issue just in case

Problem:

In CSS a color like #ef4444 (HEX) or rgb(239 68 68) (RGB) does not produce the same perceived color converted to OKLCH with function chroma('#ef4444').oklch().

Demo:

https://codesandbox.io/p/sandbox/oklch-hsl-rgb-color-space-comparison-c6gtyn?file=%2Fsrc%2FApp.tsx

To see the color output from the function as it is converted from the oklch() function: remove line 281 from the demo above

// boost C value a little bit, since chroma-js doesn't convert hex to oklch exactly 1-to-1
if (i === 1) return n * 100 + (n === 0 ? 0 : 39);

Currently it's boosted by 39 to match the hex color more closely, but originally it doesn't match

thorn0 commented 3 months ago

You use values from the range 0...0.4 as percents. The fix is if (i === 1) return (n * 100) / 0.4;

shipurjan commented 2 months ago

Indeed, so it all works out then. Thank you