Skip to content

[css-color-4] How to handle conversions that result in negative lightness? #9484

Closed

Description

@nex3

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.

Activity

  1. mirisuzanne commented on Oct 31, 2023

    @mirisuzanne
    Contributor

    @svgeesus Do you have a sense of when this clamping should happen?

  2. svgeesus commented on Oct 31, 2023

    @svgeesus
    Collaborator

    Let's see if the spec tells us. First, parsing directly specified colors:

    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.

  3. nex3 commented on Nov 3, 2023

    @nex3
    ContributorAuthor

    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).

  4. nex3 commented on Nov 14, 2023

    @nex3
    ContributorAuthor

    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.)

  5. nex3 commented on Jan 16, 2024

    @nex3
    ContributorAuthor

    Pinging this again for the new year—getting a clear answer on this is currently blocking Sass's support for Color Level 4.

  6. svgeesus commented on Feb 9, 2024

    @svgeesus
    Collaborator

    Related:

    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.

  7. nex3 commented on Feb 12, 2024

    @nex3
    ContributorAuthor

    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?

  8. nex3 commented on Feb 29, 2024

    @nex3
    ContributorAuthor

    @svgeesus Friendly ping :). This is still blocking Sass's Color 4 implementation.

  9. svgeesus commented on Mar 4, 2024

    @svgeesus
    Collaborator

    @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.

  10. nex3 commented on Mar 5, 2024

    @nex3
    ContributorAuthor

    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!

  11. nex3 commented on Mar 25, 2024

    @nex3
    ContributorAuthor

    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.

  12. romainmenke commented on Jul 29, 2026

    @romainmenke
    Member

    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:

    https://codepen.io/romainmenke/pen/XJpyKXG?editors=1111

    source computed
    chrome color-mix(in lab, color(xyz-d65 1 1 1), black) lab(50.0576 4.53418 2.89642)
    chrome color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black) lab(50 4.53418 2.89642)
    safari color-mix(in lab, color(xyz-d65 1 1 1), black) lab(50 4.532248 2.900881)
    safari color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black) lab(50 4.532248 2.900881)
    firefox color-mix(in lab, color(xyz-d65 1 1 1), black) lab(50 4.53225 2.90088)
    firefox 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?

  13. svgeesus commented on Sep 8, 2026

    @svgeesus
    Collaborator
    source computed
    chrome color-mix(in lab, color(xyz-d65 1 1 1), black) lab(50.0576 4.53418 2.89642)
    chrome color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black) lab(50 4.53418 2.89642)
    safari color-mix(in lab, color(xyz-d65 1 1 1), black) lab(50 4.532248 2.900881)
    safari color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black) lab(50 4.532248 2.900881)
    firefox color-mix(in lab, color(xyz-d65 1 1 1), black) lab(50 4.53225 2.90088)
    firefox color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black) lab(50 4.53225 2.90088)
    colorjs 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).

  14. romainmenke commented on Sep 8, 2026

    @romainmenke
    Member

    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

    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?


    I get lab(50 4.53224428 2.90088027)
    As a result of clamping during parsing of the RCS lab() function.

    color-mix(in lab, lab(from color(xyz-d65 1 1 1) l a b), black)
                      ^^^^                          ^^^^^^
    

    Not as a result of color space conversion.

  15. svgeesus commented on Sep 8, 2026

    @svgeesus
    Collaborator

    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.

    Please take a look.

  16. nex3 commented on Sep 8, 2026

    @nex3
    ContributorAuthor

    The new wording seems much clearer to me now 🙂.

  17. added
    Closed Accepted as Editorial
    Commenter Satisfied
    on Sep 9, 2026
  18. svgeesus commented on Sep 9, 2026

    @svgeesus
    Collaborator

    Not as a result of color space conversion.

    The color space conversion is XYZ to Lab, which also involves a D65 to D50 Bradford CAT.

  19. romainmenke commented on Sep 9, 2026

    @romainmenke
    Member

    Maybe I wasn't expressing myself clearly :)


    I have two questions:

    • do we expect clamping in RCS?
    • are there cases where we should be able to observe that clamping happens at parsed-value time? e.g. when using var()

    I fully agree that color space conversion must not lead to clamping.
    But in RCS there is a parsing step after color space conversion.

  20. svgeesus commented on Sep 9, 2026

    @svgeesus
    Collaborator

    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.

  21. romainmenke commented on Sep 9, 2026

    @romainmenke
    Member

    In my implementation the steps are roughly:

    • start parsing a relative color
    • parse the origin color
    • 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?
  22. romainmenke commented on Sep 9, 2026

    @romainmenke
    Member

    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 l component value keyword was clamped you would expect the outcome to have an l value of 50.

    Whereas both my implementation and chrome result in: 50.058

    implementation l
    chrome 50.0576
    firefox 50
    webkit 50
    csstools 50.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.

  23. svgeesus commented on Sep 9, 2026

    @svgeesus
    Collaborator

    Thanks for the explanation. This needs more discussion, could you move it to a new issue with a better title?

  24. svgeesus commented on Sep 26, 2026

    @svgeesus
    Collaborator

    Closing as the original issue is resolved and the remaining concerns are in a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closed Accepted as EditorialCommenter Satisfiedcss-color-4

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      Sponsor
      SponsoredKunjungi sekarang
      Promo