6 ms·
The “Better Gradients” thing is dodgy. OKLCH is a polar coordinate space. Hue is angle in this space. So to interpolate hue from one angle to another, to get f
by chrismorgan 1y ago
The “Better Gradients” thing is dodgy.
OKLCH is a polar coordinate space. Hue is angle in this space. So to interpolate hue from one angle to another, to get from one side of a circle to the other, you go round the edge. This leads to extreme examples like the one shown:
linear-gradient(in oklch, #f0f, #0f0)
You can also go round the circle the other way, which will take you via blue–aqua instead of via red–yellow:
linear-gradient(in oklch longer hue, #f0f, #0f0)
The gradient shown (in either case) is a good example of a way that perceptual colour spaces are really bad to work in: practically the entire way round the edge of the circle, it’s outside sRGB, in fact way outside of the colours humans can perceive. Perceptual colour spaces are really bad at handling the edges of gamuts, where slightly perturbing the values take you out of gamut.
Accordingly, there are algorithms defined (yes, plural: not every application has agreed on the technique to use) to drag the colour back in-gamut, but it sacrifices the perceptual uniformity. The red in that gradient is way darker than the rest of it.
When you’re looking for better gradients, if you’re caring about perceptual uniformity (which frequently you shouldn’t, perceptual colour spaces are being massively overapplied), you should probably default to interpolating in Oklab instead, which takes a straight line from one side of the circle to the other—yes, through grey, if necessary.
linear-gradient(in oklab, #f0f, #0f0)
And in this case, that gets you about as decent a magenta-to-lime gradient as you can hope for, not going via red and yellow, and not exhibiting the inappropriate darkening of sRGB interpolation (… though if I were hand-tuning such a gradient, I’d actually go a bit darker than Oklab does).
During its beta period, Tailwind v4 tried shifting from sRGB to Oklch for gradient interpolation; by release, they’d decided Oklab was a safer default.
- animal531 1y ago>During its beta period, Tailwind v4 tried shifting from sRGB to Oklch for gradient interpolation; by release, they’d decided Oklab was a safer default. I recently implemented both, first I started with OKLab which turned out really well, the gradients you get from it are amazing and the usual color sets (analogous etc.) produce really pleasing sets. However I quickly ran into the main problem with it, which is that fiddling with its Lightness, Chroma and Hue dials doesn't produce human understandable results. For example sometimes changing L or C induces a color shift, or for some given values changing L only gives midrange values that doesn't go up or down all the way. I then implemented OKLCH on top of that, which was the way I assumed everyone was doing it. Just have it act as the controller for the human layer, then convert to OKLab for creating gradients etc. The article doesn't really go into it, but having OKLCH as the frontend controller fixes the LCH sliders such that they produce values that make sense to us humans, while still having the superior OKLab setup in the back.
- ta8645 1y agoVery interesting. Is this just a limitation of our current hardware? How much of this problem would still exist if everyone had a wider gamut monitor, say full DCI-P3? That still doesn't cover the full gamut of Oklch, but would it make the problem practically disappear?
- chrismorgan 1y agoNo. We’re talking about colours way beyond the ranges of human perception. For this specific gradient, see https://oklch.com/#0.7017,0.3225,328.36,100 https://oklch.com/#0.7017,0.3225,328.36,100 and https://oklch.com/#0.86644,0.294827,142.4953,100 https://oklch.com/#0.86644,0.294827,142.4953,100, and look at the Chroma panel, see how far out of our screen gamuts they are (even tick “Show Rec2020”, which adds a lot of chroma around blue–green and magenta–red), and try to imagine the colours between the lime and magenta (in either direction). The red direction is probably the easier to reason about: there’s just no such colour as a light, bright red. You can have bright or light, but not both. (Its 3D view can also be useful to visualise these things: you’re building a straight-line bridge between two peaks, and there’s a chasm in between.)
- ta8645 1y agoBut once an algorithm to drag the colours back in-gamut was applied, would the lost perceptual uniformity still be a problem practically speaking, with DCI-P3 monitors?
- chrismorgan 1y agoYes. I repeat: these colours are way outside gamut. Not just a little bit. P3 helps a little bit, Rec.2020 would help a fair bit more, but you’re still asking for a yellow that is about twice as vibrant as is possible.
- shrx 1y agoI don't get it, why am I seeing the "out of gamut" colors if my sRGB monitor is unable to display them? Would the charts look different on a P3 monitor? edit: Also, you mentioned the colors "beyond the ranges of human perception" but I don't think there is any such limitation here, the bottleneck is the hardware (computer monitors).
- Vingdoloras 1y agoFor anyone else copy pasting the gradients into dev tools to look at them: The second one is missing the # sign on the first color. And yes, both oklch gradients look pretty weird while the oklab gradient looks nice (if you can accept it going through grey).
- chrismorgan 1y agoSorry, fixed.
- hammock 1y agoCan anyone share some images for those who don’t have dev tools to view them?
- itishappy 1y agohttps://codepen.io/ItIsHappy/pen/dPYjyaV https://codepen.io/ItIsHappy/pen/dPYjyaV
- Diggsey 1y agoAlso, isn't the way browsers interpolate colors in sRGB just a bug that I assume is retained for backwards compatibility? sRGB is a logarithmic encoding, you were never supposed to interpolate between colors directly in that encoding - the spec says you're suppose to convert to linear RGB first and do the interpolation there...
- spoiler 1y agoIt's not a bug, its a property of the colour space. Which is partially tied to how the colour is represented (RGB). When doing linear interpolation through the RGB cube (for eg a gradient), you normally pick the shortest path. It just so happens that sometimes that path passes thorough some shade of gray as different colour components are scaled. Usually you fix it by moving your point through a different colour space. Choice depends on your requirements and mediums you're working with (normally different types of light sources or screens). I had to write a low level colour interpolation librar for a few interactive art projects, so I dipped a bit into this, but I'm no colour expert
- deleted 1y ago[deleted]
- Sharlin 1y agoI think GP is referring to the difference between "normal" (gamma-encoded) sRGB and linear sRGB. Though it's not logarithmic but a power law. In any case linear interpolation done in non-linear sRGB gives you intermediate colors that are darker than they should (though historically it's been so common in computer graphics that people are accustomed to it).
- Diggsey 1y agoNo, sRGB refers to both a colour space and an encoding of that colour space. The encoding is non-linear to make best use of the 256 levels available per channel, but you were never supposed to interpolate sRGB by linearly interpolating the encoded components: you're supposed to apply the transfer function, perform the linear interpolation at higher precision, and then convert back down into the non-linear encoding. Failure to do this conversion is what leads to the bad results when interpolating: going from red to green will still go through grey but it should go through a much lighter grey compared to what happens if you get the interpolation wrong.
- djoldman 1y agoBut what really is a "color gradient"? Isn't it any continuous function that starts at a specified color and ends at another specified color? How then does one say that any gradient is good or bad? Isn't the problem you are highlighting guaranteed to exist for any colorspace that defines colors outside of human perception?
- layer8 1y agoA good gradient is one that takes a perceptually uniform, and typically perceptually shortest, path. The OKLCH gradient isn't perceptual uniform and appears to take unnecessary detours through other hues.
- cubefox 1y agoOne could also argue that the detour through other hues is necessary in this case to avoid going through grey.
- layer8 1y agoGray is arguably just another color, it’s not clear why you’d want to avoid it. How is going via red and yellow better than going via gray? Varying hue is often perceived as a larger change than varying saturation or lightness. A path going through several distinct hues is visually less uniform than one going through gray once.
- the_af 1y agoIs gray perceived as "just another color" or where all colors go when desaturated? I assumed the latter, which would explain why to avoid it if one isn't playing with saturation.
- layer8 1y agoThink of fashion, of smartphone colors, pen and pencil colors and the like. Gray, white, black, are just color choices among all the available colors. A gray T-shirt isn’t a desaturated colored T-shirt. It’s its own color.
- jakubkrehel 1y agoThis is super insightful, thank you! I wrote the article and will make changes to explain this better :)
- cubefox 1y ago> The red in that gradient is way darker than the rest of it. It doesn't look significantly darker to me.
- FuriouslyAdrift 1y agoWe already have CIE LAB as a standard that does the same thing... https://en.wikipedia.org/wiki/CIELAB_color_space https://en.wikipedia.org/wiki/CIELAB_color_space [EDIT] Ahh.. The W3C has already looked at this. https://www.w3.org/Graphics/Color/Workshop/slides/talk/lilley https://www.w3.org/Graphics/Color/Workshop/slides/talk/lille...
- ucarion 1y agoFor others, I'm sure parent knows: OKLCH is largely a bugfix for CILEAB. Both try to make a color space where even steps feel evenly spaced to a human. But CIELAB had procedural flaws in its creation. See slide 19: https://www.w3.org/Graphics/Color/Workshop/slides/talk/lilley https://www.w3.org/Graphics/Color/Workshop/slides/talk/lille... -- if you ask CIELAB to make "pure blue" (RGB 0 0 100%) become grayscale, the intermediate colors become purple to the human eye. The entire point of a perceptual color space is that that doesn't happen. OKLCH fixes that. BTW, credit to Björn Ottosson, who basically side-projected a color space into the web standards and more: https://bottosson.github.io/posts/oklab/ https://bottosson.github.io/posts/oklab/ ... folks like him are why we sometimes have nice things!
- nkrisc 1y agoIMO in pretty much all cases if the two colors at the end of your gradient are not already very close, you will always get the best results by manually specifying the colors at one or more steps between the two original colors. Which brings me to point at the crux of the matter: avoid gradients between two dissimilar colors in the first place.
- stevage 1y agoMaking a gradient from two colours on opposite sides of the spectrum is a bit of a pathological case. There's no obvious answer what it "should" look like, and also no good reason you'd really need to do this. If you're using a gradient as a design element, you'd pick your own midpoints.
- omnicognate 1y agoEdit: The article was updated. Ignore me. > you should probably default to interpolating in Oklab instead The article says as much. Quoting: > This can be a double edged sword. While some gradients might look smoother, you might also see colors that you've never defined. This is because hue in OKLCH is circular and gradients can take unexpected detours. > To avoid this, many tools use OKLAB for gradients, which interpolates in a straight line and gives more consistent results.
- jayrhynas 1y agoI was going to point that out too until I realized that the author updated the article based on the parent comment! https://news.ycombinator.com/item?id=45013291 https://news.ycombinator.com/item?id=45013291
- omnicognate 1y agoHa! Well spotted.
- anotheryou 1y agoRGB is just allways worst OKLCH usually pretty, which is all I want from gradients most of the time. I had claude build me a comparison (not sure why 2x the same HSL one) https://i.imgur.com/uziQibR.png https://i.imgur.com/uziQibR.png Also super hard in RGB: https://jsfiddle.net/nhgvzm5p/2/ https://jsfiddle.net/nhgvzm5p/2/ it's just a 2 color OKLCH gradient: oklch(0 0.07 279) 66%, oklch(0.98 0.09 276) 99%
- anotheryou 1y agomy biggest problems with OKLCH: - how to easily add "warmth". you can't just add red+green - no good tools for it (e.g. a nice gradient picker ui that lets me specify if I want to allow running out of bounds of teh color space and clip a few colors at max saturation)
- chrismorgan 1y agoThat gradient looks terrible, very bad colour banding, because you’re starting and ending at points that are way out of gamut, and the technique used to bring it in isn’t smooth. For a gradient like that, you probably want at least five points. That is the biggest problem with these colour spaces: the edges are unclear, and overflowing them has bad effects.
- anotheryou 1y agoyou think I can get the same OKLCH style colors but without banding? if I sample 5 colors and throw them in as rgb? the 3 color rgb one has very similar banding