You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Proposal: gradient paints (Paint::Gradient) as a color-fitting stage
#127
The Paint enum notes that it "leaves room for gradients and patterns later". I have a
working implementation of the gradient half against 0.6.4 and I'd like to offer it upstream,
ported to the 1.0 architecture. The port needs one small change to the ColorFitter contract,
so I wanted to agree the design before writing it rather than after.
Why it is worth doing
Smooth ramps are the case the cluster frontend handles worst. layer_difference slices a ramp
into flat bands, and every band is then filled with one solid color. You can buy accuracy back
by lowering gradient_step, but only by paying for it in layers.
Fitting an actual gradient to each region's source pixels moves that whole tradeoff. On an
abstract gradient wallpaper (1358×882), spline mode, color_precision 8, everything else at
defaults. RMSE against the source raster, and raw vtracer output size:
gradient_step
fitting
RMSE
bytes
8
off
0.0106
1,563,243
8
on
0.0094
1,595,820
16
off
0.0155
744,524
16
on
0.0109
775,207
32
off
0.0315
250,591
32
on
0.0183
260,974
64
off
0.0562
88,780
64
on
0.0337
91,644
At a fixed step, fitting costs about 4% more bytes and removes 15% to 40% of the error. The
diagonal matters more: fitting at step 16 matches no-fitting at step 8 to within 3% RMSE at
half the file size. Fewer, smarter layers beat more, dumber ones.
What it does
For each region, fit the source pixels under its mask:
Linear: least-squares color plane per channel, with the ramp axis as the top eigenvector
of the 2×2 Gram matrix, in closed form.
Radial: isotropic quadratic fit a + b·x + d·y + e·(x² + y²), so the centre falls out as (−b/2e, −d/2e).
Both are cheap and closed-form. Whichever explains more variance wins, and neither is used
unless it clears a variance-explained floor and beats a flat fill, so flat regions stay flat and
nothing regresses on artwork that has no gradients in it.
Two details cost me more than the fitting itself:
Fit only the pixels the region actually shows. Fitting over a region's full extent,
including the parts later layers paint over, made results worse than flat fills (0.048 vs
0.038 RMSE on my test image). Fitting on visible pixels only: 0.023.
Share one gradient across the bands of a sliced ramp. The band boundaries are an artifact
of layer_difference, not of the image, so giving adjacent bands one shared gradient makes
the seams stop showing. This overlaps conceptually with MergeAdjacent, and with what mosaic
mode does for geometry.
Suggested design
Paint::Gradient(GradientId), with the gradients in a side table on Segmentation and VectorDoc, rather than inlining stops in the enum. Three reasons:
Paint stays Copy + Eq, so write_grouped's <g fill> run-grouping keeps working
untouched. fill="url(#g3)" groups exactly like a hex color does.
It matches how SVG already models this. A gradient is a <defs> entry referenced by id.
Two regions sharing one gradient (point 2 above) is then just two Paints holding the same
id, and the writer emits one <defs> entry.
The one contract change.ColorFitter::fit(&self, seg: &mut Segmentation) has no access to
the source pixels, and Pipeline::finish is deliberately built to re-run phase 2 without the
image so the interactive tuning loop can cache a Segmentation. Gradient fitting fundamentally
needs those pixels.
I'd suggest carrying the source on the Segmentation (e.g. Arc<ColorImage>) rather than
adding an image parameter to fit. That keeps the trait signature and the two-phase caching API
exactly as they are, and makes a Segmentation self-describing in the same way the docs say a RegionMask already is. If you'd rather change the trait signature, that works too. It's your
call, and it's the only reason I'm asking first.
What I'd send
A PR adding colorfit::FitGradients, the Paint and writer changes above, and CLI flags, with
unit tests for the fitters (I have 12 against the current implementation) and a golden or two.
Happy to split it: paint plumbing first, then the fitter, then the shared-gradient pass.
If gradients are already on your roadmap for 1.0 and you'd rather do them yourself, say so and
I'll leave it. No hard feelings either way, and the numbers above are yours to use regardless.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
The
Paintenum notes that it "leaves room for gradients and patterns later". I have aworking implementation of the gradient half against 0.6.4 and I'd like to offer it upstream,
ported to the 1.0 architecture. The port needs one small change to the
ColorFittercontract,so I wanted to agree the design before writing it rather than after.
Why it is worth doing
Smooth ramps are the case the cluster frontend handles worst.
layer_differenceslices a rampinto flat bands, and every band is then filled with one solid color. You can buy accuracy back
by lowering
gradient_step, but only by paying for it in layers.Fitting an actual gradient to each region's source pixels moves that whole tradeoff. On an
abstract gradient wallpaper (1358×882), spline mode,
color_precision 8, everything else atdefaults. RMSE against the source raster, and raw vtracer output size:
gradient_stepAt a fixed step, fitting costs about 4% more bytes and removes 15% to 40% of the error. The
diagonal matters more: fitting at step 16 matches no-fitting at step 8 to within 3% RMSE at
half the file size. Fewer, smarter layers beat more, dumber ones.
What it does
For each region, fit the source pixels under its mask:
of the 2×2 Gram matrix, in closed form.
a + b·x + d·y + e·(x² + y²), so the centre falls out as(−b/2e, −d/2e).Both are cheap and closed-form. Whichever explains more variance wins, and neither is used
unless it clears a variance-explained floor and beats a flat fill, so flat regions stay flat and
nothing regresses on artwork that has no gradients in it.
Two details cost me more than the fitting itself:
including the parts later layers paint over, made results worse than flat fills (0.048 vs
0.038 RMSE on my test image). Fitting on visible pixels only: 0.023.
of
layer_difference, not of the image, so giving adjacent bands one shared gradient makesthe seams stop showing. This overlaps conceptually with
MergeAdjacent, and with what mosaicmode does for geometry.
Suggested design
Paint::Gradient(GradientId), with the gradients in a side table onSegmentationandVectorDoc, rather than inlining stops in the enum. Three reasons:PaintstaysCopy + Eq, sowrite_grouped's<g fill>run-grouping keeps workinguntouched.
fill="url(#g3)"groups exactly like a hex color does.<defs>entry referenced by id.Paints holding the sameid, and the writer emits one
<defs>entry.The one contract change.
ColorFitter::fit(&self, seg: &mut Segmentation)has no access tothe source pixels, and
Pipeline::finishis deliberately built to re-run phase 2 without theimage so the interactive tuning loop can cache a
Segmentation. Gradient fitting fundamentallyneeds those pixels.
I'd suggest carrying the source on the
Segmentation(e.g.Arc<ColorImage>) rather thanadding an image parameter to
fit. That keeps the trait signature and the two-phase caching APIexactly as they are, and makes a
Segmentationself-describing in the same way the docs say aRegionMaskalready is. If you'd rather change the trait signature, that works too. It's yourcall, and it's the only reason I'm asking first.
What I'd send
A PR adding
colorfit::FitGradients, thePaintand writer changes above, and CLI flags, withunit tests for the fitters (I have 12 against the current implementation) and a golden or two.
Happy to split it: paint plumbing first, then the fitter, then the shared-gradient pass.
If gradients are already on your roadmap for 1.0 and you'd rather do them yourself, say so and
I'll leave it. No hard feelings either way, and the numbers above are yours to use regardless.
All reactions