Palettes from histogram peaks
How Logo and Illustration decide which colors the image has
(packages/inlay/src/palette.ts).
The problem with quantizers
Section titled “The problem with quantizers”A quantizer groups similar pixels and averages each group. On flat art that average drifts: anti-aliased edge pixels between gold and navy pull the gold group towards navy. On the sample badge the gold sun came out pale peach, plainly wrong in the Difference view and wrong enough to break a brand color.
A second surprise: a color cap equal to the number of colors is too low. With a cap of 6 on a 5-color badge, anti-aliased shades took slots and the red mountain and green hill merged into one brown (8.75% of pixels wrong). A cap of 8 fixed it (1.39%). That’s why Logo defaults to 8, and why the Colors hint says to raise the cap if two colors merge.
Peaks instead of averages
Section titled “Peaks instead of averages”Flat art’s real colors are sharp peaks in its histogram; anti-aliasing is a thin spread of low counts between them. So:
- Build a histogram at 5 bits per channel, keeping each bin’s true mean color.
- Walk the bins from most to least frequent. Accept a bin as a new color only if it is perceptually distinct (OKLab distance ≥ 0.07) from every color already accepted and holds a meaningful share of the image.
- Stop at the color cap.
That recovered the sample’s colors almost exactly (mean error 3.28 → 1.47) and cut mean error on a real illustration from 11.1 to 4.3.
Peaks spread over several bins
Section titled “Peaks spread over several bins”Counting each bin alone failed on a product photo: a gray ship with red lettering and yellow decals came out black, white, grays and one blue, 6 colors at a cap of 8 or 64. A photographed color spreads over many neighboring bins, none of them 0.2% of the image, and the walk stopped at the first such bin. So:
- A bin’s weight is every pixel within half the distinct-color distance of it, and only local peaks (no bin nearby holds more pixels) are candidates, so a flat color is still its own bin, not a neighbor that borrowed its weight.
- Below 0.2%, down to 0.05%, a color is a detail: it’s kept unless it lies on the line between two colors already picked (mixed in sRGB, as a renderer mixes an anti-aliased edge), which is what edge shades are.
On that photo Illustration went from 6 colors to 12, red and yellow among
them (mean error 7.79 → 5.98); the clean-art scores (npm run score) are
unchanged. Weighing alone shifted the badge’s colors onto neighboring bins
(0.54% → 0.80% of pixels wrong) until candidates were limited to peaks; a flat
0.05% floor let edge shades in (0.77%) until blends were excluded.
Pale tints and flat pixels
Section titled “Pale tints and flat pixels”Peaks missed the fills of an outline illustration: thin blue outlines on white, the shapes filled with pale tints of the same blue. A pale fill is 0.03 from white in OKLab, under the 0.07 that makes a color distinct, and lies on the line from white to the blue, so it read as an edge between them. Meanwhile the outlines’ own anti-aliasing was plentiful enough (past 0.2%) to take a slot as a color.
What tells a fill from an edge is that a fill is flat: its pixels match their
four neighbors (within 8 per channel), and an anti-aliased edge’s never do.
Given the image’s width, peakPalette counts flat pixels per bin, then:
- a blend of two picked colors that is almost never flat (under 5% of its pixels) is an edge and is left out, at any share under 2% (above that, 1 px rules and dithered fills, never flat either, are colors of their own);
- spare slots go first to close fills: flat colors left out for being under
0.07 from a picked one. A truck logo’s neutral-gray grille bars and tire
treads were 0.057 from its blue-gray cab, so they traced as the cab, smeared
with navy. Each round takes the heaviest spot of flat pixels still more than
0.035 from every color picked, if it covers 0.2% of the image (never under
0.1%). Only flat art gets them (80% of its pixels flat or more; photos are
0.52 to 0.73, and there they grew Illustration’s SVG by up to half). Pale
spots are left to the tints below, and a spot on the line through two nearby
picked colors is a gradient’s shade, which would only band it, so it is
passed over. A shade between the two close colors (the truck’s bumper)
would then flip between them in blotches, so after labeling, a region of
the pair that is blotched with both takes whichever is nearer its mean
(
settleClosePairsin inlay.ts); - then to flat tints: peaks of flat pixels alone, at least 0.02 from every color picked, flat over at least 0.1% of the image, and pale (OKLab lightness 0.85 or more). A photo’s smooth sky or shadow is flat too, and without the lightness bar its dark shades filled Illustration’s colors with near-copies, doubling the SVG.
On the outline fixture in npm run score Logo went from 13.0% to 2.1% of
pixels wrong; the badge’s palette is unchanged.
Where it doesn’t apply
Section titled “Where it doesn’t apply”Photos have no peaks. On a photo the same change made things worse (9.9 →
15.3), so Poster quantizes instead: quantizePalette runs k-means in OKLab
over a color histogram, each bin weighted by its pixel count. It’s seeded
deterministically (the most common color, then the color farthest from
those chosen, weighted by how common it is), so a photo traces the same way
every time, and each color reported is the mean of its cluster’s pixels, so
it sits in the middle of the shades it stands for. The rule is per-preset because the measurements
said so.
How the tracer uses it
Section titled “How the tracer uses it”The Inlay labels each pixel with its nearest palette color in OKLab. Pixels that sit between two colors are decided by their neighbors, which keeps slivers of in-between colors off every edge.