←back to thread

855 points tontonius | 2 comments | | HN request time: 0.427s | source
Show context
chrismorgan ◴[] No.45011025[source]
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.

replies(12): >>45011178 #>>45011340 #>>45012378 #>>45012990 #>>45013291 #>>45013348 #>>45014196 #>>45015512 #>>45018962 #>>45019530 #>>45023600 #>>45040825 #
anotheryou ◴[] No.45023600[source]
RGB 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

Also super hard in RGB: 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%
replies(2): >>45023618 #>>45023969 #
1. chrismorgan ◴[] No.45023969[source]
That 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.

replies(1): >>45024628 #
2. anotheryou ◴[] No.45024628[source]
you 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