agglomerative-clustering 2.0.0 → 3.0.0-beta.2
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/README.md +518 -112
- package/dist/clustering.js +2 -14
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +1316 -155
- package/dist/index.d.cts +446 -0
- package/dist/index.d.ts +397 -29
- package/dist/index.mjs +1285 -152
- package/dist/shared.cjs +404 -0
- package/dist/shared.mjs +395 -0
- package/dist/worker-thread.mjs +429 -0
- package/dist/worker.d.ts +117 -0
- package/dist/worker.mjs +1098 -0
- package/package.json +64 -26
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,118 @@
|
|
|
1
|
+
/** How pixel bytes are laid out: `'rgb'` is 3 bytes per pixel, `'rgba'` is 4 (red, green, blue, alpha). */
|
|
1
2
|
export type ImageFormat = 'rgb' | 'rgba';
|
|
2
3
|
|
|
4
|
+
/**
|
|
5
|
+
* What went wrong, on the `code` of every error this library throws (the message is for people and may change):
|
|
6
|
+
* `invalid-argument` (image, k, options or palette), `invalid-clustering`, `unsupported-version` (a clustering from a
|
|
7
|
+
* newer format version or one using a colour space it doesn't know, or an older one that lacks what was asked for),
|
|
8
|
+
* `out-of-memory`, `wasm-load-failed` (the WebAssembly module could not be loaded or compiled: a missing or blocked file, a
|
|
9
|
+
* browser without SIMD, or a `.wasm` from another version of the library), and in the worker entry
|
|
10
|
+
* `worker-terminated`, `worker-failed` and `invalid-handle`.
|
|
11
|
+
*/
|
|
12
|
+
export type ErrorCode = 'invalid-argument' | 'invalid-clustering' | 'unsupported-version' | 'out-of-memory' | 'wasm-load-failed'
|
|
13
|
+
| 'worker-terminated' | 'worker-failed' | 'invalid-handle';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Which argument an error is about, for programs (the message is for people and may change): the image, `k`, the
|
|
17
|
+
* clustering or palette, the options object as a whole, or one option as `options.<name>` (for example
|
|
18
|
+
* `'options.colorSpace'`). `colorSpace` is the second argument of `new Palette(source, colorSpace)`.
|
|
19
|
+
*/
|
|
20
|
+
export type ErrorArgument = 'image' | 'k' | 'clustering' | 'palette' | 'colorSpace' | 'options' | `options.${string}`;
|
|
21
|
+
|
|
22
|
+
/** An `Error` thrown by this library. There is no class to test with `instanceof`: check `code`. */
|
|
23
|
+
export interface CodedError extends Error {
|
|
24
|
+
code: ErrorCode;
|
|
25
|
+
/**
|
|
26
|
+
* Which argument the error is about, when it is about one (most `invalid-argument`, `invalid-clustering` and
|
|
27
|
+
* `unsupported-version` errors), so a form can point at the right field. Absent otherwise (a failed load, running
|
|
28
|
+
* out of memory, the worker's state).
|
|
29
|
+
*/
|
|
30
|
+
argument?: ErrorArgument;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* An image as raw bytes with an explicit layout: for RGB data, a `Uint8ClampedArray`, or Node, where there is no
|
|
35
|
+
* `ImageData`. Functions that return an image give back an object of this shape with the same `format`.
|
|
36
|
+
*/
|
|
3
37
|
export interface RawImage {
|
|
4
38
|
data: Uint8Array | Uint8ClampedArray;
|
|
5
39
|
format: ImageFormat;
|
|
6
40
|
/** Width in pixels. Optional; used for dithering, and copied to `quantize*` results. */
|
|
7
|
-
width?: number;
|
|
41
|
+
width?: number | undefined;
|
|
8
42
|
/** Height in pixels. Optional; copied to `quantize*` results. */
|
|
9
|
-
height?: number;
|
|
43
|
+
height?: number | undefined;
|
|
10
44
|
}
|
|
11
45
|
|
|
12
|
-
|
|
46
|
+
/**
|
|
47
|
+
* The platform's `ImageData` class where the global exists (the DOM lib, or typings for a canvas polyfill), and `never`
|
|
48
|
+
* where it doesn't, so these declarations compile with or without the DOM lib, in browser and Node-only projects.
|
|
49
|
+
*/
|
|
50
|
+
export type PlatformImageData = typeof globalThis extends { ImageData: { prototype: infer T } } ? T : never;
|
|
51
|
+
|
|
52
|
+
/** What a function that returns an image gives back for the input it was given: the same kind of thing. */
|
|
53
|
+
export type ImageResult<I extends ImageLike> =
|
|
54
|
+
I extends Uint8Array ? Uint8Array
|
|
55
|
+
: I extends RawImage ? RawImage
|
|
56
|
+
: I extends PlatformImageData ? PlatformImageData
|
|
57
|
+
: never;
|
|
58
|
+
|
|
59
|
+
/** Raw RGBA bytes, `ImageData`, or `{ data, format }`. Functions that return an image give back the same kind. */
|
|
60
|
+
export type ImageLike = Uint8Array | PlatformImageData | RawImage;
|
|
61
|
+
|
|
62
|
+
/** `WebAssembly.Module` where the global exists, else `never` (see `PlatformImageData`). */
|
|
63
|
+
export type WasmModule = typeof globalThis extends { WebAssembly: { Module: { prototype: infer M } } } ? M : never;
|
|
64
|
+
|
|
65
|
+
declare const clusteringBrand: unique symbol;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A palette: flat RGB bytes (`[r, g, b, r, g, b, …]`) that also know the colour space they were made in. It is an
|
|
69
|
+
* ordinary `Uint8Array` (its `constructor` is `Uint8Array`, so `toEqual(new Uint8Array(…))` in a test passes) with a
|
|
70
|
+
* hidden `colorSpace` tag, so everything that takes bytes (indexing, `length`, `subarray`, encoders, files) takes it.
|
|
71
|
+
* What the tag adds: `quantizeWithPalette` and `quantizeToIndicesWithPalette` match pixels in a palette's own space
|
|
72
|
+
* unless you name another, so a palette made with `colorSpace: 'oklab'` is mapped the way it was made without you
|
|
73
|
+
* having to remember. Every function here that returns a palette returns this. `slice`, `subarray` and structured
|
|
74
|
+
* clones (`postMessage`, storage) give plain `Uint8Array`s without the tag, matched in RGB unless you say otherwise;
|
|
75
|
+
* `new Palette(bytes, 'oklab')` makes one by hand, and `value instanceof Palette` tells.
|
|
76
|
+
*/
|
|
77
|
+
export class Palette extends Uint8Array {
|
|
78
|
+
/**
|
|
79
|
+
* `source` is bytes (a `Uint8Array`, `Uint8ClampedArray` or plain array of integers 0-255, copied) or a length, whole colours
|
|
80
|
+
* either way (a multiple of 3). It is checked like any palette passed to a function: nothing is coerced into a byte, so
|
|
81
|
+
* `invalid-argument` for `[300, 0, 0]`, a string, or a length that isn't a multiple of 3.
|
|
82
|
+
*/
|
|
83
|
+
constructor(source?: ArrayLike<number> | number, colorSpace?: ColorSpace);
|
|
84
|
+
/** The space the colours were made in: how they should be matched. `'rgb'` for a palette built from plain bytes. */
|
|
85
|
+
readonly colorSpace: ColorSpace;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The bytes of a clustering, as `getClustering` returns them: an opaque `Uint8Array` (tagged, like `Palette`) that you can
|
|
90
|
+
* store (a file, IndexedDB, next to the image) and cut to a palette of any size later. It is a type of its own, so a
|
|
91
|
+
* clustering can't be passed where a palette goes (or the other way round): TypeScript says so, and at run time a
|
|
92
|
+
* `Clustering` given as a palette is rejected, with no guessing about its bytes. Stored bytes come back as a plain
|
|
93
|
+
* `Uint8Array`, which functions still accept at run time; `new Clustering(bytes)` gives them the type back (TypeScript
|
|
94
|
+
* wants it) and checks they are a clustering.
|
|
95
|
+
*/
|
|
96
|
+
export class Clustering extends Uint8Array {
|
|
97
|
+
/**
|
|
98
|
+
* Bytes (a `Uint8Array`, `Uint8ClampedArray` or plain array of integers 0-255, copied), or a view of a buffer. Throws
|
|
99
|
+
* `invalid-clustering` unless they are a clustering: a number, or anything that isn't bytes, is refused rather than coerced.
|
|
100
|
+
*/
|
|
101
|
+
constructor(bytes: Uint8Array | Uint8ClampedArray | readonly number[]);
|
|
102
|
+
constructor(buffer: ArrayBufferLike, byteOffset?: number, length?: number);
|
|
103
|
+
/** Only a type: what makes a `Clustering` different from a palette or other bytes to the compiler. */
|
|
104
|
+
readonly [clusteringBrand]: true;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** What is not a clustering: a `Clustering` is not accepted where a palette is. */
|
|
108
|
+
type NotClustering = { readonly [clusteringBrand]?: never };
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A palette to map with: a `Palette` (which knows its colour space), flat RGB bytes as a plain `Uint8Array` or
|
|
112
|
+
* `Uint8ClampedArray` (matched in RGB unless you name a space), or a plain array of integers from 0 to 255. Not a
|
|
113
|
+
* `Clustering`.
|
|
114
|
+
*/
|
|
115
|
+
export type PaletteLike = (Palette | Uint8Array | Uint8ClampedArray | readonly number[]) & NotClustering;
|
|
13
116
|
|
|
14
117
|
/** The space colours are clustered and matched in. */
|
|
15
118
|
export type ColorSpace = 'rgb' | 'oklab';
|
|
@@ -17,62 +120,327 @@ export type ColorSpace = 'rgb' | 'oklab';
|
|
|
17
120
|
/** How pixel opacity affects clustering. */
|
|
18
121
|
export type AlphaMode = 'weight' | 'ignore';
|
|
19
122
|
|
|
20
|
-
|
|
123
|
+
/** Cancelling a call. Every function that takes an image has it; see `RunOptions.signal`. */
|
|
124
|
+
export interface CancelOptions {
|
|
125
|
+
/**
|
|
126
|
+
* Cancels the call. A call given a `signal` clusters (where it clusters) in slices of about 10 ms and checks the
|
|
127
|
+
* signal between them, so an abort takes effect within a slice, not after the whole clustering. It rejects with
|
|
128
|
+
* the signal's `reason` (an `AbortError` by default), like `fetch`; note that it is that reason that is thrown, so it
|
|
129
|
+
* has the signal's own `name`, not an `ErrorCode`. A signal that is already aborted does no work. On the functions
|
|
130
|
+
* that only map pixels (`quantizeFromClustering`, `quantizeWithPalette` and their indexed forms) it is checked once,
|
|
131
|
+
* before the call starts, since they take milliseconds. Pixel mapping, which follows the clustering, can't be
|
|
132
|
+
* interrupted. The call's result is the same as without a signal. In the worker entry the worker itself stops
|
|
133
|
+
* working on the call, and a call still waiting its turn never starts.
|
|
134
|
+
*/
|
|
135
|
+
signal?: AbortSignal | undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Cancelling a long call and watching it, for the functions that cluster. Both are optional and can be used apart. */
|
|
139
|
+
export interface RunOptions extends CancelOptions {
|
|
140
|
+
/**
|
|
141
|
+
* Called with the fraction done, from 0 to 1, as the call runs: a few times per slice at most, never going
|
|
142
|
+
* backwards, ending with 1. It is approximate and not linear in time. Giving it also makes the call run in
|
|
143
|
+
* slices, so other tasks (such as painting) get a turn while it works.
|
|
144
|
+
*/
|
|
145
|
+
onProgress?: ((progress: number) => void) | undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Options for building the colour hierarchy: `getClustering`, `getPalette`, and the clustering half of `quantize` and `quantizeToIndices`. */
|
|
149
|
+
export interface ClusteringOptions extends RunOptions {
|
|
21
150
|
/**
|
|
22
151
|
* `'rgb'` (default) minimises squared sRGB error, which is what MSE/PSNR measure.
|
|
23
|
-
* `'oklab'` clusters in OKLab
|
|
152
|
+
* `'oklab'` clusters in OKLab. Neither is better everywhere: see "Choosing a colour space" in the README.
|
|
24
153
|
*/
|
|
25
|
-
colorSpace?: ColorSpace;
|
|
154
|
+
colorSpace?: ColorSpace | undefined;
|
|
26
155
|
/**
|
|
27
156
|
* Bits per channel, 1-8 (default 8: every distinct colour is clustered exactly). Lower values group colours
|
|
28
157
|
* that share their top bits before clustering, at their mean colour, which bounds the time spent on images
|
|
29
158
|
* with very many distinct colours. 6 is a good choice for large photos.
|
|
30
159
|
*/
|
|
31
|
-
precision?: number;
|
|
160
|
+
precision?: number | undefined;
|
|
32
161
|
/**
|
|
33
162
|
* How RGBA pixels count. `'weight'` (default): in proportion to their opacity, so fully transparent pixels are
|
|
34
163
|
* ignored and semi-transparent ones count partially. `'ignore'`: every pixel counts fully, as in 1.x.
|
|
35
164
|
*/
|
|
36
|
-
alpha?: AlphaMode;
|
|
165
|
+
alpha?: AlphaMode | undefined;
|
|
166
|
+
/**
|
|
167
|
+
* `false` (default), `true`, or an alpha cutoff from 1 to 255 (`true` means 128), for RGBA images: pixels with alpha
|
|
168
|
+
* below the cutoff don't exist as far as the palette is concerned. They are left out of clustering, and the rest count
|
|
169
|
+
* whole instead of by opacity (so it replaces `alpha` for this call). That is the model of indexed output (GIF, palette
|
|
170
|
+
* PNG), and it gives the same palette everywhere: `getPalette(image, k, { transparent })` is the palette
|
|
171
|
+
* `quantizeToIndices(image, k, { transparent })` uses, apart from that function's extra transparent entry.
|
|
172
|
+
* Cluster with it to get the palette you will map with (see `IndexOptions.transparent`). The cutoff is recorded in the
|
|
173
|
+
* clustering (see `ClusteringInfo.transparent`), and `quantizeToIndicesFromClustering` uses it unless you name one.
|
|
174
|
+
* It replaces `alpha` (with a cutoff, pixels count whole or not at all), so giving both is an `invalid-argument` error.
|
|
175
|
+
*/
|
|
176
|
+
transparent?: boolean | number | undefined;
|
|
37
177
|
}
|
|
38
178
|
|
|
39
|
-
|
|
179
|
+
/** Options for mapping pixels to a palette: `quantizeFromClustering`, `quantizeWithPalette`, and the mapping half of `quantize` and `quantizeToIndices`. They take `signal` but no `onProgress`: mapping takes milliseconds. */
|
|
180
|
+
export interface MappingOptions extends CancelOptions {
|
|
40
181
|
/**
|
|
41
|
-
* The space pixels are matched to their nearest palette colour in.
|
|
42
|
-
*
|
|
182
|
+
* The space pixels are matched to their nearest palette colour in. The default is the space the colours were made in:
|
|
183
|
+
* the clustering's in `quantizeFromClustering`, a `Palette`'s own in `quantizeWithPalette` and
|
|
184
|
+
* `quantizeToIndicesWithPalette`; `'rgb'` for plain bytes, and in `quantize` and `quantizeToIndices`, which match in the
|
|
185
|
+
* space they cluster in.
|
|
43
186
|
*/
|
|
44
|
-
colorSpace?: ColorSpace;
|
|
187
|
+
colorSpace?: ColorSpace | undefined;
|
|
45
188
|
/**
|
|
46
189
|
* Error-diffusion dithering (serpentine Floyd-Steinberg): `false` (default), `true`, or a strength from 0 to 1.
|
|
47
190
|
* Needs the image width: taken from `ImageData` or `RawImage.width`, or given as `width`.
|
|
48
191
|
*/
|
|
49
|
-
dither?: boolean | number;
|
|
50
|
-
/**
|
|
51
|
-
|
|
192
|
+
dither?: boolean | number | undefined;
|
|
193
|
+
/**
|
|
194
|
+
* Image width in pixels, for dithering a `Uint8Array` or a `RawImage` without `width`. An image that has its own
|
|
195
|
+
* `width` (`ImageData`, or a `RawImage` with one) must agree with it: a different value is an `invalid-argument` error.
|
|
196
|
+
*/
|
|
197
|
+
width?: number | undefined;
|
|
52
198
|
}
|
|
53
199
|
|
|
200
|
+
/** Clustering and mapping options together, for `quantize`, which does both. */
|
|
54
201
|
export type QuantizeOptions = ClusteringOptions & MappingOptions;
|
|
55
202
|
|
|
56
|
-
|
|
203
|
+
/** Options that only the `quantizeToIndices*` functions take. */
|
|
204
|
+
export interface IndexOptions {
|
|
205
|
+
/**
|
|
206
|
+
* `false` (default): every pixel gets the index of its nearest colour, whatever its alpha.
|
|
207
|
+
* `true`, or an alpha cutoff from 1 to 255 (`true` means 128): if the image is RGBA and has pixels with alpha
|
|
208
|
+
* below the cutoff, they get an extra entry appended after the palette (black, taking no part in matching), as
|
|
209
|
+
* GIFs and palette PNGs expect; its index is the result's `transparentIndex`. Every other pixel is shown fully
|
|
210
|
+
* opaque in its nearest colour. `k` counts that entry, so `k = 256` gives at most 256 entries (255 colours and the
|
|
211
|
+
* transparent one) and 8-bit indices, the limit of a GIF or palette PNG; at least one colour is always kept. Where
|
|
212
|
+
* this function builds the palette itself it also leaves the pixels below the cutoff out of clustering (see
|
|
213
|
+
* `ClusteringOptions.transparent`), so its palette is `getPalette(image, k - 1, { transparent })`'s. With a palette
|
|
214
|
+
* you supply (`quantizeToIndicesWithPalette`) there is no `k`: the entry is added to your palette as it is. In
|
|
215
|
+
* `quantizeToIndicesFromClustering` the default is the cutoff the clustering was made with.
|
|
216
|
+
*/
|
|
217
|
+
transparent?: boolean | number | undefined;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The options of `quantizeToIndices`: clustering, mapping and indexed-output options. */
|
|
221
|
+
export type QuantizeIndexOptions = QuantizeOptions & IndexOptions;
|
|
222
|
+
/** The options of `quantizeToIndicesFromClustering` and `quantizeToIndicesWithPalette`: mapping and indexed-output options. */
|
|
223
|
+
export type MappingIndexOptions = MappingOptions & IndexOptions;
|
|
224
|
+
|
|
225
|
+
interface IndexedImageFields {
|
|
226
|
+
/**
|
|
227
|
+
* Flat RGB bytes, one entry per index, including the transparent entry if there is one. Empty for an image with no
|
|
228
|
+
* pixels. An image with pixels but no visible colour has a single black entry, or with `transparent`, just
|
|
229
|
+
* the transparent entry. A `Palette`, so it carries the colour space it was matched in. This is what an encoder wants
|
|
230
|
+
* (a GIF or palette PNG has the transparent entry in its palette); to map with these colours again, use `colors`.
|
|
231
|
+
*/
|
|
232
|
+
palette: Palette;
|
|
233
|
+
/**
|
|
234
|
+
* The colours without the transparent entry: `palette` when there is none (`transparentIndex` is `null`), else
|
|
235
|
+
* `palette` up to that entry. Pass it to `quantizeToIndicesWithPalette` to map another image with the same colours, so the
|
|
236
|
+
* transparent entry isn't mistaken for a black colour and added twice.
|
|
237
|
+
*/
|
|
238
|
+
colors: Palette;
|
|
239
|
+
/** The index of the entry for fully transparent pixels, or `null` if there is none. */
|
|
240
|
+
transparentIndex: number | null;
|
|
241
|
+
/** Copied from the input, when it is `ImageData` or a `RawImage` with `width`. */
|
|
242
|
+
width?: number;
|
|
243
|
+
/** Copied from the input, when it is `ImageData` or a `RawImage` with `height`. */
|
|
244
|
+
height?: number;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* One palette index per pixel in `indices`: a `Uint8Array` up to 256 palette entries, a `Uint16Array` up to 65536, else a
|
|
249
|
+
* `Uint32Array`. `indexBits` says which (8, 16 or 32), and checking it narrows `indices` to that array type.
|
|
250
|
+
*/
|
|
251
|
+
export type IndexedImage =
|
|
252
|
+
| (IndexedImageFields & { indexBits: 8; indices: Uint8Array })
|
|
253
|
+
| (IndexedImageFields & { indexBits: 16; indices: Uint16Array })
|
|
254
|
+
| (IndexedImageFields & { indexBits: 32; indices: Uint32Array });
|
|
255
|
+
|
|
256
|
+
/** Options for `init`, and for `createWorker` (which passes `wasm` on to its worker). */
|
|
257
|
+
export interface InitOptions {
|
|
258
|
+
/**
|
|
259
|
+
* Where to get the WebAssembly module, for CDNs, strict CSPs and runtimes that can't fetch or compile it from its
|
|
260
|
+
* default place next to the package's script (Cloudflare Workers, for instance). One of:
|
|
261
|
+
* - a URL or path string, or a `URL`, to the `clustering.wasm` file;
|
|
262
|
+
* - its bytes, as an `ArrayBuffer` or a typed array;
|
|
263
|
+
* - an already compiled `WebAssembly.Module`.
|
|
264
|
+
*/
|
|
265
|
+
wasm?: string | URL | ArrayBuffer | ArrayBufferView | WasmModule | undefined;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The version of this library, as in its `package.json` (for example `'3.0.0'`). The WebAssembly module checks it matches its own when it loads. */
|
|
269
|
+
export const version: string;
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* The version of the data this library stores: the format of the clusterings `getClustering` writes (`getClusteringInfo`
|
|
273
|
+
* reports a clustering's own as its `formatVersion`). One integer for the library's stored data, unrelated to `version`: it
|
|
274
|
+
* changes only when the stored format does, so a new release that keeps the format keeps this number. This build reads
|
|
275
|
+
* this format and the one before it (2).
|
|
276
|
+
*/
|
|
277
|
+
export const formatVersion: 3;
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Loads the WebAssembly module. Every other function does it on first use, so calling `init` is optional, except to
|
|
281
|
+
* say where the module is (`wasm`), which must come before any other call. The first load that succeeds wins; a
|
|
282
|
+
* failed one can be retried.
|
|
283
|
+
*/
|
|
284
|
+
export function init(options?: InitOptions): Promise<void>;
|
|
57
285
|
|
|
58
|
-
|
|
286
|
+
/**
|
|
287
|
+
* Builds the colour hierarchy for an image: the expensive step, done once. The result is a `Clustering` (opaque bytes) you can
|
|
288
|
+
* cache or store next to the image. Cut it to a palette of any size with `getPaletteFromClustering`, or map pixels
|
|
289
|
+
* with `quantizeFromClustering`. An image with no visible pixels gives a clustering that cuts to an empty palette.
|
|
290
|
+
*/
|
|
291
|
+
export function getClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
|
|
59
292
|
|
|
60
|
-
|
|
293
|
+
/**
|
|
294
|
+
* A palette of at most `k` colours for `image` (fewer if it has fewer distinct colours), as flat RGB bytes in split
|
|
295
|
+
* order (see `getPaletteFromClustering`). It clusters the image each time: to try several sizes, call `getClustering`
|
|
296
|
+
* once and cut it.
|
|
297
|
+
*
|
|
298
|
+
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
299
|
+
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
300
|
+
*/
|
|
301
|
+
export function getPalette(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Palette>;
|
|
61
302
|
|
|
62
303
|
/**
|
|
63
|
-
* Cuts a clustering to at most `k` colours, in split order: index 0 is the dominant colour
|
|
304
|
+
* Cuts a clustering to at most `k` colours, in split order: index 0 is the dominant colour at two colours and keeps its
|
|
305
|
+
* place as the palette grows (it is not always the heaviest entry of a larger one), and the palette for
|
|
64
306
|
* `k + 1` is the palette for `k` with one entry refined and one colour appended.
|
|
307
|
+
*
|
|
308
|
+
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
309
|
+
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
65
310
|
*/
|
|
66
|
-
export function getPaletteFromClustering(clustering:
|
|
311
|
+
export function getPaletteFromClustering(clustering: Clustering, k: number): Promise<Palette>;
|
|
67
312
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
313
|
+
/**
|
|
314
|
+
* A palette with how much of the image each of its colours covers, for swatches with percentages. The three arrays come
|
|
315
|
+
* from one cut of one clustering, so they line up by construction: entry `i` of each describes the same colour.
|
|
316
|
+
*/
|
|
317
|
+
export interface Swatches {
|
|
318
|
+
/** The colours in split order, exactly what `getPaletteFromClustering` gives for the same `k`. */
|
|
319
|
+
palette: Palette;
|
|
320
|
+
/**
|
|
321
|
+
* Pixels per colour: fully transparent pixels count 0 and partly transparent ones in proportion to their opacity, as
|
|
322
|
+
* in clustering. They sum to the image's visible weight.
|
|
323
|
+
*/
|
|
324
|
+
weights: Float64Array;
|
|
325
|
+
/** Each colour's share of the visible weight, from 0 to 1. They sum to 1 (they are empty for an image with no visible pixels). */
|
|
326
|
+
shares: Float64Array;
|
|
327
|
+
}
|
|
71
328
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
329
|
+
/**
|
|
330
|
+
* The palette for `image` at `k` colours with each colour's weight and share: one call for swatches with percentages.
|
|
331
|
+
* It clusters the image each time; if you already have a clustering, use `getSwatchesFromClustering`.
|
|
332
|
+
*
|
|
333
|
+
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
334
|
+
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
335
|
+
*/
|
|
336
|
+
export function getSwatches(image: ImageLike, k: number, options?: ClusteringOptions): Promise<Swatches>;
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* The same as `getSwatches` from a clustering you already have, so trying another `k` costs well under a millisecond.
|
|
340
|
+
* Needs a clustering from 3.x; a 2.x clustering has no weights and is rejected.
|
|
341
|
+
*
|
|
342
|
+
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
343
|
+
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
344
|
+
*/
|
|
345
|
+
export function getSwatchesFromClustering(clustering: Clustering, k: number): Promise<Swatches>;
|
|
346
|
+
|
|
347
|
+
interface ClusteringInfoFields {
|
|
348
|
+
colorSpace: ColorSpace;
|
|
349
|
+
/** Bits per channel it was clustered with. */
|
|
350
|
+
precision: number;
|
|
351
|
+
/**
|
|
352
|
+
* The alpha cutoff (1-255) the clustering was made with (`transparent` in `ClusteringOptions`), or `false` for none. It is
|
|
353
|
+
* the default `transparent` of `quantizeToIndicesFromClustering`, so clustering and mapping agree unless you name another.
|
|
354
|
+
*/
|
|
355
|
+
transparent: false | number;
|
|
356
|
+
/** The largest palette it can be cut to: its number of distinct clustered colours (fewer than the image's own when `precision` is below 8). */
|
|
357
|
+
maxColors: number;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** What a 3.x clustering holds. A clustering you just made with `getClustering` is this one: check `formatVersion === 3` to narrow to it. */
|
|
361
|
+
export interface ClusteringInfoV3 extends ClusteringInfoFields {
|
|
362
|
+
/** The format of the clustering's bytes: the data version (see `formatVersion`, the one this build writes), not the library's `version`. */
|
|
363
|
+
formatVersion: 3;
|
|
364
|
+
/** Total weight in pixels (opacity-weighted). */
|
|
365
|
+
totalWeight: number;
|
|
366
|
+
/**
|
|
367
|
+
* `mse[k - 1]` is the mean squared error per channel of the k-colour clustering: each pixel's squared
|
|
368
|
+
* distance to its cluster's colour, weighted as in clustering, in 8-bit RGB values (`'rgb'`) or OKLab units
|
|
369
|
+
* (`'oklab'`). `mse[0]` is the image's variance. At `precision: 8`, `mse[maxColors - 1]` is 0; with a lower
|
|
370
|
+
* precision it is the error that grouping colours discarded, which is recorded and included. It is an estimate,
|
|
371
|
+
* computed from merge costs on integer-rounded colours. Mapping pixels to their nearest palette colour usually
|
|
372
|
+
* lowers it by 10-15%.
|
|
373
|
+
*/
|
|
374
|
+
mse: Float64Array;
|
|
375
|
+
/**
|
|
376
|
+
* `explained[k - 1]` is the share of the image's variance the k-colour clustering keeps, `1 - mse[k - 1] /
|
|
377
|
+
* mse[0]`: 0 for one colour, rising to 1 at `maxColors`, and 1 throughout for an image of a single colour. It has no
|
|
378
|
+
* units, so a target such as `0.99` means the same in `'rgb'` and `'oklab'`.
|
|
379
|
+
*/
|
|
380
|
+
explained: Float64Array;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** What a 2.x clustering holds: no weights, so no error curve. */
|
|
384
|
+
export interface ClusteringInfoV2 extends ClusteringInfoFields {
|
|
385
|
+
/** The format of the clustering's bytes (see `ClusteringInfoV3.formatVersion`). */
|
|
386
|
+
formatVersion: 2;
|
|
387
|
+
totalWeight: null;
|
|
388
|
+
mse: null;
|
|
389
|
+
explained: null;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** What `getClusteringInfo` returns: check `formatVersion` to narrow to the 3.x or 2.x shape. */
|
|
393
|
+
export type ClusteringInfo = ClusteringInfoV3 | ClusteringInfoV2;
|
|
394
|
+
|
|
395
|
+
/** Reads what a clustering holds, including its error curve for choosing a palette size. */
|
|
396
|
+
export function getClusteringInfo(clustering: Clustering): Promise<ClusteringInfo>;
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Reduces `image` to at most `k` colours and returns the mapped image as the same kind of thing you gave: a `Uint8Array`
|
|
400
|
+
* for a `Uint8Array`, `ImageData` for `ImageData`, an object with the same `format` for a `RawImage` (`ImageResult`; a
|
|
401
|
+
* value typed as the union `ImageLike` gives the union). Each pixel keeps its own alpha.
|
|
402
|
+
*
|
|
403
|
+
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
404
|
+
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
405
|
+
*/
|
|
406
|
+
export function quantize<I extends ImageLike>(image: I, k: number, options?: QuantizeOptions): Promise<ImageResult<I>>;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Maps `image` to the `k`-colour cut of a clustering you already have, so trying another `k` or another image mapping
|
|
410
|
+
* costs milliseconds. Matches in the clustering's colour space unless `colorSpace` is given. Returns the same kind of
|
|
411
|
+
* thing as `quantize`.
|
|
412
|
+
*
|
|
413
|
+
* @param k The most palette entries in the result (`k` means this in every function). A flat palette has no transparent
|
|
414
|
+
* entry, so its entries are all colours. Fewer come back when the image has fewer distinct colours.
|
|
415
|
+
*/
|
|
416
|
+
export function quantizeFromClustering<I extends ImageLike>(image: I, clustering: Clustering, k: number, options?: MappingOptions): Promise<ImageResult<I>>;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Maps each pixel of `image` to its nearest colour in a palette you supply (an edited swatch, a brand palette, one cut
|
|
420
|
+
* from a clustering). Pixels are matched in the palette's own colour space if it is a `Palette` (one this library made),
|
|
421
|
+
* so an OKLab palette maps the way `quantizeFromClustering` does; plain bytes carry no space and are matched in RGB. Name a
|
|
422
|
+
* `colorSpace` to override either. Returns the same kind of thing as `quantize`.
|
|
423
|
+
*/
|
|
424
|
+
export function quantizeWithPalette<I extends ImageLike>(image: I, palette: PaletteLike, options?: MappingOptions): Promise<ImageResult<I>>;
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Like `quantize`, but returns each pixel's palette index instead of its colour, with the palette (for palette
|
|
428
|
+
* PNGs, pixel art and pattern generators). The palette is the one `getPalette` gives, except that with `transparent`
|
|
429
|
+
* pixels below the cutoff are left out of it.
|
|
430
|
+
*
|
|
431
|
+
* @param k The most palette entries in the result (`k` means this in every function), counting the transparent entry
|
|
432
|
+
* if there is one, so `256` is the most a GIF or palette PNG holds. Fewer come back when the image has fewer distinct colours.
|
|
433
|
+
*/
|
|
434
|
+
export function quantizeToIndices(image: ImageLike, k: number, options?: QuantizeIndexOptions): Promise<IndexedImage>;
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Like `quantizeFromClustering`, but returns palette indices. The palette is `getPaletteFromClustering`'s.
|
|
438
|
+
*
|
|
439
|
+
* @param k The most palette entries in the result (`k` means this in every function), counting the transparent entry
|
|
440
|
+
* if there is one, so `256` is the most a GIF or palette PNG holds. Fewer come back when the image has fewer distinct colours.
|
|
441
|
+
*/
|
|
442
|
+
export function quantizeToIndicesFromClustering(image: ImageLike, clustering: Clustering, k: number, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
75
443
|
|
|
76
|
-
|
|
77
|
-
export function
|
|
78
|
-
export
|
|
444
|
+
/** Like `quantizeWithPalette`, but returns indices into `palette`, which is returned too. */
|
|
445
|
+
export function quantizeToIndicesWithPalette(image: ImageLike, palette: PaletteLike, options?: MappingIndexOptions): Promise<IndexedImage>;
|
|
446
|
+
export {};
|