3 ms·
Ask HN: Why are the calculations in latest WC3 CSS Color Module Level 4 wrong?
I was looking for a calculation to convert between RGB and HSL color spaces and found some equations in the latest W3C CSS Color Module Level 4 working draft (15 Dec 2021). But the calculation for converting sRGB colors to HSL colors sets the hue value to NaN [1]. Why? If I can't trust the standard to be correct, then what can I trust?
[1] - https://www.w3.org/TR/css-color-4/#rgb-to-hsl
- Someone 5y ago> But the calculation for converting sRGB colors to HSL colors sets the hue value to NaN Only if the value of d is zero. That happens iff r == g == b, so for white, black and the gray values in between them. That technically is right. https://en.wikipedia.org/wiki/HSL_and_HSV https://en.wikipedia.org/wiki/HSL_and_HSV: HSL and HSV are both cylindrical geometries (fig. 2), with hue, their angular dimension, starting at the red primary at 0°, passing through the green primary at 120° and the blue primary at 240°, and then wrapping back to red at 360°. In each geometry, the central vertical axis comprises the neutral, achromatic, or gray colors ranging, from top to bottom, white at lightness 1 (value 1) to black at lightness 0 (value 0). So, for the colors on the central axis, the hue is undefined. Picking any specific value for the hue would be an arbitrary choice.
- rikroots 5y agoYes - after I got over my anger (partly at myself for copying the function into my code without properly reading through it first) I sat down and worked out what was going on. The function, and the functions dependant on it, work fine if I replace NaN with 0. As functions go, it is very neat and tidy. Some of the code I came across while searching for an RGB > HSL solution were ... not pleasant to read through.
- Someone 5y ago> Some of the code I came across while searching for an RGB > HSL solution > But the calculation for converting sRGB colors to HSL I don’t know whether you’re aware, but RGB and sRGB are different things. sRGB is device-independent, RGB is device-dependent. Depending on what the values in the RGB you have mean, that may or may not involve ‘not pleasant’ code. (Color is one of the items on the long list of things to try to avoid when programming. A few of the other items on that list are Unicode, dates and times, multi-threading and floating point. All have standards that aren’t pleasant, but also the most pleasant we have available)
- rikroots 5y ago> Color is one of the items on the long list of things to try to avoid when programming. Too late. I got annoyed by the lack of support for HWB, LCH and LAB in Chrome/Firefox. Safari now supports them for CSS, but has not yet made the effort to support them in the canvas element. I wanted users of my JS canvas library to be able to fill and stroke things using any valid CSS color string ... so I've been doing the work to make it happen over the holidays. It's been worth the effort. Red > Blue gradients look so, so much better when using LAB color space values, compared to what you normally get using RGB.