inlaytrace 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,36 @@
1
1
  # inlaytrace
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - d37e56b: `stacked: true` now works for flat art too: shapes come back to front, each whole, with the parts hidden behind other shapes completed (a disc behind a bar traces as a whole disc). Which shape is in front is read from the junctions where two shapes' shared edge ends. Photos (`photo: true`) stack as before.
8
+ - b1c2494: Flat outline illustrations (thin coloured outlines on white, pale tinted fills) now trace. Speck clean-up folds a speck into its nearest colour, and no longer dissolves a thin stroke that lossy compression split into two shades of its ink. `peakPalette` takes a new `width` option: given it, pale tints are found by their flatness (new options `minTint`, `minFlat`), and a colour that is only ever an anti-aliased edge is left out. Slivers under 2 px wide fold into a neighbour of nearly the same colour. New option `photo` keeps the old clean-up for photos (specks join the colour they share the most edge with, no sliver pass), which is what a stacked `quantizePalette` trace wants. Binary and centerline modes take `threshold: 'auto'` (Otsu, falling back to 128 on a page with nothing to split). A thin closed shape (a 3 px letter stem) no longer collapses to a line, and the fitter no longer grows spikes on thin letters. On a new outline fixture, Logo went from 13.0% to 2.1% of pixels wrong; the badge, emblems and photos (with `photo`) score the same.
9
+ - 1d9df88: New `strokes` option for flat outline art: in palette mode, the colours drawn mostly as thin, long lines are traced down their middle as one stroked path each (`InlayPath.stroke` is the width measured from the ink), with their anti-aliased edges and darker shades joined in, and the shapes either side meet beneath them. Small lettering, short marks and solid shapes stay shapes. `scale` says how far the image was enlarged, so the line widths scale with it. Off by default; output without it is unchanged.
10
+
11
+ `toSvg` and `centerlineToSvg` now escape the attribute values they write, so a palette colour given with a quote in it can't end its attribute.
12
+ - 1719316: `peakPalette` finds colours spread over several histogram bins (a photo's red lettering, a small decal) instead of stopping at the greys; flat art's palettes are unchanged. New option `minDetail`. Stacked shapes now reach under the neighbours painted after them, so no hairline seam shows where two shapes meet (12% closer on photos, about a third more path data).
13
+ - 2cf20c5: Lines within a degree of horizontal, vertical or 45° are now exactly that, so a square stays square after JPEG and lines either side of a junction line up. `fitChain` takes `snap: false` to keep lines as fitted.
14
+
15
+ ### Patch Changes
16
+
17
+ - 56f4604: `centerline(rgba, width, height, options)` (and `traceCenterline`, `centerlineToSvg`): single strokes down the middle of the ink, for pen plotters and engraving, with the stroke width measured from the ink or set.
18
+ - b7fa938: Add a funding link (Ko-fi) to the package metadata.
19
+ - fb90904: New `gradients` option: soft shading that a palette splits into bands of tints is traced as one shape with a linear or radial gradient (`path.gradient`, written by `toSvg` as `<linearGradient>` / `<radialGradient>`), when the gradient fits much better than a flat colour. Off by default.
20
+ - e462e9b: Lines about a pixel wide stay in one piece at a slant, instead of breaking into dashes or vanishing.
21
+ - 1ff571f: Centerline and outline-art strokes run straight through junctions. Thinning bends the skeleton toward each junction, so an X of thick strokes came out pinched at its crossing and a T's stem bent to meet the bar; within a stroke width of a junction the arms are now cut back and drawn straight on to where their own lines meet.
22
+ - 6e9a2d0: Small text and dots beside flat art now trace. Thin anti-aliased strokes (1.5 px grey letters) were labelled as the paper, pale edges as a nearby colour, and each letter then cleared as a speck. In flat art (not `photo`), a pixel between colours and close to none of them is now read as a mix of the pair that explains it best; one next to only one other colour keeps its own; and a speck that stands well apart from every colour around it stays from an eighth of the speck area up, so raising `speckle` still clears it. Poster output is unchanged.
23
+ - 3660a64: In images with exactly flat colours, such as most PNG exports, edges are placed between pixels from their anti-aliasing instead of on the pixel grid: straight sides land where they really are, and small shapes come out cleaner (2–16% fewer wrong pixels on the PNG fixtures, for the same nodes). Whether an image qualifies is judged from its pixels, so typical JPEG and WebP traces and Poster are unchanged.
24
+ - 62d79d3: `toSvg` escapes the drawing's width and height like every other value it writes.
25
+ - 4b20b95: Lines one or two pixels wide keep their width to their ends. Where such a line's outline turned back at its end inside one fitted run (an arm of a T or a Y), its two sides were joined at a point, so each arm tapered to nothing and thin outlines looked broken.
26
+
27
+ ## 0.1.3
28
+
29
+ ### Patch Changes
30
+
31
+ - f71beed: New `shapes` option: a closed outline that is a circle, an ellipse, a rectangle, a rounded rectangle or a regular polygon is drawn as that exact shape instead of fitted curves (on the sample badge, 0.70% → 0.54% wrong pixels). Off by default.
32
+ - 074cc75: `detectSymmetry(rgba, width, height, options?)` finds the likeliest mirror axes (vertical and horizontal) and rotational orders (2–8 by default) in an image, each scored by the share of the drawing that matches its reflection or turn.
33
+
3
34
  ## 0.1.2
4
35
 
5
36
  ### Patch Changes
package/README.md CHANGED
@@ -9,8 +9,8 @@ curves. Pixel art keeps its exact staircase.
9
9
  Pure TypeScript with no DOM and no WebAssembly: it runs in browsers, Web
10
10
  Workers and Node. MIT licensed.
11
11
 
12
- It is the tracer behind the [Inlay app](https://inlaytrace.com/app/)
13
- for logos, illustrations, line art and pixel art.
12
+ It is the tracer behind every preset of the [Inlay app](https://inlaytrace.com/app/),
13
+ from logos and pixel art to photos.
14
14
 
15
15
  ## Install
16
16
 
@@ -42,7 +42,8 @@ const result = trace(rgba, width, height, {
42
42
  cornerAngle: 45, // turns sharper than this stay corners
43
43
  })
44
44
 
45
- result.paths // [{ d: 'M…Z', fill: '#rrggbb' }, …] largest first, nonzero fill
45
+ result.paths // [{ d: 'M…Z', fill: '#rrggbb' }, …] largest first, nonzero fill;
46
+ // with `strokes`, lines last: { d, fill, stroke: width }, stroked in `fill`, unfilled
46
47
  toSvg(result) // '<svg …>…</svg>'
47
48
  ```
48
49
 
@@ -50,11 +51,14 @@ toSvg(result) // '<svg …>…</svg>'
50
51
 
51
52
  | Options | For |
52
53
  | --- | --- |
53
- | `mode: 'palette', palette` | Flat colour art. `palette` is the colours to label pixels with: `peakPalette(rgba, n)` takes the sharp peaks of the histogram (flat art's real colours); `exactPalette(rgba, n)` takes the image's exact colours, most-used first. |
54
+ | `mode: 'palette', palette` | Flat colour art. `palette` is the colours to label pixels with: `peakPalette(rgba, n, { width })` takes the sharp peaks of the histogram (flat art's real colours), and given the image's width also pale tints found by their flatness (a light fill on white); `exactPalette(rgba, n)` takes the image's exact colours, most-used first. |
54
55
  | `mode: 'palette', palette, merge` | Fold colours together before regions are built, so the edge between them disappears: `{ '#from': '#into' }`. |
55
56
  | `mode: 'palette', palette: exactPalette(…), pixels: true` | Pixel art: no clean-up of lone pixels, no curve fitting; every boundary is its pixel staircase with straight runs joined. |
56
- | `mode: 'palette', palette: quantizePalette(…), stacked: true` | Photos as flat colour bands. `quantizePalette(rgba, n)` clusters the image's colours (k-means in OKLab, deterministic); `stacked` draws each shape whole, larger first, instead of cutting holes. |
57
- | `mode: 'binary', threshold` | Line art: ink darker than `threshold` (0–255), or adaptive per neighbourhood when `null`. Only the ink is drawn. |
57
+ | `mode: 'palette', palette, strokes: true` | Outline art: colours drawn mostly as thin, long lines (2.5–8 px wide; give `scale` if the image was enlarged) are traced down their middle as round-capped strokes at the measured width, and the shapes either side meet beneath them. Short marks, small lettering and solid shapes stay shapes. |
58
+ | `mode: 'palette', palette, gradients: true` | Soft shading as gradients: neighbouring tints that are bands of one smooth shade become one shape with a linear or radial gradient (`path.gradient`; `fill` is its average colour), when the gradient fits far better than a flat colour. For screens and print, not cutters. |
59
+ | `mode: 'palette', palette, stacked: true` | Flat art as layers: each shape whole, back to front. Which of two touching shapes is in front comes from the junctions where their edge ends, and a shape partly hidden is completed underneath (a circle, a corner, or a smooth continuation), so a disc behind a bar is a whole disc. |
60
+ | `mode: 'palette', palette: quantizePalette(…), stacked: true, photo: true` | Photos as flat colour bands. `quantizePalette(rgba, n)` clusters the image's colours (k-means in OKLab, deterministic); `stacked` draws each shape whole, larger first, instead of cutting holes. |
61
+ | `mode: 'binary', threshold` | Line art: ink darker than `threshold` (0–255), the image's own ink/paper split when `'auto'` (Otsu; coloured ink works too), or adaptive per neighbourhood when `null`. Only the ink is drawn. |
58
62
 
59
63
  ### Also exported
60
64
 
@@ -63,6 +67,12 @@ toSvg(result) // '<svg …>…</svg>'
63
67
  Béziers between them).
64
68
  - `parsePath(d)` / `serializePath(subpaths)`: path data as editable nodes with
65
69
  absolute handles, and back. Round trips are idempotent after the first pass.
70
+ - `centerline(rgba, width, height, options)` / `traceCenterline(…)`: single
71
+ strokes down the middle of the ink, for pen plotters and engraving, with the
72
+ stroke width measured from the ink (or set).
73
+ - `detectSymmetry(rgba, width, height)`: the likeliest mirror axes and
74
+ rotational orders in an image, each scored by how much of the drawing
75
+ matches (the app offers to enforce one at 95% or more).
66
76
  - `oklab(rgb)`: the perceptual colour space the palette and labelling use.
67
77
 
68
78
  ## How it works
@@ -92,8 +102,9 @@ every export.
92
102
  ## Measured
93
103
 
94
104
  On a 640 px JPEG of a five-colour badge, scored by rendering at 4× against the
95
- clean source art: 0.70% wrong pixels with 177 nodes, against 0.85% with 271
96
- nodes for VTracer tracing each colour on its own. At 2048 px it traces in about
105
+ clean source art: 0.54% wrong pixels with 175 nodes with `shapes` on (0.70%
106
+ and 177 without), against 0.85% with 271 nodes for VTracer tracing each colour
107
+ on its own. At 2048 px it traces in about
97
108
  1.3 s (VTracer: 17.5 s, timed by hand). On pixel art both draw every pixel exactly; Inlay uses
98
109
  about 14% fewer nodes and is about 4× faster on a 1024 px sprite.
99
110
 
package/dist/index.d.ts CHANGED
@@ -42,6 +42,16 @@ type FitOptions = {
42
42
  cornerAngle: number;
43
43
  /** Half-window in px of the smoothing over the pixel staircase (default 12). */
44
44
  smoothing?: number;
45
+ /**
46
+ * Draw a closed outline that is a circle, an ellipse, a (rounded) rectangle
47
+ * or a regular polygon as that exact shape (see shapes.ts). Default false.
48
+ */
49
+ shapes?: boolean;
50
+ /**
51
+ * Make lines within 1° of horizontal, vertical or 45° exactly that (see
52
+ * `snapLines`). Default true; exact shapes are never snapped.
53
+ */
54
+ snap?: boolean;
45
55
  };
46
56
  /** A cubic Bézier: start, two handles, end. A straight line has handles on the chord. */
47
57
  type Cubic = [Pt, Pt, Pt, Pt];
@@ -50,11 +60,101 @@ export declare function isFlat([p0, c1, c2, p3]: Cubic): boolean;
50
60
  /**
51
61
  * Fit a chain. Open: from points[0] to the last point, which stay fixed.
52
62
  * Closed: points[0] is not repeated at the end; the result is a loop that starts
53
- * at a corner if there is one.
63
+ * at a corner if there is one. `fine`, if given, is the same chain placed more
64
+ * exactly (between pixels) and is what gets fitted, exact shapes included;
65
+ * `points` stays the pixel outline that the area check measures.
54
66
  */
55
- export declare function fitChain(points: Pt[], closed: boolean, o: FitOptions): Cubic[];
67
+ export declare function fitChain(points: Pt[], closed: boolean, o: FitOptions, fine?: Pt[]): Cubic[];
56
68
  export declare function pointAt(b: Cubic, t: number): Pt;
57
69
  //#endregion
70
+ //#region src/palette.d.ts
71
+ /**
72
+ * Palette extraction for flat artwork.
73
+ *
74
+ * VTracer's own quantizer averages each cluster, so a flat colour that borders
75
+ * a lighter one drifts towards it: on the sample badge the gold sun (#e9a23b)
76
+ * came out pale peach (#e9c38f) — wrong enough to break a brand colour. Flat
77
+ * art has a property quantizers ignore: its real colours are sharp *peaks* in
78
+ * the histogram, and everything between them (anti-aliasing, JPEG ringing) is
79
+ * a thin spread of low-count shades. So instead of clustering, take the peaks:
80
+ *
81
+ * 1. Histogram at 5 bits per channel, keeping each bin's true mean colour.
82
+ * 2. Weigh each bin by the pixels within half the distinct-colour distance of
83
+ * it (OKLab), so a colour spread over several bins counts in full.
84
+ * 3. Walk bins from heaviest to lightest; accept a bin as a new colour only
85
+ * if it is perceptually distinct (OKLab distance) from every colour already
86
+ * accepted, and its weight is a meaningful share of the image. A smaller
87
+ * share (a decal, lettering) is still a colour if it isn't a blend of two
88
+ * colours already accepted (an anti-aliased edge between them).
89
+ * 4. Stop at the requested count.
90
+ * 5. Given the image's `width`, fill what's left of the count with pale tints:
91
+ * light flat colours (pixels that match their four neighbours) too close to a
92
+ * picked colour for step 3. A light-blue fill on white is 0.03 from it in
93
+ * OKLab and lies on the line from white to the blue ink, so step 3 calls
94
+ * it an edge between the two; but an edge is never flat, and a fill is.
95
+ *
96
+ * This is for logos and illustrations. Photos have no peaks (on a photo the
97
+ * peaks raised mean error from 9.9 to 15.3), so they use `quantizePalette`.
98
+ */
99
+ type Rgb = [number, number, number];
100
+ export declare function peakPalette(rgba: ArrayLike<number>, maxColors: number, { minDistance, minShare, minDetail, width, minTint, minFlat }?: {
101
+ minDistance?: number;
102
+ minShare?: number;
103
+ minDetail?: number;
104
+ width?: number;
105
+ minTint?: number;
106
+ minFlat?: number;
107
+ }): string[];
108
+ export declare function oklab([r, g, b]: Rgb): Rgb;
109
+ /**
110
+ * A photo's palette: `maxColors` colours that best summarise it, by k-means in
111
+ * OKLab (so the clusters are perceptual, not RGB boxes).
112
+ *
113
+ * It clusters a 5-bit-per-channel histogram, each bin weighted by its pixel
114
+ * count, rather than the pixels themselves: the same answer to within a bin,
115
+ * at a fraction of the work. It's deterministic: the first centre is the most
116
+ * common colour, and each next one is the colour farthest from those chosen,
117
+ * weighted by how common it is (k-means++ without the dice), so a photo traces
118
+ * the same way every time. Each colour returned is the true mean of the pixels
119
+ * in its cluster, most common first. `maxColors` is rounded and taken as at
120
+ * least 1 (so 0 or NaN gives one colour); fewer come back when the image has
121
+ * fewer distinct colours, and none when every pixel is transparent.
122
+ */
123
+ export declare function quantizePalette(rgba: ArrayLike<number>, maxColors: number, { iterations }?: {
124
+ iterations?: number;
125
+ }): string[];
126
+ /**
127
+ * Pixel art's palette: its colours exactly, most-used first. Pixel art has a
128
+ * handful of deliberate colours, and a one-pixel eye matters as much as the
129
+ * sky, so nothing is merged or dropped for being rare or close to another.
130
+ * Beyond `maxColors` distinct colours (a photo picked as pixel art), the
131
+ * rarest ones take their nearest kept colour when pixels are labelled.
132
+ */
133
+ export declare function exactPalette(rgba: ArrayLike<number>, maxColors: number): string[];
134
+ //#endregion
135
+ //#region src/gradient.d.ts
136
+ type GradientStop = {
137
+ offset: number;
138
+ color: string;
139
+ };
140
+ /** Coordinates in the trace's pixels (gradientUnits="userSpaceOnUse"). */
141
+ type Gradient = {
142
+ type: 'linear';
143
+ x1: number;
144
+ y1: number;
145
+ x2: number;
146
+ y2: number;
147
+ stops: GradientStop[];
148
+ } | {
149
+ type: 'radial';
150
+ cx: number;
151
+ cy: number;
152
+ r: number;
153
+ stops: GradientStop[];
154
+ };
155
+ /** A gradient's colour at a point. */
156
+ export declare const colorAt: (g: Gradient, x: number, y: number) => Rgb;
157
+ //#endregion
58
158
  //#region src/inlay.d.ts
59
159
  type InlayOptions = FitOptions & {
60
160
  /** Regions smaller than speckle × speckle pixels are merged into their surroundings. */
@@ -67,15 +167,51 @@ type InlayOptions = FitOptions & {
67
167
  pixels?: boolean;
68
168
  /**
69
169
  * Stacked shapes, for photos: each region is drawn whole, without the holes
70
- * other regions sit in, and larger shapes come first so smaller ones paint
71
- * over them. Every hole's outline is exactly the outline of what fills it,
72
- * so the picture is the same as cut-out shapes, with less path data (each
73
- * hole isn't drawn a second time) and no hairline seams where anti-aliased
74
- * edges meet. A hole with transparency anywhere inside it, however deep, is
170
+ * other regions sit in, larger shapes first so smaller ones paint over them,
171
+ * and reaching under every neighbour painted after it. So each shared edge
172
+ * is drawn once, by the later shape over the earlier one's colour, and no
173
+ * hairline seam shows where anti-aliased edges meet (on photos, 12% closer
174
+ * to the photo than without, for about a third more path data). A hole with transparency anywhere inside it, however deep, is
75
175
  * kept, so transparency stays transparent. Default: cut out, which is what a
76
176
  * vinyl cutter or a screen printer wants.
177
+ *
178
+ * For flat art (not `photo`) it means layers instead: shapes in front-to-back
179
+ * order, each whole. Which of two touching shapes is in front is read from
180
+ * the junctions where their boundary ends, and a shape behind another is
181
+ * completed under it (its circle, its corner, or a smooth continuation),
182
+ * so a moon behind a bar is one disc. Holes that later shapes fill are
183
+ * dropped, as for photos.
77
184
  */
78
185
  stacked?: boolean;
186
+ /**
187
+ * Photos: specks join the colour they share the most edge with, and slivers
188
+ * are left alone. Flat art's clean-up (specks join the nearest colour, and
189
+ * slivers of one ink's two shades fold together) keeps thin strokes whole,
190
+ * but on photos it kept so many real regions that files grew by half.
191
+ */
192
+ photo?: boolean;
193
+ /**
194
+ * Outline art: draw thin lines of ink as lines. A colour that is mostly
195
+ * thin, long regions (outlines, small lettering) is traced down its middle
196
+ * as round-capped strokes of the width measured from it, and the shapes
197
+ * around each line grow to meet under it, so a line can be moved or
198
+ * restyled without a gap opening. Palette mode only; ignored for photos
199
+ * and pixel art.
200
+ */
201
+ strokes?: boolean;
202
+ /**
203
+ * How many times the drawing was enlarged before tracing (an upscaled small
204
+ * image, or a full-size trace of one usually traced smaller). The widths
205
+ * `strokes` takes as lines (2.5–8 px) grow with it. Default 1.
206
+ */
207
+ scale?: number;
208
+ /**
209
+ * Soft shading as gradients: neighbouring tints that are bands of one
210
+ * smooth shade become one shape with a linear or radial gradient, when it
211
+ * fits much better than a flat colour (gradient.ts). Palette mode only, not
212
+ * pixel art. Off by default: a cutter or a plotter can't draw a gradient.
213
+ */
214
+ gradients?: boolean;
79
215
  } & (
80
216
  /**
81
217
  * `merge` folds colours together before any region is built, so the edge
@@ -88,18 +224,28 @@ type InlayOptions = FitOptions & {
88
224
  merge?: Record<string, string>;
89
225
  } |
90
226
  /**
91
- * Ink is darker than `threshold` (0–255). When null, each pixel is compared
92
- * with its neighbourhood instead, for photos of paper under uneven light.
227
+ * Ink is darker than `threshold` (0–255). With 'auto', the threshold is
228
+ * the image's own split between ink and paper (Otsu's), so coloured ink
229
+ * works as well as black (a page with no clear ink, such as a blank
230
+ * scan, falls back to 128). When null, each pixel is compared with its
231
+ * neighbourhood instead, for photos of paper under uneven light.
93
232
  * Only ink is drawn.
94
233
  */
95
234
  {
96
235
  mode: 'binary';
97
- threshold: number | null;
236
+ threshold: number | 'auto' | null;
98
237
  });
99
- /** One filled shape: path data (M/L/C/Z, absolute, nonzero fill) and its colour. */
238
+ /**
239
+ * One filled shape: path data (M/L/C/Z, absolute, nonzero fill) and its
240
+ * colour. With `stroke` (a width in px), a line drawn in that colour instead,
241
+ * with round caps and joins. With `gradient`, the shape is filled with it, and
242
+ * `fill` is its average colour, for anything that can only draw flat colour.
243
+ */
100
244
  type InlayPath = {
101
245
  d: string;
102
246
  fill: string;
247
+ stroke?: number;
248
+ gradient?: Gradient;
103
249
  };
104
250
  /** A trace: the drawing's size in pixels and its shapes, largest first (paint order). */
105
251
  type InlayResult = {
@@ -128,56 +274,86 @@ export declare function mergeLabels(labels: Int32Array, colors: string[], merge:
128
274
  /** Otsu's threshold over opaque pixels: the split that best separates two populations. */
129
275
  export declare function otsu(lum: Uint8Array, alpha: Uint8Array): number;
130
276
  //#endregion
131
- //#region src/palette.d.ts
132
- /**
133
- * Palette extraction for flat artwork.
134
- *
135
- * VTracer's own quantizer averages each cluster, so a flat colour that borders
136
- * a lighter one drifts towards it: on the sample badge the gold sun (#e9a23b)
137
- * came out pale peach (#e9c38f) — wrong enough to break a brand colour. Flat
138
- * art has a property quantizers ignore: its real colours are sharp *peaks* in
139
- * the histogram, and everything between them (anti-aliasing, JPEG ringing) is
140
- * a thin spread of low-count shades. So instead of clustering, take the peaks:
141
- *
142
- * 1. Histogram at 5 bits per channel, keeping each bin's true mean colour.
143
- * 2. Walk bins from most to least frequent; accept a bin as a new colour only
144
- * if it is perceptually distinct (OKLab distance) from every colour already
145
- * accepted, and holds a meaningful share of the image.
146
- * 3. Stop at the requested count.
147
- *
148
- * This is for logos and illustrations. Photos have no peaks (on a photo the
149
- * peaks raised mean error from 9.9 to 15.3), so they use `quantizePalette`.
150
- */
151
- type Rgb = [number, number, number];
152
- export declare function peakPalette(rgba: ArrayLike<number>, maxColors: number, { minDistance, minShare }?: {
153
- minDistance?: number;
154
- minShare?: number;
155
- }): string[];
156
- export declare function oklab([r, g, b]: Rgb): Rgb;
277
+ //#region src/centerline.d.ts
278
+ type CenterlineOptions = {
279
+ /** Ink is darker than this (0–255); 'auto' splits ink from paper by the image's histogram (Otsu); null compares each pixel with its neighbourhood instead. */
280
+ threshold: number | 'auto' | null;
281
+ /** Ink and paper regions smaller than speckle × speckle pixels are folded into their surroundings. */
282
+ speckle: number;
283
+ /** Max distance in pixels between the skeleton and the fitted stroke. */
284
+ tolerance: number;
285
+ /** Turns sharper than this (degrees from straight) are corners. */
286
+ cornerAngle: number;
287
+ /** The stroke width in pixels; measured from the ink when left out. */
288
+ strokeWidth?: number;
289
+ /** The stroke colour. Default black. */
290
+ color?: string;
291
+ };
292
+ /** One stroked path: open subpaths (and closed loops), drawn with round caps and joins. */
293
+ type CenterlinePath = {
294
+ d: string;
295
+ stroke: string;
296
+ strokeWidth: number;
297
+ };
298
+ type CenterlineResult = {
299
+ width: number;
300
+ height: number;
301
+ paths: CenterlinePath[];
302
+ /** The stroke width measured from the ink, in pixels (whatever `strokeWidth` asked for). */
303
+ measuredWidth: number;
304
+ };
305
+ /** Trace to an SVG string; `centerline` gives the strokes as data. */
306
+ export declare const traceCenterline: (rgba: ArrayLike<number>, w: number, h: number, o: CenterlineOptions) => string;
307
+ export declare function centerlineToSvg(r: CenterlineResult): string;
308
+ export declare function centerline(rgba: ArrayLike<number>, w: number, h: number, o: CenterlineOptions): CenterlineResult;
309
+ //#endregion
310
+ //#region src/symmetry.d.ts
157
311
  /**
158
- * A photo's palette: `maxColors` colours that best summarise it, by k-means in
159
- * OKLab (so the clusters are perceptual, not RGB boxes).
312
+ * Symmetry: find a likely mirror axis or rotational order in a raster.
160
313
  *
161
- * It clusters a 5-bit-per-channel histogram, each bin weighted by its pixel
162
- * count, rather than the pixels themselves: the same answer to within a bin,
163
- * at a fraction of the work. It's deterministic: the first centre is the most
164
- * common colour, and each next one is the colour farthest from those chosen,
165
- * weighted by how common it is (k-means++ without the dice), so a photo traces
166
- * the same way every time. Each colour returned is the true mean of the pixels
167
- * in its cluster, most common first. `maxColors` is rounded and taken as at
168
- * least 1 (so 0 or NaN gives one colour); fewer come back when the image has
169
- * fewer distinct colours, and none when every pixel is transparent.
314
+ * Works on the image, not the trace, so the answer doesn't depend on the trace
315
+ * settings. The foreground is every opaque pixel that differs from the
316
+ * background (the most common colour around the border, or transparency). A
317
+ * symmetric drawing's foreground centroid lies on its mirror axis or at its
318
+ * centre of rotation, so only candidates near the centroid are scored, each by
319
+ * the share of pixels (foreground on either side) whose reflection or rotation
320
+ * has the same colour. A coarse pass over a sample of pixels picks the
321
+ * candidates; a finer one settles them.
170
322
  */
171
- export declare function quantizePalette(rgba: ArrayLike<number>, maxColors: number, { iterations }?: {
172
- iterations?: number;
173
- }): string[];
323
+ type MirrorSymmetry = {
324
+ kind: 'mirror';
325
+ /** A vertical axis splits left from right; a horizontal one top from bottom. */
326
+ orientation: 'vertical' | 'horizontal';
327
+ /** x of a vertical axis or y of a horizontal one, in pixels, to the nearest 0.5. */
328
+ position: number;
329
+ /** The share of the drawing that matches its reflection, 0–1. */
330
+ score: number;
331
+ };
332
+ type RotationalSymmetry = {
333
+ kind: 'rotation';
334
+ /** How many times the drawing repeats in a full turn (2 is a half turn). */
335
+ order: number;
336
+ /** The centre of rotation, in pixels. */
337
+ cx: number;
338
+ cy: number;
339
+ /** The share of the drawing that matches itself turned by 1/order, 0–1. */
340
+ score: number;
341
+ };
342
+ type Symmetry = MirrorSymmetry | RotationalSymmetry;
343
+ type SymmetryOptions = {
344
+ /** RGB distance (0–441) under which two pixels count as the same colour. Default 60. */
345
+ tolerance?: number;
346
+ /** Rotational orders to try. Default 2–8. */
347
+ orders?: number[];
348
+ };
174
349
  /**
175
- * Pixel art's palette: its colours exactly, most-used first. Pixel art has a
176
- * handful of deliberate colours, and a one-pixel eye matters as much as the
177
- * sky, so nothing is merged or dropped for being rare or close to another.
178
- * Beyond `maxColors` distinct colours (a photo picked as pixel art), the
179
- * rarest ones take their nearest kept colour when pixels are labelled.
350
+ * Score every candidate symmetry: the best vertical and horizontal mirror axes
351
+ * and the best centre for each rotational order, highest score first. A clean
352
+ * symmetric drawing scores close to 1; around 0.95 is a reasonable bar for
353
+ * "likely", since JPEG noise and anti-aliasing never match exactly. A round
354
+ * drawing scores well for every order. Returns `[]` for an image with almost
355
+ * no foreground.
180
356
  */
181
- export declare function exactPalette(rgba: ArrayLike<number>, maxColors: number): string[];
357
+ export declare function detectSymmetry(rgba: ArrayLike<number>, w: number, h: number, options?: SymmetryOptions): Symmetry[];
182
358
  //#endregion
183
- export type { Cubic, FitOptions, InlayOptions, InlayPath, InlayResult, PathNode, Pt, Rgb, Subpath };
359
+ export type { CenterlineOptions, CenterlinePath, CenterlineResult, Cubic, FitOptions, Gradient, GradientStop, InlayOptions, InlayPath, InlayResult, MirrorSymmetry, PathNode, Pt, Rgb, RotationalSymmetry, Subpath, Symmetry, SymmetryOptions };