Percentage number coords: why do the percentage value ranges of the sRGB Space coords differ from seemingly all others? #643
Replies: 2 comments 3 replies
-
Color.js is aligned with the new color syntax for RGB spaces: https://www.w3.org/TR/css-color-4/#color-function. The original use of 0-255 is based on the usage of colors in UINT8 storage types, 0-1 is a more convenient range in floating point. It's pretty trivial to multiply your values by 255 if you must consume them in the 0-255 range. You can also go the route of creating a custom color space class that automatically outputs the color in this range. More info on such an approach can be found in this discussion: #593. |
Beta Was this translation helpful? Give feedback.
-
If you want to format an individual channel I would look into the Format and Type classes
I would be nice if there was a higher level interface to the |
Beta Was this translation helpful? Give feedback.
-
TL;DR: why does the sRGB space store its coords in
<percentage>
s in the range [0, 1] when seemingly all other spaces use the<number>
ranges of their<percentage> | <number> | none
CSS value types (e.g. [0, 100] for HSL's s and l channels)?For the s and l channels of an HSL color, the internal values on the
coords
property are relative to the value 100 (e.g. 50 = 50%). For the r, g, and b channels of an RGB color, the internal values on thecoords
property are relative to the value 1 (e.g. 0.5 = 50%). Seemingly for all other spaces (I checked HWB, Lab, LCH, Oklab, and OkLCH), the coords behave like HSL. Why that difference?I want to output coords formatted such that they're valid values in a CSS color (and vice-versa: read exactly this output and turn into coords for a
new Color()
. I want to do so by channel so I can't just format the whole number. I want to format an individual channel.It should only take knowing what CSS specifies as a valid type for that channel to do so. For example, I can look up https://www.w3.org/TR/css-color-4/#funcdef-rgb and find out that RGB channels are relative to the value 255 for
<number>
and relative to the value 100 for<percentage>
. Similarly for HSL (https://www.w3.org/TR/css-color-4/#funcdef-hsl), I find that both<number>
and<percentage>
on the s and l channels are relative to the value 100. For my purposes, I'm fine with outputting the<number>
representation so for the colors in the example above, I want to show50
for the HSL color's s channel and 127.5 for the RGB color's g channel.This is difficult to do with the colorjs.io coords using different ranges like that because I can't rely on the fact that a channel that is
<percentage> | <number> | none
in CSS is stored as a value in one knowable range in colorjs.io. I also have to know which space I'm dealing with and I have to know that per space I'm interacting with. I do always know the space, but it makes the logic for outputting the right things needlessly complex. Ifhsl.coords
were[ 270, 0.5, 0.25 ]
(andrgb.coords
unchanged) orrgb.coords
were[ 255, 127.5, 63.75 ]
(andhsl.coords
unchanged), I could remove the knowledge of which color space I'm dealing with from this process; I'd just need to know the CSS type definition of the channel itself and the formatting would be easy.Looking further into this, it almost looks like an oversight that RGB coords are stored/represented this way in colorjs.io
Beta Was this translation helpful? Give feedback.
All reactions