Repository navigation
Expand file tree
/
Copy path03-palette.html
More file actions
298 lines (288 loc) · 19.6 KB
/
Copy path03-palette.html
File metadata and controls
298 lines (288 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Palette — inkvec</title>
<link rel="stylesheet" href="assets/doc.css">
</head>
<body>
<div class="wrap">
<header class="masthead">
<p class="eyebrow">Stage 3 of 16</p>
<h1>Deciding how many colours a picture actually has</h1>
<p class="standfirst">Not every distinct-looking pixel is a distinct ink, and not every
pair of similar pixels is the same ink. This stage decides the palette by asking whether
each candidate colour earns its keep — not by clustering pixels or picking a fixed
distance and hoping it fits everywhere.</p>
<p class="pairlink">Technical reference: <a href="03-palette.md">03-palette.md</a></p>
</header>
<main>
<section>
<h2>A question with two ways to get it wrong</h2>
<p>Before anything about shapes or curves can be decided, the tracer needs to know: how
many distinct colours — inks — did the artist actually use? This sounds simple and is
not, because a picture never presents its palette cleanly. Every soft edge sprinkles in
blend colours that are not really inks at all, and some genuinely different inks — two
similar shades of pale pink, say — sit very close together in colour.</p>
<p>Get the count wrong in either direction and it shows:</p>
<div class="plain">
<h3>Too few colours</h3>
<p>Merge two inks the artist meant to keep separate, and the region loses detail — or
worse, the tracer notices something is not quite flat there and invents a gradient to
explain the leftover difference, when the truth was simply two flat colours side by
side.</p>
</div>
<div class="plain">
<h3>Too many colours</h3>
<p>Mint an ink for every faint ripple a compressed file leaves near its edges, or for
every step of an anti-aliased ramp, and a drawing with five real colours comes out with
dozens — each one becoming its own shape, its own outline, its own path in the final
file.</p>
</div>
<p>This is a genuine judgement call, not a lookup. It is what statisticians call a
<b>model selection</b> problem — deciding how complex a description to use — rather than
a clustering problem. Clustering asks "which pixels look similar." Model selection asks
"does adding this colour to my description actually pay for itself, given how much of
the picture it explains and how much noise there is to explain away."</p>
</section>
<section>
<h2>Why a fixed ruler does not work</h2>
<p>The obvious approach is to pick a colour space where distance roughly matches what a
human eye perceives, and declare two colours "the same ink" whenever they sit within a
fixed distance. This mostly works — until it meets the colour space's own quirks.</p>
<p>The space used here (OKLab) makes lightness a cube root of brightness, which is
deliberate and mostly helpful — human vision is far more sensitive to differences among
dark tones than among light ones, so building that sensitivity into the space keeps a
single ruler roughly fair. But taken to its extreme, right down near black, that same
cube root stretches distances enormously. A handful of the very darkest, almost
imperceptible one-pixel-level differences near black can measure as farther apart, in
this space, than two visibly different mid-tones. A clean black logo with no colour
variation at all was, at one point, read as four separate near-black inks purely because
of this stretch — differences nobody looking at the picture could ever see.</p>
<figure>
<svg viewBox="0 0 640 220" role="img" aria-label="A ruler that measures distances near black as far larger than the same visual gap near white">
<text x="20" y="24" class="fig-label-strong">The same ruler, two very different readings</text>
<g transform="translate(20,55)">
<text x="0" y="-10" class="fig-label">Near black</text>
<rect x="0" y="0" width="40" height="40" fill="#000000" stroke="var(--rule)"/>
<rect x="55" y="0" width="40" height="40" fill="#070707" stroke="var(--rule)"/>
<line x1="42" y1="20" x2="53" y2="20" class="fig-accent-stroke" stroke-width="3"/>
<text x="47" y="10" text-anchor="middle" class="fig-label" fill="var(--copper)">huge in OKLab</text>
<text x="47" y="65" text-anchor="middle" class="fig-label">invisible to the eye</text>
</g>
<g transform="translate(260,55)">
<text x="0" y="-10" class="fig-label">Mid grey</text>
<rect x="0" y="0" width="40" height="40" fill="#7d7d7d" stroke="var(--rule)"/>
<rect x="55" y="0" width="40" height="40" fill="#858585" stroke="var(--rule)"/>
<line x1="42" y1="20" x2="53" y2="20" class="fig-accent-stroke" stroke-width="3"/>
<text x="47" y="10" text-anchor="middle" class="fig-label" fill="var(--copper)">smaller in OKLab</text>
<text x="47" y="65" text-anchor="middle" class="fig-label">but this one you CAN see</text>
</g>
</svg>
<figcaption>The same numeric gap in OKLab means very different things depending on where
it sits. A single fixed threshold cannot serve both cases at once.</figcaption>
</figure>
<p>So the stage does not trust one ruler alone. It keeps a second, independent check —
CIEDE2000, a colour-difference formula built specifically to match what people can
actually distinguish — and uses it as a hard floor: below a small CIEDE2000 gap, two
colours are declared the same ink no matter what any other test says, because no argument
about "the artwork probably meant this" can rescue a difference nobody can see.</p>
</section>
<section>
<h2>Does this colour earn its keep?</h2>
<p>For every candidate colour, ordered from most to least common, the stage asks a version
of the same question a scientist asks when deciding whether to add a new term to a model:
does keeping this separate explain the picture enough better to be worth the extra
complexity?</p>
<p>Concretely: if this candidate were folded into the nearest colour already accepted, how
much error would that introduce across every pixel that would have chosen it? And is that
error bigger than the fixed cost of writing one more colour into the file? A colour with
thousands of pixels sitting well clear of anything already accepted almost always wins —
the error saved by keeping it separate dwarfs the small fixed cost of adding it. A colour
with a handful of pixels a hair's breadth from an existing one almost always loses — not
worth a whole new entry for barely any benefit.</p>
<figure>
<svg viewBox="0 0 640 240" role="img" aria-label="A weighing scale, one side the cost of a new colour, one side the error saved by keeping it separate">
<text x="20" y="24" class="fig-label-strong">Is a new ink worth its keep?</text>
<g transform="translate(320,60)">
<line x1="0" y1="0" x2="0" y2="130" class="fig-rule" stroke-width="3"/>
<line x1="-220" y1="30" x2="220" y2="30" class="fig-rule" stroke-width="3"/>
<!-- left pan: cost of a new ink -->
<g transform="translate(-180,30)">
<line x1="0" y1="0" x2="0" y2="45" class="fig-rule"/>
<ellipse cx="0" cy="55" rx="70" ry="16" fill="var(--copper-soft)" stroke="var(--copper)"/>
<text x="0" y="60" text-anchor="middle" class="fig-label">fixed cost of one new ink</text>
</g>
<!-- right pan: residual saved -->
<g transform="translate(180,30)">
<line x1="0" y1="0" x2="0" y2="65" class="fig-rule"/>
<ellipse cx="0" cy="80" rx="80" ry="20" fill="var(--paper-raised)" stroke="var(--ink)"/>
<text x="0" y="76" text-anchor="middle" class="fig-label">error saved,</text>
<text x="0" y="90" text-anchor="middle" class="fig-label">across every pixel that would choose it</text>
</g>
<text x="0" y="-15" text-anchor="middle" class="fig-label-strong">kept only if the right pan is heavier</text>
</g>
</svg>
<figcaption>The fixed cost never changes. What changes is how many pixels the candidate
would explain and how far from an alternative they'd otherwise sit — both measured
against the noise actually present in this particular image.</figcaption>
</figure>
<p>The comparison is scaled by how noisy the image is. In a very clean, exact render, even
a small colour gap is trustworthy evidence, because there is nothing else that could have
caused it. In an image known to have been resampled, upscaled or compressed, the same
small gap is much less trustworthy — it could easily be an artefact — so the bar for
keeping a nearby candidate separate is raised.</p>
</section>
<section>
<h2>Telling a blend from a real pale colour</h2>
<p>Every soft edge between two inks produces a ramp of in-between pixels — a coverage
blend, not a colour choice. Left unchecked, that ramp would mint its own family of
spurious inks. But a genuinely pale, real colour — a pastel pink, say — sits in exactly
the same part of colour space as a blend between white and red. Colour position alone
cannot tell the two apart. Shape can.</p>
<p>An anti-aliasing ramp is a thin band running along a boundary: almost none of its pixels
have every neighbour also in the band, because the band is only a pixel or two wide. A
real ink, even a small one, covers area — most of its pixels are safely surrounded by more
of the same colour. This stage measures exactly that: what fraction of a candidate's
pixels are "interior," meaning every neighbour also belongs to it.</p>
<figure>
<svg viewBox="0 0 640 220" role="img" aria-label="A thin ramp of blend pixels along a boundary compared with a solid patch of real ink, only the solid patch has interior pixels">
<text x="20" y="24" class="fig-label-strong">A ramp has no interior. A patch does.</text>
<g transform="translate(30,50)">
<text x="0" y="-8" class="fig-label">Anti-aliasing ramp (boundary)</text>
<g>
<rect x="0" y="0" width="26" height="26" fill="var(--ink)"/>
<rect x="26" y="0" width="26" height="26" fill="#4b3a26"/>
<rect x="52" y="0" width="26" height="26" fill="#a6926f"/>
<rect x="78" y="0" width="26" height="26" fill="var(--paper)"/>
<rect x="0" y="26" width="26" height="26" fill="var(--ink)"/>
<rect x="26" y="26" width="26" height="26" fill="#4b3a26"/>
<rect x="52" y="26" width="26" height="26" fill="#a6926f"/>
<rect x="78" y="26" width="26" height="26" fill="var(--paper)"/>
</g>
<text x="0" y="72" class="fig-label" fill="var(--copper)">no pixel is fully surrounded by its own shade</text>
</g>
<g transform="translate(360,50)">
<text x="0" y="-8" class="fig-label">Real pale ink (a patch)</text>
<g>
<rect x="0" y="0" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
<rect x="26" y="0" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
<rect x="52" y="0" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
<rect x="0" y="26" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
<rect x="26" y="26" width="26" height="26" fill="var(--copper)" stroke="var(--rule)"/>
<rect x="52" y="26" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
<rect x="0" y="52" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
<rect x="26" y="52" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
<rect x="52" y="52" width="26" height="26" fill="#e3c9c9" stroke="var(--rule)"/>
</g>
<text x="0" y="98" class="fig-label" fill="var(--copper)">the centre pixel is surrounded on all sides</text>
</g>
</svg>
<figcaption>Same colour, opposite verdicts — because the question being asked is about
shape, not hue.</figcaption>
</figure>
<p>There is one more wrinkle. A real ink drawn only two or three pixels wide is thin enough
that it, too, has almost no fully-surrounded interior — the interior test alone cannot
tell it apart from a ramp. For that narrow case, the stage asks one more question: does
this colour's neighbourhood actually straddle the two colours it supposedly blends —
having, close by, a neighbour clearly nearer one and a neighbour clearly nearer the other?
A genuine coverage ramp always does. A real thin stroke of its own colour usually does
not: it touches its neighbours on either side but is not caught between them. This
distinction was needed after a real case where a stroke's shadow — a thin brown line that
happened to be a mathematical mix of the shapes on either side of it — was nearly deleted
as if it were only an anti-aliasing artefact.</p>
</section>
<section>
<h2>A halo is not a colour</h2>
<p>When a picture has been enlarged by an image editor, its edges ring: the resizing filter
overshoots, and a thin halo slightly brighter (or darker) than the shape runs along every
edge. The Studio's own sample, a gold crest, has one: a one-pixel rim about 7 % brighter
than the gold all around every shape. That rim is close enough to the gold that the "is it
worth its keep" question above, which counts pixels, said yes — there are thousands of rim
pixels. So the crest came out as three golds, and its round dots came out notched where the
rim had been cut away as a shape of its own.</p>
<p>The fix asks of such a near-duplicate what the blend test already asks of a blend: does it
cover area, or is it only a band? A rim has no pixel surrounded by its own colour, so it is
not an ink. The crest now comes out as one gold with round dots, in a third of the file
size. A thin colour that is a mix of its neighbours keeps its own, stricter test — the
shadow-stroke case above — because asking it the same question cost skin-tone shading in a
family of emoji.</p>
</section>
<section>
<h2>Small but real</h2>
<p>The oldest rule in this stage was a size floor: a colour covering less than about half a
percent of the picture was never an ink, because a blend colour on an edge is always rare.
It also threw away small colours that were nothing like a blend — a pupil, a red mouth, a
yellow star — which then got painted in a neighbour's colour.</p>
<p>A small candidate now has to show something instead: enough of its pixels (8 on a
128-pixel picture, proportionally more on a larger one) must be colours that no mix of the
inks around them explains, and that are not an edge halo of those inks either. A pupil
passes: nothing around it mixes to its colour. A sliver of anti-aliasing fails: every pixel
of it is a mix of the two colours it sits between. On the standard test icons this made the
colour error 2 % smaller at 128 pixels and 1.4 % smaller at 512.</p>
</section>
<section>
<h2>One ink, one colour code</h2>
<p>After the palette is decided, each shape gets its colour from its own pixels, and two
shapes of one ink can come out a shade or two apart — the crest's gold was written as two
slightly different codes. Shapes of one ink whose colours differ by less than anyone could
see now all take the colour of that ink's biggest, best-measured shape. The rule is kept that
tight on purpose: when it was looser, a white page and a very slightly grey white arrow —
which the artist really did draw as two whites — were made one colour, and the whole page
took the arrow's tint.</p>
</section>
<section>
<h2>Telling clean input from damaged input</h2>
<p>How strict the palette should be depends on where the picture came from. An exact,
cleanly rendered image can be trusted down to fine colour differences — if two nearby
shades appear, the artist probably meant two shades. A picture that has been resized,
blurred, or run through a compressed image format cannot be trusted the same way — its
soft edges and its compression ripples both invent colours nobody chose.</p>
<p>This stage does not try to guess that from the colours it is sorting; instead it is
told, explicitly, by three measurements made before it starts: how wide the picture's
edges measure (a resized or blurred image has wider edges than a native render), what file
format the picture was saved in (some compressed formats are known to damage flat areas
without widening edges at all), and whether the pixels just beside the edges ripple the
way compression leaves them — which catches a compressed picture that was saved again in
a format that keeps every pixel exactly, where the file format alone would say "clean". All three
are described in the previous stage. Only when one of them says "this input is not clean"
does the palette loosen its tolerance for near-duplicate colours and the confidence it
grants to small colour gaps.</p>
<div class="aside">This has to stay conditional rather than always-on. Running the looser,
more forgiving settings on a genuinely clean image was measured to cost real quality —
about an eleven percent drop on a standard set of test icons — because on clean input a
small colour gap really is the artist's choice, and blurring that distinction away throws
artwork out along with the artefacts it was meant to catch.</div>
</section>
<section>
<h2>What this stage hands forward</h2>
<p>The result is a short list of colours — the recovered palette — plus, for each pixel in
the picture, which of those colours it has been assigned to. Every later stage builds on
top of this decision: the shapes that get outlined, the boundaries that get measured, and
the gradients that get fitted are all defined in terms of these inks. Get the count right
here, and everything downstream has the right raw material to work with.</p>
</section>
</main>
<nav class="stages">
<a href="00-overview.html">Overview</a>
<a href="01-intake.html">1 Intake</a>
<a href="02-coverage.html">2 Coverage</a>
<a href="03-palette.html" aria-current="page">3 Palette</a>
<a href="04-regions.html">4 Regions</a>
<a href="05-gradients.html">5 Gradients</a>
<a href="06-planar-map.html">6 Planar map</a>
<a href="07-subpixel.html">7 Sub-pixel</a>
<a href="08-boundary-solve.html">8 Boundary solve</a>
<a href="09-decode.html">9 Decode</a>
<a href="10-symmetry.html">10 Symmetry</a>
<a href="11-fitting.html">11 Curve fitting</a>
<a href="12-repair.html">12 Repair</a>
<a href="13-emit.html">13 Emit</a>
<a href="14-fast-mode.html">14 Fast mode</a>
<a href="15-strokes.html">15 Strokes</a>
<a href="constants.md">Constants</a>
</nav>
</div>
</body>
</html>