Gradients in non-sRGB color spaces are inaccurately approximated on steep parts
Categories
(Core :: Web Painting, defect)
Tracking
()
People
(Reporter: timo.mikkolainen, Unassigned)
Details
Attachments
(1 file)
|
1.82 KB,
text/html
|
Details |
User Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:159.0) Gecko/20100101 Firefox/159.0
Steps to reproduce
Created a gradient in a non-sRGB space (srgb-linear) with a short, steep part (inline below).
<div style="width:512px;height:48px;background:linear-gradient(to right in srgb-linear, black 252px, white 260px)"></div>
<div style="width:512px;height:48px;overflow:hidden"><div style="width:512px;height:48px;transform-origin:256px 0;transform:scaleX(64);background:linear-gradient(to right in srgb-linear, black 252px, white 260px)"></div></div>
<div style="width:512px;height:48px;background:linear-gradient(to right in srgb-linear, black, white)"></div>
Actual results
The short, sharp change in the gradient doesn't quite look as it should (compared to other browsers & what I think it should be):
there seem to be kinks, and the values are off quite a bit.
The same happens in oklab, oklch, lch and the other non-sRGB spaces: the shorter the segment, the larger the error.
Expected results
The gradient smoothly transitions between the colors in the given color space.
See attachment for reproduction.
Comment 1•29 days ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::Graphics: WebRender' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.
Comment 2•24 days ago
|
||
Some info from claude:
The kinks come from how Firefox approximates interpolation in non-sRGB color spaces. WebRender only interpolates gradients linearly between stops, so for other spaces layout converts each segment between two color stops into a run of extra stops, computed in the requested space. This happens in ColorStopInterpolator::CreateStops (layout/painting/ColorStopInterpolator.h).
The number of extra stops in a segment is proportional to that segment's share of the whole gradient line: 128 across the full range, with a minimum of 1. In the first testcase the black 252px to white 260px segment covers 8/512 of the gradient, so it gets floor(260/512128) - floor(252/512128) = 65 - 63 = 2 extra stops. The transition is drawn as three straight pieces, which matches the kinks in the zoomed screenshot. It also explains why shorter segments show larger errors in every non-sRGB space.
This is not a regression; it's how non-sRGB gradients have been implemented since they shipped.
The fix belongs in layout. WebRender evaluates the stops it receives exactly for each pixel and accepts up to 720 stops per gradient (MAX_GRADIENT_STOPS), so giving each segment more stops directly reduces the error. Evenly spaced stops are an inefficient way to get there, though. In a rough model, black to white in srgb-linear is still about 5 levels (out of 255) off near black with 64 evenly spaced pieces, while placing the stops adaptively gets under half a level with about 20 pieces. For a few color pairs in srgb-linear and oklab, adaptive placement needed 11 to 38 pieces per segment for that accuracy, so gradients of up to about 18 segments would fit in 720 stops; longer ones would have to share the budget. The comment above kFullRangeExtraStops saying WebRender re-encodes gradients to 128 pairs of stops is out of date.
| Reporter | ||
Comment 3•24 days ago
|
||
I actually noticed this issue whilst writing a cubic Bézier gradient approximator, so I have some input to give on the approximation:
If you're interpolating between points on the curve (whether it's even x distribution, even t distribution or curvature based), the result ends up biased. Think of your typical S-shaped curve (e.g. ease-in-out): each section at x<0.5 will overshoot the curve between the stops (it 'skips the valley'), and each section at x>0.5 will undershoot it (it 'cuts through the hill').
You can avoid the bias by fitting the interpolated points outside the curve, but that will cost you some milliseconds (depending on the method & end goal/metric).
In either case, you'll produce some sort of error.
It might end up being cheaper to just evaluate each sample properly.
Description
•