inlaytrace 0.0.0-stage → 0.1.1
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 +20 -0
- package/LICENSE +21 -0
- package/README.md +100 -2
- package/dist/index.d.ts +156 -0
- package/dist/index.js +1534 -0
- package/package.json +52 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# inlaytrace
|
|
2
|
+
|
|
3
|
+
## 0.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- The npm page says the package has no dependencies or install scripts, and that releases carry provenance. No code changes.
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
First published release: the tracer the [Inlay app](https://mattblack.space/tools/img-to-svg/)
|
|
12
|
+
uses for logos, illustrations, line art and pixel art, built as ESM with type
|
|
13
|
+
declarations.
|
|
14
|
+
|
|
15
|
+
- `trace(rgba, width, height, options)` in `palette` mode (flat colour, with
|
|
16
|
+
optional `merge` and `pixels` for pixel art) and `binary` mode (line art).
|
|
17
|
+
- `toSvg`, `traceInlay`, `peakPalette`, `exactPalette`, `oklab`, `fitChain`,
|
|
18
|
+
`isFlat`, `pointAt`, `parsePath`, `serializePath` and `mapPoints`.
|
|
19
|
+
- `mergeLabels`, `otsu` and `staircase` are exported for the app's tests and
|
|
20
|
+
aren't part of the stable API.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Matt Black
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,101 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Inlay
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A raster-to-vector tracer whose shapes **share their edges exactly**, like
|
|
4
|
+
inlaid wood: every boundary between two colours is traced and fitted once and
|
|
5
|
+
used by both neighbours, so layers never gap or overlap. Straight edges come out
|
|
6
|
+
as single lines, corners as single sharp points, and round things as smooth
|
|
7
|
+
curves. Pixel art keeps its exact staircase.
|
|
8
|
+
|
|
9
|
+
Pure TypeScript with no DOM and no WebAssembly: it runs in browsers, Web
|
|
10
|
+
Workers and Node. MIT licensed.
|
|
11
|
+
|
|
12
|
+
It is the tracer behind the [Inlay app](https://mattblack.space/tools/img-to-svg/)
|
|
13
|
+
for logos, illustrations, line art and pixel art.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install inlaytrace
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
ESM only, with type declarations. Node 20 or newer, any current browser, and
|
|
22
|
+
Web Workers. No dependencies and no install scripts. Each release is published
|
|
23
|
+
from this repo's release workflow with
|
|
24
|
+
[provenance](https://docs.npmjs.com/generating-provenance-statements), so npm
|
|
25
|
+
shows which commit built it.
|
|
26
|
+
|
|
27
|
+
Versions are 0.x while the API settles: a minor version (0.1 → 0.2) may change
|
|
28
|
+
it, and the [changelog](https://github.com/mtblck/inlaytrace/blob/main/packages/inlay/CHANGELOG.md)
|
|
29
|
+
says how.
|
|
30
|
+
|
|
31
|
+
## Use
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { trace, toSvg, peakPalette } from 'inlaytrace'
|
|
35
|
+
|
|
36
|
+
// RGBA pixels, e.g. from canvas getImageData().data
|
|
37
|
+
const result = trace(rgba, width, height, {
|
|
38
|
+
mode: 'palette',
|
|
39
|
+
palette: peakPalette(rgba, 8), // the image's main colours, as #rrggbb
|
|
40
|
+
speckle: 8, // merge regions smaller than 8 × 8 px into their surroundings
|
|
41
|
+
tolerance: 1, // px the fitted outline may stray from the pixels
|
|
42
|
+
cornerAngle: 45, // turns sharper than this stay corners
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
result.paths // [{ d: 'M…Z', fill: '#rrggbb' }, …] largest first, nonzero fill
|
|
46
|
+
toSvg(result) // '<svg …>…</svg>'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Modes
|
|
50
|
+
|
|
51
|
+
| Options | For |
|
|
52
|
+
| --- | --- |
|
|
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, merge` | Fold colours together before regions are built, so the edge between them disappears: `{ '#from': '#into' }`. |
|
|
55
|
+
| `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: 'binary', threshold` | Line art: ink darker than `threshold` (0–255), or adaptive per neighbourhood when `null`. Only the ink is drawn. |
|
|
57
|
+
|
|
58
|
+
### Also exported
|
|
59
|
+
|
|
60
|
+
- `fitChain(points, closed, options)`: the curve fitter on its own, for any
|
|
61
|
+
chain of points about a pixel apart (corners, straight runs, Schneider
|
|
62
|
+
Béziers between them).
|
|
63
|
+
- `parsePath(d)` / `serializePath(subpaths)`: path data as editable nodes with
|
|
64
|
+
absolute handles, and back. Round trips are idempotent after the first pass.
|
|
65
|
+
- `oklab(rgb)`: the perceptual colour space the palette and labelling use.
|
|
66
|
+
|
|
67
|
+
## How it works
|
|
68
|
+
|
|
69
|
+
1. **Label** every pixel with a palette colour in OKLab. A pixel between two
|
|
70
|
+
colours (anti-aliasing) takes the nearest colour *among its neighbours*, so
|
|
71
|
+
a red/blue edge never grows a purple sliver.
|
|
72
|
+
2. **Clean up**: a pixel whose neighbours nearly all disagree joins them;
|
|
73
|
+
regions smaller than `speckle`² merge into their surroundings.
|
|
74
|
+
3. **Walk the cracks** between differently labelled pixels. Where three regions
|
|
75
|
+
meet is a junction; each boundary between junctions is one chain, with one
|
|
76
|
+
region on either side.
|
|
77
|
+
4. **Fit each chain once**: corners are two straight sides meeting near the
|
|
78
|
+
outline; straight runs become single lines; the rest become least-squares
|
|
79
|
+
cubic Béziers with matching tangents, smoothed only as much as the local
|
|
80
|
+
curvature allows.
|
|
81
|
+
5. **Assemble** each region from its chains (reversed where it sees them from
|
|
82
|
+
the other side). Neighbours share the same fitted edge exactly.
|
|
83
|
+
|
|
84
|
+
The full write-up, with measurements, is in the
|
|
85
|
+
[docs](https://github.com/mtblck/inlaytrace/blob/main/docs/how-it-works/inlay.md);
|
|
86
|
+
[getting started](https://github.com/mtblck/inlaytrace/blob/main/docs/package/getting-started.md)
|
|
87
|
+
covers reading pixels in the browser and in Node, and the
|
|
88
|
+
[API reference](https://github.com/mtblck/inlaytrace/blob/main/docs/package/api.md)
|
|
89
|
+
every export.
|
|
90
|
+
|
|
91
|
+
## Measured
|
|
92
|
+
|
|
93
|
+
On a 640 px JPEG of a five-colour badge, scored by rendering at 4× against the
|
|
94
|
+
clean source art: 0.70% wrong pixels with 177 nodes, against 0.85% with 271
|
|
95
|
+
nodes for VTracer tracing each colour on its own. At 2048 px it traces in about
|
|
96
|
+
1.3 s (VTracer: 17.5 s, timed by hand). On pixel art both draw every pixel exactly; Inlay uses
|
|
97
|
+
about 14% fewer nodes and is about 4× faster on a 1024 px sprite.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
MIT. Source, issues and the app: [mtblck/inlaytrace](https://github.com/mtblck/inlaytrace).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
//#region src/path.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* SVG path data as editable nodes, and back.
|
|
4
|
+
*
|
|
5
|
+
* A path is a list of subpaths; each subpath is a list of anchor nodes with
|
|
6
|
+
* optional absolute bezier handles, the model every vector editor uses. The
|
|
7
|
+
* segment between two nodes is a cubic when either side has a handle and a
|
|
8
|
+
* line otherwise. Everything is converted to absolute M/L/C/Z on the way in,
|
|
9
|
+
* so dragging a node never has to re-resolve relative coordinates downstream.
|
|
10
|
+
*
|
|
11
|
+
* Round trips must not drift: numbers are written with at most three decimals
|
|
12
|
+
* (0.0005 worst case), and writing is idempotent after the first pass, so
|
|
13
|
+
* parse → serialise any number of times moves nothing beyond 0.01.
|
|
14
|
+
*
|
|
15
|
+
* Arcs are approximated by a straight line. Inlay writes only M/L/C/Z, and so
|
|
16
|
+
* do the other tracers and editors this was written against (VTracer writes
|
|
17
|
+
* M/L/C/Z; paper.js adds H/V/S, which are handled).
|
|
18
|
+
*/
|
|
19
|
+
type Pt = {
|
|
20
|
+
x: number;
|
|
21
|
+
y: number;
|
|
22
|
+
};
|
|
23
|
+
type PathNode = {
|
|
24
|
+
x: number;
|
|
25
|
+
y: number;
|
|
26
|
+
in: Pt | null;
|
|
27
|
+
out: Pt | null;
|
|
28
|
+
};
|
|
29
|
+
type Subpath = {
|
|
30
|
+
nodes: PathNode[];
|
|
31
|
+
closed: boolean;
|
|
32
|
+
};
|
|
33
|
+
export declare function parsePath(d: string): Subpath[];
|
|
34
|
+
export declare function serializePath(subs: Subpath[]): string;
|
|
35
|
+
export declare function mapPoints(subs: Subpath[], f: (p: Pt) => Pt): Subpath[];
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/fit.d.ts
|
|
38
|
+
type FitOptions = {
|
|
39
|
+
/** Max distance in pixels between the outline and the fitted path. */
|
|
40
|
+
tolerance: number;
|
|
41
|
+
/** Turns sharper than this (degrees from straight) are corners. */
|
|
42
|
+
cornerAngle: number;
|
|
43
|
+
/** Half-window in px of the smoothing over the pixel staircase (default 12). */
|
|
44
|
+
smoothing?: number;
|
|
45
|
+
};
|
|
46
|
+
/** A cubic Bézier: start, two handles, end. A straight line has handles on the chord. */
|
|
47
|
+
type Cubic = [Pt, Pt, Pt, Pt];
|
|
48
|
+
/** A cubic whose handles sit on its chord (within 0.15 px, and between the ends) is a line. */
|
|
49
|
+
export declare function isFlat([p0, c1, c2, p3]: Cubic): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Fit a chain. Open: from points[0] to the last point, which stay fixed.
|
|
52
|
+
* Closed: points[0] is not repeated at the end; the result is a loop that starts
|
|
53
|
+
* at a corner if there is one.
|
|
54
|
+
*/
|
|
55
|
+
export declare function fitChain(points: Pt[], closed: boolean, o: FitOptions): Cubic[];
|
|
56
|
+
export declare function pointAt(b: Cubic, t: number): Pt;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/inlay.d.ts
|
|
59
|
+
type InlayOptions = FitOptions & {
|
|
60
|
+
/** Regions smaller than speckle × speckle pixels are merged into their surroundings. */
|
|
61
|
+
speckle: number;
|
|
62
|
+
/**
|
|
63
|
+
* Pixel art: keep every pixel. No clean-up of single pixels, and no curve
|
|
64
|
+
* fitting: each boundary is its exact pixel staircase, with straight runs
|
|
65
|
+
* joined into single lines.
|
|
66
|
+
*/
|
|
67
|
+
pixels?: boolean;
|
|
68
|
+
} & (
|
|
69
|
+
/**
|
|
70
|
+
* `merge` folds colours together before any region is built, so the edge
|
|
71
|
+
* between them disappears: { from: to }, traced hex to traced hex, chains
|
|
72
|
+
* followed (a → b → c is a → c).
|
|
73
|
+
*/
|
|
74
|
+
{
|
|
75
|
+
mode: 'palette';
|
|
76
|
+
palette: string[];
|
|
77
|
+
merge?: Record<string, string>;
|
|
78
|
+
} |
|
|
79
|
+
/**
|
|
80
|
+
* Ink is darker than `threshold` (0–255). When null, each pixel is compared
|
|
81
|
+
* with its neighbourhood instead, for photos of paper under uneven light.
|
|
82
|
+
* Only ink is drawn.
|
|
83
|
+
*/
|
|
84
|
+
{
|
|
85
|
+
mode: 'binary';
|
|
86
|
+
threshold: number | null;
|
|
87
|
+
});
|
|
88
|
+
/** One filled shape: path data (M/L/C/Z, absolute, nonzero fill) and its colour. */
|
|
89
|
+
type InlayPath = {
|
|
90
|
+
d: string;
|
|
91
|
+
fill: string;
|
|
92
|
+
};
|
|
93
|
+
/** A trace: the drawing's size in pixels and its shapes, largest first (paint order). */
|
|
94
|
+
type InlayResult = {
|
|
95
|
+
width: number;
|
|
96
|
+
height: number;
|
|
97
|
+
paths: InlayPath[];
|
|
98
|
+
};
|
|
99
|
+
/** The trace as an SVG document. */
|
|
100
|
+
export declare function toSvg(r: InlayResult): string;
|
|
101
|
+
/** Trace to an SVG string; `trace` gives the shapes as data. */
|
|
102
|
+
export declare const traceInlay: (rgba: ArrayLike<number>, w: number, h: number, o: InlayOptions) => string;
|
|
103
|
+
/**
|
|
104
|
+
* Trace RGBA pixels (`w` × `h`, 4 bytes a pixel, as from canvas getImageData)
|
|
105
|
+
* into filled shapes that share their edges exactly.
|
|
106
|
+
*/
|
|
107
|
+
export declare function trace(rgba: ArrayLike<number>, w: number, h: number, o: InlayOptions): InlayResult;
|
|
108
|
+
/**
|
|
109
|
+
* A chain as its exact pixel boundary: a line between each pair of turns.
|
|
110
|
+
* The points are pixel corners about a pixel apart, so a run of them in one
|
|
111
|
+
* direction is one straight edge. A closed loop starts at a turn, so its seam
|
|
112
|
+
* isn't a node in the middle of an edge.
|
|
113
|
+
*/
|
|
114
|
+
export declare function staircase(pts: Pt[], closed: boolean): Cubic[];
|
|
115
|
+
/** Relabel merged colours as their targets, following chains, in place. */
|
|
116
|
+
export declare function mergeLabels(labels: Int32Array, colors: string[], merge: Record<string, string>): void;
|
|
117
|
+
/** Otsu's threshold over opaque pixels: the split that best separates two populations. */
|
|
118
|
+
export declare function otsu(lum: Uint8Array, alpha: Uint8Array): number;
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/palette.d.ts
|
|
121
|
+
/**
|
|
122
|
+
* Palette extraction for flat artwork.
|
|
123
|
+
*
|
|
124
|
+
* VTracer's own quantizer averages each cluster, so a flat colour that borders
|
|
125
|
+
* a lighter one drifts towards it: on the sample badge the gold sun (#e9a23b)
|
|
126
|
+
* came out pale peach (#e9c38f) — wrong enough to break a brand colour. Flat
|
|
127
|
+
* art has a property quantizers ignore: its real colours are sharp *peaks* in
|
|
128
|
+
* the histogram, and everything between them (anti-aliasing, JPEG ringing) is
|
|
129
|
+
* a thin spread of low-count shades. So instead of clustering, take the peaks:
|
|
130
|
+
*
|
|
131
|
+
* 1. Histogram at 5 bits per channel, keeping each bin's true mean colour.
|
|
132
|
+
* 2. Walk bins from most to least frequent; accept a bin as a new colour only
|
|
133
|
+
* if it is perceptually distinct (OKLab distance) from every colour already
|
|
134
|
+
* accepted, and holds a meaningful share of the image.
|
|
135
|
+
* 3. Stop at the requested count.
|
|
136
|
+
*
|
|
137
|
+
* The result is passed to VTracer as a fixed palette, which it matches in OKLab.
|
|
138
|
+
* This is for logos and illustrations; photos have no peaks, so the poster
|
|
139
|
+
* preset keeps VTracer's quantizer.
|
|
140
|
+
*/
|
|
141
|
+
type Rgb = [number, number, number];
|
|
142
|
+
export declare function peakPalette(rgba: ArrayLike<number>, maxColors: number, { minDistance, minShare }?: {
|
|
143
|
+
minDistance?: number;
|
|
144
|
+
minShare?: number;
|
|
145
|
+
}): string[];
|
|
146
|
+
export declare function oklab([r, g, b]: Rgb): Rgb;
|
|
147
|
+
/**
|
|
148
|
+
* Pixel art's palette: its colours exactly, most-used first. Pixel art has a
|
|
149
|
+
* handful of deliberate colours, and a one-pixel eye matters as much as the
|
|
150
|
+
* sky, so nothing is merged or dropped for being rare or close to another.
|
|
151
|
+
* Beyond `maxColors` distinct colours (a photo picked as pixel art), the
|
|
152
|
+
* rarest ones take their nearest kept colour when pixels are labelled.
|
|
153
|
+
*/
|
|
154
|
+
export declare function exactPalette(rgba: ArrayLike<number>, maxColors: number): string[];
|
|
155
|
+
//#endregion
|
|
156
|
+
export type { Cubic, FitOptions, InlayOptions, InlayPath, InlayResult, PathNode, Pt, Rgb, Subpath };
|