Get lightnes channel as % in OKLCH? #504
Replies: 2 comments 4 replies
-
This is not a problem with JavaScript, but the way floating point arithmetic works in all modern computers. The data type cannot store an infinite amount of decimal places to preserve exact precision, so at times you will get what you have demonstrated in your opening question. The slight imprecision is introduced during the mathematical conversion from the lightness value to the percentage value. Even if Color.js provided a specific function to get precision, you would still get decimals with the exact same behavior. The library automatically doing it vs you doing it will not change the results. You can round values off if you do not like repeating decimals. So generally, I would view this as two separate questions.
|
Beta Was this translation helpful? Give feedback.
-
First, I do think we should use Second, that seems like an excellent use case for custom formats: let turquoise = new Color("oklch(.7 .1 180)");
turquoise.toString({format: {
name: "oklch",
coords: [
"<percentage>[0, 255]",
"<number>",
"<angle>"
]
}}) Here’s a color notebook showing this: https://colorjs.io/notebook/?storage=https%3A%2F%2Fgist.github.com%2FLeaVerou%2F80cf3f377267a0380edbf59db1bf0db0 |
Beta Was this translation helpful? Give feedback.
-
I'm working with OKLCH. I need to get the lightness channel as a % as that's how it's typically displayed in CSS eg
oklch(15.352% 0.0368 183.61)
By default it seems this library doenst use % for lightness and instead a range from 0-1. When I multiply these decimals by 100 to get the % value I sometimes run into the issue JavaScript has in multiplying decimals eg
0.15352 * 100 = 15.351999999999999
.I know that this is a qwerk of JavaScript and not the fault of this library per se, but is there an easy way to get the lightness channel as a % to save me from finding some work around?
https://codesandbox.io/p/sandbox/gracious-tdd-vsymvs?file=%2Fsrc%2FApp.tsx%3A1%2C1-33%2C1
Beta Was this translation helpful? Give feedback.
All reactions