←back to thread

855 points tontonius | 9 comments | | HN request time: 0s | source | bottom
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 #
djoldman ◴[] No.45012990[source]
But 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?

replies(2): >>45013072 #>>45015813 #
layer8 ◴[] No.45013072[source]
A 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.
replies(1): >>45013444 #
cubefox ◴[] No.45013444[source]
One could also argue that the detour through other hues is necessary in this case to avoid going through grey.
replies(1): >>45013622 #
layer8 ◴[] No.45013622[source]
Gray 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.
replies(7): >>45013704 #>>45014617 #>>45015125 #>>45016009 #>>45018125 #>>45018860 #>>45024190 #
1. brulard ◴[] No.45014617[source]
Let's say you continuously change wavelength of a laser from blue (~480nm) to red (~630nm), you are going through green, not through gray. If in your use case going through gray makes sense, that's ok, there may be many paths from one color to another.
replies(2): >>45015320 #>>45016520 #
2. Sharlin ◴[] No.45015320[source]
In general people don't really think of color in terms of the spectral progression (or the hue wheel), and I don't think that most people intuitively expect a gradient between two colors to pass through another "unrelated" primary or secondary color. The point is somewhat moot though, given that such gradients (like yellow to blue or red to green) are very unnatural anyway.
replies(3): >>45017636 #>>45018017 #>>45018195 #
3. JKCalhoun ◴[] No.45016520[source]
Shine a red and a green light near each other on a wall — what does the transition look like?
replies(1): >>45017085 #
4. ZoomZoomZoom ◴[] No.45017085[source]
There's no transition, this is color mixing, or overlay in case of light.
replies(1): >>45017746 #
5. runarberg ◴[] No.45017636[source]
Honestly I suspect this is largely a non issue. I have never made a gradient that goes through more than 2 different color (by some vague measure of different) without adding an additional stop. If I wanted to go through yellow and green to get to blue, I would add a stop at yellow and another at green, and I suspect most developers would do the same.
6. JKCalhoun ◴[] No.45017746{3}[source]
Is that not a transition through the color mixing (or overlay). I'm assuming the light sort of tails off as you leave the area of one color and head to the other (and the other color comes on with more intensity then).

I suppose that's different with light than some analog with pigments? (Two dabs of color set apart, a brush perhaps used to blend them as continuously as is possible.)

replies(1): >>45020778 #
7. gowld ◴[] No.45018017[source]
It's funny and a bit sad because we just went througha decades-long effort to migrate away from jet/rainbow gradients to vik/batlow/bi-hue gradients, and now rainbow is forcing its way back.

https://theconversation.com/how-rainbow-colour-maps-can-dist...

https://www.poynter.org/archive/2013/why-rainbow-colors-aren...

8. itishappy ◴[] No.45018195[source]
I disagree somewhat. Color mixing just isn't particularly intuitive. It's not the most intuitive to get a third hue, but that doesn't justify grey (which has an undefined hue). I do think most people are quite comfortable with the fact that between blue and yellow exists green, but is it a saturated green or a desaturated green? Additive and subtractive color mixing behave very differently here.
9. cubefox ◴[] No.45020778{4}[source]
Your light thought experiment would produce a color gradient via additive color mixing.

A magenta to green gradient would then go through white rather than grey. A subtractive magenta-green gradient would go through black. Not sure what physical setup would produce the latter gradient. But the standard RGB (or OKLAB) gradient goes through grey rather than white or black. This type of gradient is physically created by dithering: Dithering a gradient from magenta to green, by just using these two base colors, would produce a perceptual grey in the middle. This type of color mixing is otherwise better known as alpha blending.