You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[css-color-4] How to handle conversions that result in negative lightness? #9484
It's possible for a color conversion to result in negative lightness. For example, the color lab(1% 60 100) is naively equivalent to oklab(-4.8743815325% 0.7106198377 0.1847214228). Negative lightnesses are generally disallowed, but the spec is only explicit that they should be clamped at parsed-value time, which leaves it ambiguous what to do when they come up as the result of a conversion.
It's not clear to me what the right solution is here. If you clamp eagerly, you break the ability to round-trip safely between Lab and OKLab. Clamping at parsed-value time means that there are valid Lab colors which can't be represented in OKLab. Is this acceptable? If not, how should it be solved?
Edit: A more prominent example is color(xyz-d65 1 1 1), which can't be exactly represented in Lab without a lightness above 100% because Lab uses a d50 whitepoint.
In Lab, the first argument specifies the CIE Lightness, L. This is a number between 0% or 0 and 100% or 100 Values less than 0% or 0 must be clamped to 0% at parsed-value time; values greater than 100% or 100 are clamped to 100% at parsed-value time.
Now, colors specified some other way but converted:
Colors may be converted from one color space to another and, provided that there is no gamut mapping and that each color space can represent out of gamut colors, (for RGB spaces, this means that the transfer function is defined over the extended range) then (subject to numerical precision and round-off error) the two colors will look the same and represent the same color sensation.
So: if I take color(xyz-d65 1 1 1) and convert it to Lab offline and paste the result, lab(100.12 9.0645 5.8018) into my CSS then it will be clamped at parse time.
But if I do color-mix(in lab, color(xyz-d65 1 1 1) 100%, black) there will be no clamping until it comes time to display it on screen at used-value time.
I appreciate that this sucks for CSS pre-processors.
The bearing of the second paragraph you quoted on this isn't at all obvious to me, even now that you've called it out. I'm happy enough to take "out-of-gamut lightness is a valid intermediate state" as a resolution on your say-so, but I think the current wording of the spec would make it very difficult for most people (even those fairly familiar with color spaces) to confidently deduce that color-mix(in lab, color(xyz-d65 1 1 1) 100%, black) is supposed to be lab(100.12 9.0645 5.8018) and not lab(100 9.0645 5.8018).
I've been doing some additional poking around with this in relation to sass/sass#3737, and I'm a bit confused about some related behaviors in the latest draft of Color 4. This draft doesn't mention any kind of clamping at any point for hsl() syntax (unlike rgb() which clamps all its channels), and it says that "HSL colors resolve to sRGB". At the same time, the sample conversion algorithms explicitly say that they expects the saturation and lightness to be "in reference range [0,100]" and the red, green, and blue components to be in 0..1. (For what it's worth I believe the RGB/HSL conversion algorithms do work as written to round-trip out-of-gamut colors, although the HWB algorithms do not.)
Browsers (I've tested Chrome and Firefox) also do seem to clamp colors specified with hsl(). color-mix(in xyz, hsl(0, 0%, 500%) 50%, black) and color-mix(in xyz, hsl(0, 0%, 100%) 50%, black) both render as color(xyz 0.4752279635 0.5 0.5445288754), despite the fact that the former should have much higher lightness than the latter. Should this be considered a bug in the browsers' implementation (or just not adopting the latest spec yet)?
This is particularly important for Sass, because we need a way to represent out-of-gamut legacy colors that's both compatible with older browsers and preserves the semantics of the out-of-bounds colors in modern browsers. We had originally been emitting rgb() with out-of-gamut values, but it looks like that's now clamped, so I'm hoping that out-of-gamut HSL will work instead.
(Edit: For what it's worth, my preference would be not to clamp rgb(), or lab() for that matter, at parse-time so that the simple representation of out-of-gamut RGB colors works the same way as out-of-gamut color() values.)
hsl() has a parse-time clamp of negative saturation, when using legacy syntax. The above issue is about what to do with hsl() modern syntax, at parse time, where there is no interop.
For rgb() there is parse-time clamping in legacy syntax and in modern syntax, because of historical backlog (same with the 0 to 255 range) while color() does not, because it does not have that web-compat burden.
At the same time, the sample conversion algorithms explicitly say that they expects the saturation and lightness to be "in reference range [0,100]" and the red, green, and blue components to be in 0..1
The reference range is used for converting percentage values to numbers. It does not imply that values outside that range are invalid.
(For what it's worth I believe the RGB/HSL conversion algorithms do work as written to round-trip out-of-gamut colors, although the HWB algorithms do not.)
I agree, they do.
For what it's worth, my preference would be not to clamp rgb(), or lab() for that matter, at parse-time so that the simple representation of out-of-gamut RGB colors works the same way as out-of-gamut color() values.)
If rgb() was just now being deployed, that would be preferable but we have Web compat to consider. Which brings us to `lab() and friends. The CIE ones (for compatibility with industry practice, again) use a D50 white point and thus, very dark colors with unrealistically high chroma will tend to become negative on one side of the neutral axis because chromatic adaptation is effectively tilting that axis.
hsl() has a parse-time clamp of negative saturation, when using legacy syntax. The above issue is about what to do with hsl() modern syntax, at parse time, where there is no interop.
But what about lightness? As specified, HSL does no clamping for lightness, but in practice browsers do clamp it as I described above.
Negative saturation is a much less critical concern for me, because (I believe) hsl(h -s l) is always equivalent to HSL((h + 180) s l) anyway.
The reference range is used for converting percentage values to numbers. It does not imply that values outside that range are invalid.
In that case, is it a bug that the HWB conversions don't round-trip accurately if the percentages are outside [0,100]?
If rgb() was just now being deployed, that would be preferable but we have Web compat to consider. Which brings us to `lab() and friends. The CIE ones (for compatibility with industry practice, again) use a D50 white point and thus, very dark colors with unrealistically high chroma will tend to become negative on one side of the neutral axis because chromatic adaptation is effectively tilting that axis.
I understand why negative lightnesses can appear in Lab. But why are they clamped?
@nex3 I am so sorry. I keep reading over this issue, reading over related issues, and trying to come up with a self-consistent set of principles for when clamping happens and why; but in some cases the "why" is "historical precedent from CSS Color 3" and stuff like that. So I don't form a coherent answer and leave it to come back to.
That must be very frustrating, my apologies.
The "in practice" is because CSS Color 3 puts two different things into one paragraph: parse-time clamping of HSL input and display-time clamping to the device gamut:
If saturation is less than 0%, implementations must clip it to 0%. If the resulting value is outside the device gamut, implementations must clip it to the device gamut. This clipping should preserve the hue when possible, but is otherwise undefined. (In other words, the clipping is different from applying the rules for clipping of RGB colors after applying the algorithm below for converting HSL to RGB.)
Because implementations at that time assumed all screens were sRGB, and also because hsl() serializes as rgb() with only 8-bit precision, implementations of that time can't be blamed for condensing that into one operation on input.
To be clear, I'm totally willing to have a solution here that doesn't work perfectly in browsers yet. If you can confirm that the intention of the current spec is that hsl() should not clamp lightness, then we can make Sass's implementation target the spec and have a decent-but-not-great fallback for older browsers in which lightness is clamped, with the expectation that future browsers will do the right thing eventually. That's a lot better for us than having to emit a color() function that doesn't work in older browsers at all!
Since we need to move forward here with the Sass implementation, I'm going to operate on the assumption that the spec as currently written is intended—specifically in that a negative HSL lightness should not be clamped at any point—and that browsers will be expected to match this specification at some point in the future. If this assumption ends up being incorrect, please let me know as soon as possible.
So: if I take color(xyz-d65 1 1 1) and convert it to Lab offline and paste the result, lab(100.12 9.0645 5.8018) into my CSS then it will be clamped at parse time.
But if I do color-mix(in lab, color(xyz-d65 1 1 1) 100%, black) there will be no clamping until it comes time to display it on screen at used-value time.
I think we need tests for this.
Current implementations:
I added the result from the color.js color-mix() step by step app, which does no clamping. (It doesn't do RCS in that app, so I can't do the second example).
values greater than 100% or 100 are clamped to 100% at parsed-value time.
Also considering these examples:
lab(from color(xyz-d65 1 1 1) l a b)
lab(from lab(100 0 0) calc(l + 1) a b)
lab(from lab(100 0 0) calc(l +var(--l-1)) a b)
lab(calc(var(--l-100)+ 1) 0 0)
These all clamp in all implementations.
Does parsed-value time precede variable substitution?
The bearing of the second paragraph you quoted on this isn't at all obvious to me, even now that you've called it out. I'm happy enough to take "out-of-gamut lightness is a valid intermediate state" as a resolution on your say-so, but I think the current wording of the spec would make it very difficult for most people (even those fairly familiar with color spaces) to confidently deduce that color-mix(in lab, color(xyz-d65 1 1 1) 100%, black) is supposed to be lab(100.12 9.0645 5.8018) and not lab(100 9.0645 5.8018).
People should not need to take a resolution on my say-so; I made this explicit, and carefully differentiated between parse-time clamping (for historical reasons, mostly) and unclamped results (for round-tripping). Added some examples, too.
I fully agree that color space conversion must not lead to clamping.
But in RCS there is a parsing step after color space conversion.
Could you explain why there is a parsing step? There is nothing about serializing and then re-parsing; it all reads like in-memory color object manipulation, to me.
The spec also says
When relative color syntax is used, color component values, whether directly specified or arising from color space conversion, are not clamped to the reference ranges but are retained as-is. This preserves out of gamut values, if the destination color space is capable of representing them.
However, when relative color syntax is used, alpha component values whether directly specified or arising from color space conversion, are clamped to the reference range.
finish parsing the relative color, replacing component value keywords with their respective values
At least in my implementation the individual component value keywords indeed represent unclamped values. But the outcome goes through the same clamping steps as a non-RCS function. So you end up with clamping anyway.
I suspect that browser implementations are doing something similar.
I am also not quite sure how to implement this in a way that prevents clamping.
Do we then completely skip clamping in RCS?
For example: lab(from red 120% 0 0)
This doesn't use any of the original component values.
Does this clamp?
Or: lab(from color(xyz-d50 1 1 1) calc(l + 2) a b)
Here l should be 100% but the calc causes the value to go outside of the reference ranges.
In other words?
do we completely forgo clamping in RCS?
or do we mark component values if they contain a a component value keyword and only skip clamping for those?
(firefox and webkit are definitely incorrect as they do clamp the component value keywords!)
Edit: I am not arguing in favor of clamping :)
Fully agree that these values should round trip nicely and that clamping should be avoided.
Only unsure how to implement and trying to figure out what everyone is doing currently.
Activity
mirisuzanne commentedon Oct 31, 2023
@svgeesus Do you have a sense of when this clamping should happen?
svgeesus commentedon Oct 31, 2023
Let's see if the spec tells us. First, parsing directly specified colors:
Now, colors specified some other way but converted:
So: if I take
color(xyz-d65 1 1 1)and convert it to Lab offline and paste the result,lab(100.12 9.0645 5.8018)into my CSS then it will be clamped at parse time.But if I do
color-mix(in lab, color(xyz-d65 1 1 1) 100%, black)there will be no clamping until it comes time to display it on screen at used-value time.I appreciate that this sucks for CSS pre-processors.
nex3 commentedon Nov 3, 2023
The bearing of the second paragraph you quoted on this isn't at all obvious to me, even now that you've called it out. I'm happy enough to take "out-of-gamut lightness is a valid intermediate state" as a resolution on your say-so, but I think the current wording of the spec would make it very difficult for most people (even those fairly familiar with color spaces) to confidently deduce that
color-mix(in lab, color(xyz-d65 1 1 1) 100%, black)is supposed to belab(100.12 9.0645 5.8018)and notlab(100 9.0645 5.8018).nex3 commentedon Nov 14, 2023
I've been doing some additional poking around with this in relation to sass/sass#3737, and I'm a bit confused about some related behaviors in the latest draft of Color 4. This draft doesn't mention any kind of clamping at any point for
hsl()syntax (unlikergb()which clamps all its channels), and it says that "HSL colors resolve to sRGB". At the same time, the sample conversion algorithms explicitly say that they expects the saturation and lightness to be "in reference range[0,100]" and the red, green, and blue components to be in0..1. (For what it's worth I believe the RGB/HSL conversion algorithms do work as written to round-trip out-of-gamut colors, although the HWB algorithms do not.)Browsers (I've tested Chrome and Firefox) also do seem to clamp colors specified with
hsl().color-mix(in xyz, hsl(0, 0%, 500%) 50%, black)andcolor-mix(in xyz, hsl(0, 0%, 100%) 50%, black)both render ascolor(xyz 0.4752279635 0.5 0.5445288754), despite the fact that the former should have much higher lightness than the latter. Should this be considered a bug in the browsers' implementation (or just not adopting the latest spec yet)?This is particularly important for Sass, because we need a way to represent out-of-gamut legacy colors that's both compatible with older browsers and preserves the semantics of the out-of-bounds colors in modern browsers. We had originally been emitting
rgb()with out-of-gamut values, but it looks like that's now clamped, so I'm hoping that out-of-gamut HSL will work instead.(Edit: For what it's worth, my preference would be not to clamp
rgb(), orlab()for that matter, at parse-time so that the simple representation of out-of-gamut RGB colors works the same way as out-of-gamutcolor()values.)nex3 commentedon Jan 16, 2024
Pinging this again for the new year—getting a clear answer on this is currently blocking Sass's support for Color Level 4.
svgeesus commentedon Feb 9, 2024
Related:
hsl()has a parse-time clamp of negative saturation, when using legacy syntax. The above issue is about what to do withhsl()modern syntax, at parse time, where there is no interop.For
rgb()there is parse-time clamping in legacy syntax and in modern syntax, because of historical backlog (same with the 0 to 255 range) whilecolor()does not, because it does not have that web-compat burden.The reference range is used for converting percentage values to numbers. It does not imply that values outside that range are invalid.
I agree, they do.
If
rgb()was just now being deployed, that would be preferable but we have Web compat to consider. Which brings us to `lab() and friends. The CIE ones (for compatibility with industry practice, again) use a D50 white point and thus, very dark colors with unrealistically high chroma will tend to become negative on one side of the neutral axis because chromatic adaptation is effectively tilting that axis.nex3 commentedon Feb 12, 2024
But what about lightness? As specified, HSL does no clamping for lightness, but in practice browsers do clamp it as I described above.
Negative saturation is a much less critical concern for me, because (I believe)
hsl(h -s l)is always equivalent toHSL((h + 180) s l)anyway.In that case, is it a bug that the HWB conversions don't round-trip accurately if the percentages are outside
[0,100]?I understand why negative lightnesses can appear in Lab. But why are they clamped?
nex3 commentedon Feb 29, 2024
@svgeesus Friendly ping :). This is still blocking Sass's Color 4 implementation.
svgeesus commentedon Mar 4, 2024
@nex3 I am so sorry. I keep reading over this issue, reading over related issues, and trying to come up with a self-consistent set of principles for when clamping happens and why; but in some cases the "why" is "historical precedent from CSS Color 3" and stuff like that. So I don't form a coherent answer and leave it to come back to.
That must be very frustrating, my apologies.
The "in practice" is because CSS Color 3 puts two different things into one paragraph: parse-time clamping of HSL input and display-time clamping to the device gamut:
Because implementations at that time assumed all screens were sRGB, and also because
hsl()serializes asrgb()with only 8-bit precision, implementations of that time can't be blamed for condensing that into one operation on input.nex3 commentedon Mar 5, 2024
To be clear, I'm totally willing to have a solution here that doesn't work perfectly in browsers yet. If you can confirm that the intention of the current spec is that
hsl()should not clamp lightness, then we can make Sass's implementation target the spec and have a decent-but-not-great fallback for older browsers in which lightness is clamped, with the expectation that future browsers will do the right thing eventually. That's a lot better for us than having to emit acolor()function that doesn't work in older browsers at all!nex3 commentedon Mar 25, 2024
Since we need to move forward here with the Sass implementation, I'm going to operate on the assumption that the spec as currently written is intended—specifically in that a negative HSL lightness should not be clamped at any point—and that browsers will be expected to match this specification at some point in the future. If this assumption ends up being incorrect, please let me know as soon as possible.
romainmenke commentedon Jul 29, 2026
I think we need tests for this.
Current implementations:
https://codepen.io/romainmenke/pen/XJpyKXG?editors=1111
color-mix(in lab, color(xyz-d65 1 1 1), black)lab(50.0576 4.53418 2.89642)color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black)lab(50 4.53418 2.89642)color-mix(in lab, color(xyz-d65 1 1 1), black)lab(50 4.532248 2.900881)color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black)lab(50 4.532248 2.900881)color-mix(in lab, color(xyz-d65 1 1 1), black)lab(50 4.53225 2.90088)color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black)lab(50 4.53225 2.90088)Is Chrome correct here?
They clamp in relative color syntax.
Or should RCS also forgo clamping?
svgeesus commentedon Sep 8, 2026
color-mix(in lab, color(xyz-d65 1 1 1), black)lab(50.0576 4.53418 2.89642)color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black)lab(50 4.53418 2.89642)color-mix(in lab, color(xyz-d65 1 1 1), black)lab(50 4.532248 2.900881)color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black)lab(50 4.532248 2.900881)color-mix(in lab, color(xyz-d65 1 1 1), black)lab(50 4.53225 2.90088)color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black)lab(50 4.53225 2.90088)color-mix(in lab, color(xyz-d65 1 1 1), black)lab(50.058% 4.5322 2.9009)I added the result from the color.js color-mix() step by step app, which does no clamping. (It doesn't do RCS in that app, so I can't do the second example).
romainmenke commentedon Sep 8, 2026
Yes,
color-mix()definitely shouldn't have clamping.But RCS maybe does, or only in some cases?
https://drafts.csswg.org/css-color-4/#specifying-lab-lch
Also considering these examples:
These all clamp in all implementations.
Does
parsed-value timeprecede variable substitution?I get
lab(50 4.53224428 2.90088027)As a result of clamping during parsing of the RCS
lab()function.Not as a result of color space conversion.
[css-color-4] Clarify that intermediate values from color space conve…
svgeesus commentedon Sep 8, 2026
People should not need to take a resolution on my say-so; I made this explicit, and carefully differentiated between parse-time clamping (for historical reasons, mostly) and unclamped results (for round-tripping). Added some examples, too.
Please take a look.
nex3 commentedon Sep 8, 2026
The new wording seems much clearer to me now 🙂.
svgeesus commentedon Sep 9, 2026
The color space conversion is XYZ to Lab, which also involves a D65 to D50 Bradford CAT.
romainmenke commentedon Sep 9, 2026
Maybe I wasn't expressing myself clearly :)
I have two questions:
parsed-value time? e.g. when usingvar()I fully agree that color space conversion must not lead to clamping.
But in RCS there is a parsing step after color space conversion.
svgeesus commentedon Sep 9, 2026
Could you explain why there is a parsing step? There is nothing about serializing and then re-parsing; it all reads like in-memory color object manipulation, to me.
The spec also says
romainmenke commentedon Sep 9, 2026
In my implementation the steps are roughly:
At least in my implementation the individual component value keywords indeed represent unclamped values. But the outcome goes through the same clamping steps as a non-RCS function. So you end up with clamping anyway.
I suspect that browser implementations are doing something similar.
I am also not quite sure how to implement this in a way that prevents clamping.
Do we then completely skip clamping in RCS?
For example:
lab(from red 120% 0 0)This doesn't use any of the original component values.
Does this clamp?
Or:
lab(from color(xyz-d50 1 1 1) calc(l + 2) a b)Here
lshould be100%but thecalccauses the value to go outside of the reference ranges.In other words?
romainmenke commentedon Sep 9, 2026
A concrete example/illustration of what implementations seem to do today:
lab(from color(xyz-d65 1 1 1) calc(l / 2) a b)If the
lcomponent value keyword was clamped you would expect the outcome to have anlvalue of50.Whereas both my implementation and chrome result in:
50.058l50.0576505050.05771898(firefox and webkit are definitely incorrect as they do clamp the component value keywords!)
Edit: I am not arguing in favor of clamping :)
Fully agree that these values should round trip nicely and that clamping should be avoided.
Only unsure how to implement and trying to figure out what everyone is doing currently.
svgeesus commentedon Sep 9, 2026
Thanks for the explanation. This needs more discussion, could you move it to a new issue with a better title?
svgeesus commentedon Sep 26, 2026
Closing as the original issue is resolved and the remaining concerns are in a new issue.