agglomerative-clustering 3.0.0-beta.7 → 3.1.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/dist/shared.cjs CHANGED
@@ -6,18 +6,23 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.Clustering = Clustering;
7
7
  exports.MARK = void 0;
8
8
  exports.Palette = Palette;
9
- exports.version = exports.tooLarge = exports.snapshot = exports.sizeText = exports.revive = exports.readHeader = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageDataLike = exports.formatVersion = exports.fail = exports.errorCodes = exports.colorSpaces = exports.colorSpaceName = exports.checkPalette = exports.checkImageDataPixels = void 0;
9
+ exports.version = exports.tooLarge = exports.snapshot = exports.sizeText = exports.revive = exports.readHeader = exports.parseDither = exports.maxBytes = exports.markPalette = exports.markClustering = exports.mark = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isImageData = exports.isDetached = exports.isCodedError = exports.isClustering = exports.isArrayBufferLike = exports.imageHint = exports.imageFormats = exports.imageDataLike = exports.formatVersion = exports.fail = exports.errorCodes = exports.ditherSteps = exports.ditherMethods = exports.colorSpaces = exports.colorSpaceName = exports.colorSpaceCodes = exports.checkPalette = exports.checkImageDataPixels = exports.alphaModes = void 0;
10
10
  // Shared by both entries (index.js and worker.js) and the worker thread: errors, option snapshots, reading a clustering's
11
11
  // header, the Palette and Clustering types, and the markers that carry those types across the worker boundary. Imported as
12
12
  // './shared.js'; the build rewrites that to './shared.mjs' or './shared.cjs' (scripts/babel-import-extensions.cjs).
13
13
 
14
14
  // The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
15
15
  // is built from, src/version.hpp) in step with it. Do not edit it by hand.
16
- const version = exports.version = '3.0.0-beta.7';
17
- const colorSpaces = exports.colorSpaces = {
16
+ const version = exports.version = '3.1.0';
17
+ const colorSpaceCodes = exports.colorSpaceCodes = {
18
18
  'rgb': 0,
19
19
  'oklab': 1
20
20
  };
21
+ // Every option whose value is one of a fixed set of names has the set exported as a frozen list (like errorCodes,
22
+ // ditherMethods and ditherSteps), for building a choice from it or checking a value against it.
23
+ const colorSpaces = exports.colorSpaces = Object.freeze(Object.keys(colorSpaceCodes));
24
+ const imageFormats = exports.imageFormats = Object.freeze(['rgb', 'rgba']);
25
+ const alphaModes = exports.alphaModes = Object.freeze(['weight', 'ignore']);
21
26
  const colorSpaceName = code => code === 1 ? 'oklab' : 'rgb';
22
27
 
23
28
  // Every error this library throws is an Error with a `code` saying what went wrong, so callers needn't match on message
@@ -167,6 +172,93 @@ const snapshot = options => {
167
172
  return copy;
168
173
  };
169
174
 
175
+ // ----- Dithering ------------------------------------------------------------------------------------------------------
176
+ // The dither methods and their codes in C, and the step counts ordered dithering takes (the counts that divide the 64
177
+ // cells of its 8x8 matrix evenly). Steps 0 tells C to choose: 64 for a palette of two colours, else 16.
178
+ exports.snapshot = snapshot;
179
+ const ditherMethodCodes = {
180
+ 'floyd-steinberg': 0,
181
+ 'atkinson': 1,
182
+ 'ordered': 2
183
+ };
184
+ // The method names and step counts, exported (like errorCodes) for code that builds options from a form.
185
+ const ditherMethods = exports.ditherMethods = Object.freeze(Object.keys(ditherMethodCodes));
186
+ const ditherSteps = exports.ditherSteps = Object.freeze([2, 4, 8, 16, 32, 64]);
187
+
188
+ // A short description of a value for an error message.
189
+ const describe = value => typeof value === 'string' ? `'${value}'` : typeof value === 'number' || typeof value === 'boolean' || value === null ? String(value) : Array.isArray(value) ? 'an array' : typeof value === 'object' ? `a ${Object.prototype.toString.call(value).slice(8, -1)}` : typeof value;
190
+
191
+ // Reads every form of the dither option: false/undefined (off), true or a strength from 0 to 1 (Floyd-Steinberg, as in
192
+ // 3.0), a method name, or an object { method, strength, steps } that the plain forms are shorthands for.
193
+ // Returns {strength, method, steps}, with strength 0 meaning no dithering and steps 0 meaning the default.
194
+ const parseDither = value => {
195
+ const invalid = (key, message) => fail('invalid-argument', message, key === undefined ? 'options.dither' : `options.dither.${key}`);
196
+ // What a value from a form most likely meant: 'none' for no dithering, or a number given as a string.
197
+ const numeric = v => typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v));
198
+ const asNumber = v => numeric(v) ? ` It is a string: pass a number, for example Number(value).` : '';
199
+ const wrongForm = () => invalid(undefined, `Invalid dither: got ${describe(value)}; it must be a boolean, a strength from 0 to 1, 'floyd-steinberg', 'atkinson', 'ordered', or an object { method, strength, steps }.${typeof value === 'string' && ['none', 'off', 'false'].includes(value.trim().toLowerCase()) ? ' Use false for no dithering.' : asNumber(value)}`);
200
+ const has = key => Object.prototype.hasOwnProperty.call(ditherMethodCodes, key);
201
+ if (value === undefined || value === false) {
202
+ return {
203
+ strength: 0,
204
+ method: 0,
205
+ steps: 0
206
+ };
207
+ }
208
+ if (value === true) {
209
+ return {
210
+ strength: 1,
211
+ method: 0,
212
+ steps: 0
213
+ };
214
+ }
215
+ if (typeof value === 'number') {
216
+ if (!Number.isFinite(value) || value < 0 || value > 1) {
217
+ throw wrongForm();
218
+ }
219
+ return {
220
+ strength: value,
221
+ method: 0,
222
+ steps: 0
223
+ };
224
+ }
225
+ if (typeof value === 'string' && has(value)) {
226
+ return {
227
+ strength: 1,
228
+ method: ditherMethodCodes[value],
229
+ steps: 0
230
+ };
231
+ }
232
+ if (!isOptionsObject(value)) {
233
+ throw wrongForm();
234
+ }
235
+ const object = snapshot(value);
236
+ for (const key of Object.keys(object)) {
237
+ if (!['method', 'strength', 'steps'].includes(key)) {
238
+ throw invalid(key, `Invalid dither option '${key}': expected method, strength or steps.`);
239
+ }
240
+ }
241
+ const methodName = object.method === undefined ? 'floyd-steinberg' : object.method;
242
+ if (typeof methodName !== 'string' || !has(methodName)) {
243
+ throw invalid('method', `Invalid dither.method: got ${describe(methodName)}; it must be 'floyd-steinberg', 'atkinson' or 'ordered'.`);
244
+ }
245
+ const strength = object.strength === undefined ? 1 : object.strength;
246
+ if (typeof strength !== 'number' || !Number.isFinite(strength) || strength < 0 || strength > 1) {
247
+ throw invalid('strength', `Invalid dither.strength: got ${describe(strength)}; it must be a number from 0 to 1.${asNumber(strength)}`);
248
+ }
249
+ if (object.steps !== undefined && methodName !== 'ordered') {
250
+ throw invalid('steps', `Invalid dither.steps: only ordered dithering takes steps, so write dither: { method: 'ordered', steps }.`);
251
+ }
252
+ if (object.steps !== undefined && !ditherSteps.includes(object.steps)) {
253
+ throw invalid('steps', `Invalid dither.steps: got ${describe(object.steps)}; it must be 2, 4, 8, 16, 32 or 64.${asNumber(object.steps)}`);
254
+ }
255
+ return {
256
+ strength,
257
+ method: ditherMethodCodes[methodName],
258
+ steps: object.steps === undefined ? 0 : object.steps
259
+ };
260
+ };
261
+
170
262
  // ----- Palette and Clustering ----------------------------------------------------------------------------------------
171
263
  // Palettes and clusterings are byte arrays, and nothing about a Uint8Array says which one it is, or (for a palette) which
172
264
  // colour space its colours were made in. So the ones this library returns are ordinary Uint8Arrays carrying two hidden,
@@ -176,7 +268,7 @@ const snapshot = options => {
176
268
  // IndexedDB) give plain untagged Uint8Arrays. `Palette` and `Clustering` are how to make one by hand, and `instanceof`
177
269
  // recognises them. The tags are found by a symbol registered with Symbol.for, so a copy of the library loaded twice (the
178
270
  // ES module and CommonJS builds, a bundler's duplicate, the worker entry's own copy) recognises the other's.
179
- exports.snapshot = snapshot;
271
+ exports.parseDither = parseDither;
180
272
  const paletteBrand = Symbol.for('agglomerative-clustering.palette');
181
273
  const clusteringBrand = Symbol.for('agglomerative-clustering.clustering');
182
274
  const isPalette = value => isUint8Array(value) && value[paletteBrand] === true;
@@ -223,7 +315,7 @@ const markClustering = bytes => {
223
315
  // checks would trip over.
224
316
  exports.markClustering = markClustering;
225
317
  function Palette(source, colorSpace = 'rgb') {
226
- if (!Object.keys(colorSpaces).includes(colorSpace)) {
318
+ if (!colorSpaces.includes(colorSpace)) {
227
319
  throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'colorSpace');
228
320
  }
229
321
  if (source === undefined) {
package/dist/shared.mjs CHANGED
@@ -4,11 +4,16 @@
4
4
 
5
5
  // The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
6
6
  // is built from, src/version.hpp) in step with it. Do not edit it by hand.
7
- export const version = '3.0.0-beta.7';
8
- export const colorSpaces = {
7
+ export const version = '3.1.0';
8
+ export const colorSpaceCodes = {
9
9
  'rgb': 0,
10
10
  'oklab': 1
11
11
  };
12
+ // Every option whose value is one of a fixed set of names has the set exported as a frozen list (like errorCodes,
13
+ // ditherMethods and ditherSteps), for building a choice from it or checking a value against it.
14
+ export const colorSpaces = Object.freeze(Object.keys(colorSpaceCodes));
15
+ export const imageFormats = Object.freeze(['rgb', 'rgba']);
16
+ export const alphaModes = Object.freeze(['weight', 'ignore']);
12
17
  export const colorSpaceName = code => code === 1 ? 'oklab' : 'rgb';
13
18
 
14
19
  // Every error this library throws is an Error with a `code` saying what went wrong, so callers needn't match on message
@@ -144,6 +149,92 @@ export const snapshot = options => {
144
149
  return copy;
145
150
  };
146
151
 
152
+ // ----- Dithering ------------------------------------------------------------------------------------------------------
153
+ // The dither methods and their codes in C, and the step counts ordered dithering takes (the counts that divide the 64
154
+ // cells of its 8x8 matrix evenly). Steps 0 tells C to choose: 64 for a palette of two colours, else 16.
155
+ const ditherMethodCodes = {
156
+ 'floyd-steinberg': 0,
157
+ 'atkinson': 1,
158
+ 'ordered': 2
159
+ };
160
+ // The method names and step counts, exported (like errorCodes) for code that builds options from a form.
161
+ export const ditherMethods = Object.freeze(Object.keys(ditherMethodCodes));
162
+ export const ditherSteps = Object.freeze([2, 4, 8, 16, 32, 64]);
163
+
164
+ // A short description of a value for an error message.
165
+ const describe = value => typeof value === 'string' ? `'${value}'` : typeof value === 'number' || typeof value === 'boolean' || value === null ? String(value) : Array.isArray(value) ? 'an array' : typeof value === 'object' ? `a ${Object.prototype.toString.call(value).slice(8, -1)}` : typeof value;
166
+
167
+ // Reads every form of the dither option: false/undefined (off), true or a strength from 0 to 1 (Floyd-Steinberg, as in
168
+ // 3.0), a method name, or an object { method, strength, steps } that the plain forms are shorthands for.
169
+ // Returns {strength, method, steps}, with strength 0 meaning no dithering and steps 0 meaning the default.
170
+ export const parseDither = value => {
171
+ const invalid = (key, message) => fail('invalid-argument', message, key === undefined ? 'options.dither' : `options.dither.${key}`);
172
+ // What a value from a form most likely meant: 'none' for no dithering, or a number given as a string.
173
+ const numeric = v => typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v));
174
+ const asNumber = v => numeric(v) ? ` It is a string: pass a number, for example Number(value).` : '';
175
+ const wrongForm = () => invalid(undefined, `Invalid dither: got ${describe(value)}; it must be a boolean, a strength from 0 to 1, 'floyd-steinberg', 'atkinson', 'ordered', or an object { method, strength, steps }.${typeof value === 'string' && ['none', 'off', 'false'].includes(value.trim().toLowerCase()) ? ' Use false for no dithering.' : asNumber(value)}`);
176
+ const has = key => Object.prototype.hasOwnProperty.call(ditherMethodCodes, key);
177
+ if (value === undefined || value === false) {
178
+ return {
179
+ strength: 0,
180
+ method: 0,
181
+ steps: 0
182
+ };
183
+ }
184
+ if (value === true) {
185
+ return {
186
+ strength: 1,
187
+ method: 0,
188
+ steps: 0
189
+ };
190
+ }
191
+ if (typeof value === 'number') {
192
+ if (!Number.isFinite(value) || value < 0 || value > 1) {
193
+ throw wrongForm();
194
+ }
195
+ return {
196
+ strength: value,
197
+ method: 0,
198
+ steps: 0
199
+ };
200
+ }
201
+ if (typeof value === 'string' && has(value)) {
202
+ return {
203
+ strength: 1,
204
+ method: ditherMethodCodes[value],
205
+ steps: 0
206
+ };
207
+ }
208
+ if (!isOptionsObject(value)) {
209
+ throw wrongForm();
210
+ }
211
+ const object = snapshot(value);
212
+ for (const key of Object.keys(object)) {
213
+ if (!['method', 'strength', 'steps'].includes(key)) {
214
+ throw invalid(key, `Invalid dither option '${key}': expected method, strength or steps.`);
215
+ }
216
+ }
217
+ const methodName = object.method === undefined ? 'floyd-steinberg' : object.method;
218
+ if (typeof methodName !== 'string' || !has(methodName)) {
219
+ throw invalid('method', `Invalid dither.method: got ${describe(methodName)}; it must be 'floyd-steinberg', 'atkinson' or 'ordered'.`);
220
+ }
221
+ const strength = object.strength === undefined ? 1 : object.strength;
222
+ if (typeof strength !== 'number' || !Number.isFinite(strength) || strength < 0 || strength > 1) {
223
+ throw invalid('strength', `Invalid dither.strength: got ${describe(strength)}; it must be a number from 0 to 1.${asNumber(strength)}`);
224
+ }
225
+ if (object.steps !== undefined && methodName !== 'ordered') {
226
+ throw invalid('steps', `Invalid dither.steps: only ordered dithering takes steps, so write dither: { method: 'ordered', steps }.`);
227
+ }
228
+ if (object.steps !== undefined && !ditherSteps.includes(object.steps)) {
229
+ throw invalid('steps', `Invalid dither.steps: got ${describe(object.steps)}; it must be 2, 4, 8, 16, 32 or 64.${asNumber(object.steps)}`);
230
+ }
231
+ return {
232
+ strength,
233
+ method: ditherMethodCodes[methodName],
234
+ steps: object.steps === undefined ? 0 : object.steps
235
+ };
236
+ };
237
+
147
238
  // ----- Palette and Clustering ----------------------------------------------------------------------------------------
148
239
  // Palettes and clusterings are byte arrays, and nothing about a Uint8Array says which one it is, or (for a palette) which
149
240
  // colour space its colours were made in. So the ones this library returns are ordinary Uint8Arrays carrying two hidden,
@@ -195,7 +286,7 @@ export const markClustering = bytes => {
195
286
  // A function and not a `class`, because it returns a plain Uint8Array and answers `instanceof` itself, which transpiled class
196
287
  // checks would trip over.
197
288
  export function Palette(source, colorSpace = 'rgb') {
198
- if (!Object.keys(colorSpaces).includes(colorSpace)) {
289
+ if (!colorSpaces.includes(colorSpace)) {
199
290
  throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'colorSpace');
200
291
  }
201
292
  if (source === undefined) {
@@ -95,6 +95,20 @@ const optionsIndex = {
95
95
  // One AbortController per call that can be cancelled, by call id. It exists from the moment the call arrives, so a call
96
96
  // still waiting its turn can be cancelled and never starts.
97
97
  const controllers = new Map();
98
+
99
+ // A turn of the event loop, so messages already sent to this thread (cancels) are read first. In Node, setImmediate runs
100
+ // after the pending messages. In a browser, a message to itself on a MessageChannel: it is queued behind the messages
101
+ // already waiting, as they are posted messages too, and unlike a zero timeout it isn't stretched to 4 ms when timers
102
+ // nest (as they do along a queue of calls), which made each skipped call cost 4 ms.
103
+ const nextTurn = typeof setImmediate === 'function' ? () => new Promise(resolve => setImmediate(resolve)) : typeof MessageChannel === 'function' ? (() => {
104
+ const channel = new MessageChannel(),
105
+ waiting = [];
106
+ channel.port1.onmessage = () => waiting.shift()();
107
+ return () => new Promise(resolve => {
108
+ waiting.push(resolve);
109
+ channel.port2.postMessage(null);
110
+ });
111
+ })() : () => new Promise(resolve => setTimeout(resolve, 0));
98
112
  const respond = async ({
99
113
  id,
100
114
  method,
@@ -103,10 +117,15 @@ const respond = async ({
103
117
  }, post) => {
104
118
  const controller = controllers.get(id);
105
119
  try {
120
+ // The call before this one may have run in one piece (mapping does), with no turn of the event loop for a cancel
121
+ // of this call to arrive; take one before starting, so a call cancelled while it waited never runs.
122
+ if (controller && !controller.signal.aborted) {
123
+ await nextTurn();
124
+ }
106
125
  if (controller && controller.signal.aborted) {
107
126
  return;
108
127
  }
109
- if (!Object.hasOwn(methods, method)) {
128
+ if (!Object.prototype.hasOwnProperty.call(methods, method)) {
110
129
  throw Object.assign(new Error(`Unknown method '${method}'.`), {
111
130
  code: 'invalid-argument'
112
131
  });
package/dist/worker.cjs CHANGED
@@ -17,14 +17,45 @@ Object.defineProperty(exports, "Palette", {
17
17
  return _shared.Palette;
18
18
  }
19
19
  });
20
+ Object.defineProperty(exports, "alphaModes", {
21
+ enumerable: true,
22
+ get: function () {
23
+ return _shared.alphaModes;
24
+ }
25
+ });
26
+ Object.defineProperty(exports, "colorSpaces", {
27
+ enumerable: true,
28
+ get: function () {
29
+ return _shared.colorSpaces;
30
+ }
31
+ });
20
32
  exports.createWorker = void 0;
33
+ Object.defineProperty(exports, "ditherMethods", {
34
+ enumerable: true,
35
+ get: function () {
36
+ return _shared.ditherMethods;
37
+ }
38
+ });
39
+ Object.defineProperty(exports, "ditherSteps", {
40
+ enumerable: true,
41
+ get: function () {
42
+ return _shared.ditherSteps;
43
+ }
44
+ });
21
45
  Object.defineProperty(exports, "errorCodes", {
22
46
  enumerable: true,
23
47
  get: function () {
24
48
  return _shared.errorCodes;
25
49
  }
26
50
  });
27
- exports.init = exports.getSwatchesFromClustering = exports.getSwatches = exports.getPaletteFromClustering = exports.getPalette = exports.getClusteringInfo = exports.getClustering = exports.formatVersion = void 0;
51
+ exports.getSwatchesFromClustering = exports.getSwatches = exports.getPaletteFromClustering = exports.getPalette = exports.getClusteringInfo = exports.getClustering = exports.formatVersion = void 0;
52
+ Object.defineProperty(exports, "imageFormats", {
53
+ enumerable: true,
54
+ get: function () {
55
+ return _shared.imageFormats;
56
+ }
57
+ });
58
+ exports.init = void 0;
28
59
  Object.defineProperty(exports, "isCodedError", {
29
60
  enumerable: true,
30
61
  get: function () {
@@ -448,6 +479,14 @@ const takeControls = (options, progressAllowed = true) => {
448
479
  };
449
480
  }
450
481
  options = (0, _shared.snapshot)(options);
482
+ // The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
483
+ // at all, for a Proxy or a function): read it here the way the main entry does, and send a plain copy.
484
+ if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
485
+ // One read of each setting (a getter could answer differently twice): the copy is what is checked and sent.
486
+ const copy = (0, _shared.isOptionsObject)(options.dither) ? Object.assign({}, (0, _shared.snapshot)(options.dither)) : options.dither;
487
+ (0, _shared.parseDither)(copy);
488
+ options.dither = copy;
489
+ }
451
490
  const {
452
491
  signal,
453
492
  onProgress
package/dist/worker.d.cts CHANGED
@@ -17,6 +17,9 @@ export { Clustering } from './index.cjs';
17
17
  /** The same list as the main entry's `errorCodes`: the worker entry's errors have the same codes. */
18
18
  export { errorCodes } from './index.cjs';
19
19
 
20
+ /** The same lists as the main entry's: every colour space, image format, alpha mode, dither method and step count. */
21
+ export { colorSpaces, imageFormats, alphaModes, ditherMethods, ditherSteps } from './index.cjs';
22
+
20
23
  /** The main entry's `isCodedError`: true for this library's errors, from either entry. */
21
24
  export { isCodedError } from './index.cjs';
22
25
 
@@ -27,8 +30,8 @@ export const version: string;
27
30
  export const formatVersion: number;
28
31
 
29
32
  export type {
30
- ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, ClusteringOptions, MappingOptions, QuantizeOptions,
31
- ErrorCode, ErrorArgument, CodedError, InitOptions, RunOptions, CancelOptions, IndexOptions, QuantizeIndexOptions, MappingIndexOptions, IndexedImage, ClusteringInfo, Swatches,
33
+ ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, DitherMethod, DitherOptions, DitherSteps, ClusteringOptions, MappingOptions, QuantizeOptions,
34
+ ErrorCode, ErrorArgument, CodedError, InitOptions, ProgressOptions, CancelOptions, IndexOptions, QuantizeIndexOptions, MappingIndexOptions, IndexedImage, ClusteringInfo, Swatches,
32
35
  } from './index.cjs';
33
36
 
34
37
  /** Options for `createWorker`. */
package/dist/worker.d.ts CHANGED
@@ -17,6 +17,9 @@ export { Clustering } from './index.js';
17
17
  /** The same list as the main entry's `errorCodes`: the worker entry's errors have the same codes. */
18
18
  export { errorCodes } from './index.js';
19
19
 
20
+ /** The same lists as the main entry's: every colour space, image format, alpha mode, dither method and step count. */
21
+ export { colorSpaces, imageFormats, alphaModes, ditherMethods, ditherSteps } from './index.js';
22
+
20
23
  /** The main entry's `isCodedError`: true for this library's errors, from either entry. */
21
24
  export { isCodedError } from './index.js';
22
25
 
@@ -27,8 +30,8 @@ export const version: string;
27
30
  export const formatVersion: number;
28
31
 
29
32
  export type {
30
- ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, ClusteringOptions, MappingOptions, QuantizeOptions,
31
- ErrorCode, ErrorArgument, CodedError, InitOptions, RunOptions, CancelOptions, IndexOptions, QuantizeIndexOptions, MappingIndexOptions, IndexedImage, ClusteringInfo, Swatches,
33
+ ImageFormat, RawImage, ImageLike, PaletteLike, PlatformImageData, ImageResult, WasmModule, ColorSpace, AlphaMode, DitherMethod, DitherOptions, DitherSteps, ClusteringOptions, MappingOptions, QuantizeOptions,
34
+ ErrorCode, ErrorArgument, CodedError, InitOptions, ProgressOptions, CancelOptions, IndexOptions, QuantizeIndexOptions, MappingIndexOptions, IndexedImage, ClusteringInfo, Swatches,
32
35
  } from './index.js';
33
36
 
34
37
  /** Options for `createWorker`. */
package/dist/worker.mjs CHANGED
@@ -4,10 +4,10 @@
4
4
  // handle, so re-cutting and re-mapping it copies nothing across. The functions exported directly use one shared
5
5
  // worker, started on first use.
6
6
 
7
- import { fail, isOptionsObject, snapshot, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge } from "./shared.mjs";
7
+ import { fail, isOptionsObject, snapshot, parseDither, mark, revive, latestVersion, imageHint, isDetached, imageDataLike, isImageData, checkImageDataPixels, maxBytes, tooLarge } from "./shared.mjs";
8
8
 
9
9
  // The same version, types and data-format version as the main entry (one implementation, in shared.js).
10
- export { version, Palette, Clustering, errorCodes, isCodedError } from "./shared.mjs";
10
+ export { version, Palette, Clustering, errorCodes, isCodedError, ditherMethods, ditherSteps, colorSpaces, imageFormats, alphaModes } from "./shared.mjs";
11
11
  export const formatVersion = latestVersion;
12
12
 
13
13
  // Node's worker_threads, where this script runs from a file in Node, Bun or Deno (from npm). A runtime that only imitates Node
@@ -411,6 +411,14 @@ const takeControls = (options, progressAllowed = true) => {
411
411
  };
412
412
  }
413
413
  options = snapshot(options);
414
+ // The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
415
+ // at all, for a Proxy or a function): read it here the way the main entry does, and send a plain copy.
416
+ if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
417
+ // One read of each setting (a getter could answer differently twice): the copy is what is checked and sent.
418
+ const copy = isOptionsObject(options.dither) ? Object.assign({}, snapshot(options.dither)) : options.dither;
419
+ parseDither(copy);
420
+ options.dither = copy;
421
+ }
414
422
  const {
415
423
  signal,
416
424
  onProgress
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agglomerative-clustering",
3
- "version": "3.0.0-beta.7",
3
+ "version": "3.1.0",
4
4
  "description": "Colour palette extraction and image quantization with exact Ward clustering, in WebAssembly. Cluster an image once, then cut it to a palette of any size.",
5
5
  "license": "MIT",
6
6
  "author": "Rohan Fredriksson",