agglomerative-clustering 2.0.0-beta.1 → 3.0.0-beta.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/README.md +455 -108
- package/dist/clustering.js +2 -14
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +995 -150
- package/dist/index.d.cts +446 -0
- package/dist/index.d.ts +389 -21
- package/dist/index.mjs +977 -147
- package/dist/shared.cjs +349 -0
- package/dist/shared.mjs +340 -0
- package/dist/worker-thread.mjs +429 -0
- package/dist/worker.d.ts +117 -0
- package/dist/worker.mjs +953 -0
- package/package.json +64 -26
package/dist/shared.cjs
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Clustering = Clustering;
|
|
7
|
+
exports.MARK = void 0;
|
|
8
|
+
exports.Palette = Palette;
|
|
9
|
+
exports.version = exports.snapshot = exports.revive = exports.readHeader = exports.markPalette = exports.markClustering = exports.mark = exports.latestVersion = exports.isUint8ClampedArray = exports.isUint8Array = exports.isPalette = exports.isOptionsObject = exports.isClustering = exports.isArrayBufferLike = exports.formatVersion = exports.fail = exports.colorSpaces = exports.colorSpaceName = exports.checkPalette = void 0;
|
|
10
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
11
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
12
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
13
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
14
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
15
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
16
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
17
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
|
|
18
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
19
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
20
|
+
function _construct(t, e, r) { if (_isNativeReflectConstruct()) return Reflect.construct.apply(null, arguments); var o = [null]; o.push.apply(o, e); var p = new (t.bind.apply(t, o))(); return r && _setPrototypeOf(p, r.prototype), p; }
|
|
21
|
+
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
|
|
22
|
+
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
|
|
23
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
24
|
+
// Shared by both entries (index.js and worker.js) and the worker thread: errors, option snapshots, reading a clustering's
|
|
25
|
+
// header, the Palette and Clustering types, and the markers that carry those types across the worker boundary. Imported as
|
|
26
|
+
// './shared.js'; the build rewrites that to './shared.mjs' or './shared.cjs' (scripts/babel-import-extensions.cjs).
|
|
27
|
+
|
|
28
|
+
// The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
|
|
29
|
+
// is built from, src/version.hpp) in step with it. Do not edit it by hand.
|
|
30
|
+
var version = exports.version = '3.0.0-beta.1';
|
|
31
|
+
var colorSpaces = exports.colorSpaces = {
|
|
32
|
+
'rgb': 0,
|
|
33
|
+
'oklab': 1
|
|
34
|
+
};
|
|
35
|
+
var colorSpaceName = exports.colorSpaceName = function colorSpaceName(code) {
|
|
36
|
+
return code === 1 ? 'oklab' : 'rgb';
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// Every error this library throws is an Error with a `code` saying what went wrong, so callers needn't match on message
|
|
40
|
+
// text: 'invalid-argument' (image, k, options, palette), 'invalid-clustering', 'unsupported-version' (a clustering from a
|
|
41
|
+
// newer format, or an older one without what was asked for), 'out-of-memory', 'wasm-load-failed', and in the worker entry
|
|
42
|
+
// 'worker-terminated', 'worker-failed' and 'invalid-handle'. When the error is about one argument, `argument` names it
|
|
43
|
+
// for programs (the message is for people): 'image', 'k', 'clustering', 'palette', 'options', or 'options.<key>' for
|
|
44
|
+
// one option.
|
|
45
|
+
var fail = exports.fail = function fail(code, message, argument) {
|
|
46
|
+
return Object.assign(new Error(message), argument === undefined ? {
|
|
47
|
+
code: code
|
|
48
|
+
} : {
|
|
49
|
+
code: code,
|
|
50
|
+
argument: argument
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Type checks by tag rather than `instanceof`, so values from another realm (an iframe, a vm context, a test sandbox,
|
|
55
|
+
// whose Uint8Array is a different constructor) are recognised. A Node Buffer is a Uint8Array by tag too.
|
|
56
|
+
var tag = function tag(value) {
|
|
57
|
+
return Object.prototype.toString.call(value);
|
|
58
|
+
};
|
|
59
|
+
var isUint8Array = exports.isUint8Array = function isUint8Array(value) {
|
|
60
|
+
return ArrayBuffer.isView(value) && tag(value) === '[object Uint8Array]';
|
|
61
|
+
};
|
|
62
|
+
var isUint8ClampedArray = exports.isUint8ClampedArray = function isUint8ClampedArray(value) {
|
|
63
|
+
return ArrayBuffer.isView(value) && tag(value) === '[object Uint8ClampedArray]';
|
|
64
|
+
};
|
|
65
|
+
var isArrayBufferLike = exports.isArrayBufferLike = function isArrayBufferLike(value) {
|
|
66
|
+
return tag(value) === '[object ArrayBuffer]' || tag(value) === '[object SharedArrayBuffer]';
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// An options object is an ordinary object. A Map, Date, ArrayBuffer or typed array has no own keys and would pass as
|
|
70
|
+
// `{}`, silently ignoring what the caller meant. Checked by tag rather than prototype so objects from another realm
|
|
71
|
+
// (an iframe, a test sandbox) are fine.
|
|
72
|
+
var isOptionsObject = exports.isOptionsObject = function isOptionsObject(value) {
|
|
73
|
+
return value !== null && _typeof(value) === 'object' && Object.prototype.toString.call(value) === '[object Object]';
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// One plain copy of an options object, taken once: every enumerable property, own or inherited (so options layered with
|
|
77
|
+
// Object.create(defaults) work and an inherited typo is still caught), each read a single time. Everything after this
|
|
78
|
+
// uses the copy, so what is validated is what is used, even if a property is a getter that answers differently each time.
|
|
79
|
+
// The copy has no prototype, so a key like '__proto__' is just a name.
|
|
80
|
+
var snapshot = exports.snapshot = function snapshot(options) {
|
|
81
|
+
var copy = Object.create(null);
|
|
82
|
+
for (var key in options) {
|
|
83
|
+
copy[key] = options[key];
|
|
84
|
+
}
|
|
85
|
+
return copy;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// ----- Palette and Clustering ----------------------------------------------------------------------------------------
|
|
89
|
+
// Palettes and clusterings are byte arrays, and nothing about a Uint8Array says which one it is, or (for a palette) which
|
|
90
|
+
// colour space its colours were made in. So the ones this library returns are ordinary Uint8Arrays carrying two hidden,
|
|
91
|
+
// non-enumerable tags: what they are (a symbol property) and, for a palette, `colorSpace`. Being plain Uint8Arrays is the
|
|
92
|
+
// point: everything that takes bytes takes them (indexing, `subarray`, files, encoders), `constructor` is Uint8Array, so
|
|
93
|
+
// `toEqual(new Uint8Array(...))` in a test still passes, and `slice`, `subarray` and structured clones (postMessage,
|
|
94
|
+
// IndexedDB) give plain untagged Uint8Arrays. `Palette` and `Clustering` are how to make one by hand, and `instanceof`
|
|
95
|
+
// recognises them. The tags are found by a symbol registered with Symbol.for, so a copy of the library loaded twice (the
|
|
96
|
+
// ES module and CommonJS builds, a bundler's duplicate, the worker entry's own copy) recognises the other's.
|
|
97
|
+
var paletteBrand = Symbol["for"]('agglomerative-clustering.palette');
|
|
98
|
+
var clusteringBrand = Symbol["for"]('agglomerative-clustering.clustering');
|
|
99
|
+
var isPalette = exports.isPalette = function isPalette(value) {
|
|
100
|
+
return isUint8Array(value) && value[paletteBrand] === true;
|
|
101
|
+
};
|
|
102
|
+
var isClustering = exports.isClustering = function isClustering(value) {
|
|
103
|
+
return isUint8Array(value) && value[clusteringBrand] === true;
|
|
104
|
+
};
|
|
105
|
+
// Node's console.log and util.inspect call this (other environments ignore it), so the tag shows when debugging:
|
|
106
|
+
// `Palette(oklab) [ 12, 34, ... ]` rather than a plain `Uint8Array(…)`.
|
|
107
|
+
var inspect = Symbol["for"]('nodejs.util.inspect.custom');
|
|
108
|
+
var view = function view(label) {
|
|
109
|
+
return function (depth, options, nodeInspect) {
|
|
110
|
+
var plain = new Uint8Array(this.buffer, this.byteOffset, this.length);
|
|
111
|
+
return "".concat(label(this), " ").concat(typeof nodeInspect === 'function' ? nodeInspect(plain, options) : "[".concat(plain.length, " bytes]"));
|
|
112
|
+
};
|
|
113
|
+
};
|
|
114
|
+
var paletteView = view(function (palette) {
|
|
115
|
+
return "Palette(".concat(palette.colorSpace, ")");
|
|
116
|
+
});
|
|
117
|
+
var clusteringView = view(function () {
|
|
118
|
+
return 'Clustering';
|
|
119
|
+
});
|
|
120
|
+
var markPalette = exports.markPalette = function markPalette(bytes, colorSpace) {
|
|
121
|
+
Object.defineProperty(bytes, paletteBrand, {
|
|
122
|
+
value: true
|
|
123
|
+
});
|
|
124
|
+
Object.defineProperty(bytes, 'colorSpace', {
|
|
125
|
+
value: colorSpace
|
|
126
|
+
});
|
|
127
|
+
Object.defineProperty(bytes, inspect, {
|
|
128
|
+
value: paletteView
|
|
129
|
+
});
|
|
130
|
+
return bytes;
|
|
131
|
+
};
|
|
132
|
+
var markClustering = exports.markClustering = function markClustering(bytes) {
|
|
133
|
+
Object.defineProperty(bytes, clusteringBrand, {
|
|
134
|
+
value: true
|
|
135
|
+
});
|
|
136
|
+
Object.defineProperty(bytes, inspect, {
|
|
137
|
+
value: clusteringView
|
|
138
|
+
});
|
|
139
|
+
return bytes;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// Flat RGB bytes with the colour space they were made in, so mapping with one matches the way it was made. `new Palette(bytes,
|
|
143
|
+
// 'oklab')` copies `bytes` (or makes `length` zeros, or takes an array of byte values) and returns a Uint8Array tagged so.
|
|
144
|
+
// A function and not a `class`, because it returns a plain Uint8Array and answers `instanceof` itself, which transpiled class
|
|
145
|
+
// checks would trip over.
|
|
146
|
+
function Palette(source) {
|
|
147
|
+
var colorSpace = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'rgb';
|
|
148
|
+
if (!Object.keys(colorSpaces).includes(colorSpace)) {
|
|
149
|
+
throw fail('invalid-argument', "Invalid colorSpace: must be 'rgb' or 'oklab'.", 'colorSpace');
|
|
150
|
+
}
|
|
151
|
+
if (source === undefined) {
|
|
152
|
+
source = 0;
|
|
153
|
+
}
|
|
154
|
+
// The same rules as for a palette passed to a function, so a Palette can't hold what they would reject: a number coerced to a
|
|
155
|
+
// byte (300 would become 44), a string turned into 0, or a length that isn't whole colours.
|
|
156
|
+
if (typeof source === 'number') {
|
|
157
|
+
if (!Number.isInteger(source) || source < 0 || source % 3 !== 0) {
|
|
158
|
+
throw fail('invalid-argument', "Invalid palette: a length must be a whole number of bytes, a multiple of 3 (flat RGB bytes).", 'palette');
|
|
159
|
+
}
|
|
160
|
+
} else {
|
|
161
|
+
checkPalette(source);
|
|
162
|
+
}
|
|
163
|
+
return markPalette(new Uint8Array(source), colorSpace);
|
|
164
|
+
}
|
|
165
|
+
Object.defineProperty(Palette, Symbol.hasInstance, {
|
|
166
|
+
value: isPalette
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// The bytes of a clustering, checked: `new Clustering(bytes)` is how stored clusterings get their type back. Arguments are
|
|
170
|
+
// those of a Uint8Array (bytes to copy, or a buffer with an offset and length to view).
|
|
171
|
+
function Clustering() {
|
|
172
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
173
|
+
args[_key] = arguments[_key];
|
|
174
|
+
}
|
|
175
|
+
var source = args[0];
|
|
176
|
+
// Bytes, or a buffer to view: never a number (a length would allocate and make an empty clustering) or anything a Uint8Array
|
|
177
|
+
// would coerce. A plain array must hold byte values, as for a palette.
|
|
178
|
+
var isBuffer = isArrayBufferLike(source);
|
|
179
|
+
if (!isUint8Array(source) && !isUint8ClampedArray(source) && !isBuffer && !Array.isArray(source)) {
|
|
180
|
+
throw fail('invalid-clustering', "Invalid clustering: expected its bytes (a Uint8Array, or an ArrayBuffer with an optional offset and length) from getClustering.", 'clustering');
|
|
181
|
+
}
|
|
182
|
+
if (Array.isArray(source) && !source.every(function (value) {
|
|
183
|
+
return Number.isInteger(value) && value >= 0 && value <= 255;
|
|
184
|
+
})) {
|
|
185
|
+
throw fail('invalid-clustering', "Invalid clustering: a plain array must hold byte values, integers from 0 to 255.", 'clustering');
|
|
186
|
+
}
|
|
187
|
+
var bytes;
|
|
188
|
+
try {
|
|
189
|
+
bytes = _construct(Uint8Array, args);
|
|
190
|
+
} catch (error) {
|
|
191
|
+
throw fail('invalid-clustering', "Invalid clustering: ".concat(error.message, "."), 'clustering');
|
|
192
|
+
}
|
|
193
|
+
readHeader(bytes);
|
|
194
|
+
return markClustering(bytes);
|
|
195
|
+
}
|
|
196
|
+
Object.defineProperty(Clustering, Symbol.hasInstance, {
|
|
197
|
+
value: isClustering
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Reads a clustering's header, or throws if it isn't a clustering this version can read. Clusterings start with the
|
|
201
|
+
// magic byte 0xAC and their version number (2 or 3). Clusterings from 1.x had no header and could only be recognised by
|
|
202
|
+
// their length, which many other byte strings share (a palette of the right size passed as a clustering was accepted
|
|
203
|
+
// and gave nonsense), so they are no longer read.
|
|
204
|
+
// version 2: [0xAC, 2, colour space, precision], then 9-byte records of colours
|
|
205
|
+
// version 3: [0xAC, 3, header length, record length, colour space, precision, weight unit, weight shift,
|
|
206
|
+
// total weight (u32 LE), 0, grouped error (f32 LE, from header length 17), ...], then records of
|
|
207
|
+
// colours, parent index, lighter weight and cost
|
|
208
|
+
var latestVersion = exports.latestVersion = 3;
|
|
209
|
+
|
|
210
|
+
// The version of stored data (clusterings) this build writes. It reads this one and format 2 (see readHeader). Unlike
|
|
211
|
+
// `version`, it changes only when the stored format does, so a 3.x release that keeps the format keeps this number.
|
|
212
|
+
var formatVersion = exports.formatVersion = latestVersion;
|
|
213
|
+
var readHeader = exports.readHeader = function readHeader(clustering) {
|
|
214
|
+
if (isPalette(clustering)) {
|
|
215
|
+
throw fail('invalid-clustering', "Invalid clustering: this is a palette, not a clustering. Palettes come from getPalette and getPaletteFromClustering; a clustering comes from getClustering.", 'clustering');
|
|
216
|
+
}
|
|
217
|
+
if (!isUint8Array(clustering)) {
|
|
218
|
+
throw fail('invalid-clustering', "Invalid clustering: must be a Uint8Array from getClustering.", 'clustering');
|
|
219
|
+
}
|
|
220
|
+
var length = clustering.length;
|
|
221
|
+
if (length >= 4 && clustering[0] === 0xAC) {
|
|
222
|
+
var _version = clustering[1];
|
|
223
|
+
if (_version === 2 && clustering[2] <= 1 && (length === 4 || length % 9 === 7)) {
|
|
224
|
+
return {
|
|
225
|
+
version: _version,
|
|
226
|
+
colorSpace: clustering[2],
|
|
227
|
+
precision: clustering[3],
|
|
228
|
+
transparent: 0,
|
|
229
|
+
headerLength: 4,
|
|
230
|
+
recordLength: 9,
|
|
231
|
+
merges: length === 4 ? 0 : (length - 7) / 9
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (_version === 3 && length >= 13) {
|
|
235
|
+
var headerLength = clustering[2],
|
|
236
|
+
recordLength = clustering[3];
|
|
237
|
+
if (headerLength >= 13 && recordLength >= 21 && headerLength <= length) {
|
|
238
|
+
// A header field with a fixed set of meanings (colour space, weight unit) that holds a value this version
|
|
239
|
+
// doesn't know is how a later release would add one, so it asks for an upgrade rather than misreading it.
|
|
240
|
+
if (clustering[4] > 1 || clustering[6] > 1) {
|
|
241
|
+
throw fail('unsupported-version', "Invalid clustering: it uses a colour space or weight unit that this version of agglomerative-clustering does not know. If it came from a newer version, upgrade the package; otherwise it is damaged.", 'clustering');
|
|
242
|
+
}
|
|
243
|
+
var merges = length < headerLength + 3 ? 0 : Math.floor((length - headerLength - 3) / recordLength);
|
|
244
|
+
var padding = length < headerLength + 3 ? length - headerLength : length - headerLength - 3 - merges * recordLength;
|
|
245
|
+
if (padding < 9) {
|
|
246
|
+
return {
|
|
247
|
+
version: _version,
|
|
248
|
+
colorSpace: clustering[4],
|
|
249
|
+
precision: clustering[5],
|
|
250
|
+
transparent: clustering[12],
|
|
251
|
+
headerLength: headerLength,
|
|
252
|
+
recordLength: recordLength,
|
|
253
|
+
merges: merges
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (_version > latestVersion) {
|
|
259
|
+
throw fail('unsupported-version', "Invalid clustering: format version ".concat(_version, " is newer than this version of agglomerative-clustering reads. Upgrade the package."), 'clustering');
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
throw fail('invalid-clustering', "Invalid clustering: not a clustering from getClustering. (Clusterings from agglomerative-clustering 1.x are no longer read: cluster the image again.)", 'clustering');
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// Palettes are flat RGB bytes. An empty palette is allowed: it is what getPalette gives for a fully transparent
|
|
266
|
+
// image, and quantize maps such an image the same way.
|
|
267
|
+
var checkPalette = exports.checkPalette = function checkPalette(palette) {
|
|
268
|
+
if (isClustering(palette)) {
|
|
269
|
+
throw fail('invalid-argument', "Invalid palette: this is a clustering (from getClustering), not a palette. Cut a palette from it with getPaletteFromClustering, or map with quantizeFromClustering.", 'palette');
|
|
270
|
+
}
|
|
271
|
+
if (!isUint8Array(palette) && !isUint8ClampedArray(palette) && !Array.isArray(palette)) {
|
|
272
|
+
throw fail('invalid-argument', "Invalid palette: must be a Uint8Array of RGB bytes.", 'palette');
|
|
273
|
+
}
|
|
274
|
+
if (palette.length % 3 !== 0) {
|
|
275
|
+
throw fail('invalid-argument', "Invalid palette: length must be a multiple of 3 (flat RGB bytes).", 'palette');
|
|
276
|
+
}
|
|
277
|
+
// Typed arrays are bytes already. A plain array is copied into bytes, which would turn an array of [r, g, b] colours
|
|
278
|
+
// into zeros, strings into numbers and 300 into 44 without a word, so it must hold byte values.
|
|
279
|
+
if (Array.isArray(palette)) {
|
|
280
|
+
var _iterator = _createForOfIteratorHelper(palette),
|
|
281
|
+
_step;
|
|
282
|
+
try {
|
|
283
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
284
|
+
var value = _step.value;
|
|
285
|
+
if (!Number.isInteger(value) || value < 0 || value > 255) {
|
|
286
|
+
throw fail('invalid-argument', "Invalid palette: a plain array must be flat RGB bytes, integers from 0 to 255 (not an array of [r, g, b] colours).", 'palette');
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
} catch (err) {
|
|
290
|
+
_iterator.e(err);
|
|
291
|
+
} finally {
|
|
292
|
+
_iterator.f();
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// ----- Across the worker boundary ------------------------------------------------------------------------------------
|
|
298
|
+
// A structured clone drops the tags (a palette's colour space with them), so palettes and clusterings cross to and from the
|
|
299
|
+
// worker thread as small markers and become typed arrays again on the other side: `mark` before posting (an argument, a
|
|
300
|
+
// result, or a property of a result), `revive` after receiving.
|
|
301
|
+
var MARK = exports.MARK = '__agglomerativeClustering';
|
|
302
|
+
var markOne = function markOne(value) {
|
|
303
|
+
if (isPalette(value)) {
|
|
304
|
+
return _defineProperty(_defineProperty(_defineProperty({}, MARK, 'palette'), "bytes", new Uint8Array(value.buffer, value.byteOffset, value.length)), "colorSpace", value.colorSpace);
|
|
305
|
+
}
|
|
306
|
+
if (isClustering(value)) {
|
|
307
|
+
return _defineProperty(_defineProperty({}, MARK, 'clustering'), "bytes", new Uint8Array(value.buffer, value.byteOffset, value.length));
|
|
308
|
+
}
|
|
309
|
+
return value;
|
|
310
|
+
};
|
|
311
|
+
var mark = exports.mark = function mark(value) {
|
|
312
|
+
if (!value || _typeof(value) !== 'object' || ArrayBuffer.isView(value)) {
|
|
313
|
+
return markOne(value);
|
|
314
|
+
}
|
|
315
|
+
if (!isOptionsObject(value)) {
|
|
316
|
+
return value;
|
|
317
|
+
}
|
|
318
|
+
return Object.fromEntries(Object.entries(value).map(function (_ref3) {
|
|
319
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
320
|
+
key = _ref4[0],
|
|
321
|
+
part = _ref4[1];
|
|
322
|
+
return [key, markOne(part)];
|
|
323
|
+
}));
|
|
324
|
+
};
|
|
325
|
+
var reviveOne = function reviveOne(value) {
|
|
326
|
+
if (!value || _typeof(value) !== 'object' || !value[MARK]) {
|
|
327
|
+
return value;
|
|
328
|
+
}
|
|
329
|
+
if (value[MARK] === 'palette') {
|
|
330
|
+
return markPalette(new Uint8Array(value.bytes.buffer, value.bytes.byteOffset, value.bytes.length), value.colorSpace);
|
|
331
|
+
}
|
|
332
|
+
return markClustering(new Uint8Array(value.bytes.buffer, value.bytes.byteOffset, value.bytes.length));
|
|
333
|
+
};
|
|
334
|
+
var revive = exports.revive = function revive(value) {
|
|
335
|
+
if (!value || _typeof(value) !== 'object' || ArrayBuffer.isView(value)) {
|
|
336
|
+
return value;
|
|
337
|
+
}
|
|
338
|
+
if (value[MARK]) {
|
|
339
|
+
return reviveOne(value);
|
|
340
|
+
}
|
|
341
|
+
if (!isOptionsObject(value)) {
|
|
342
|
+
return value;
|
|
343
|
+
}
|
|
344
|
+
for (var _i = 0, _Object$keys = Object.keys(value); _i < _Object$keys.length; _i++) {
|
|
345
|
+
var key = _Object$keys[_i];
|
|
346
|
+
value[key] = reviveOne(value[key]);
|
|
347
|
+
}
|
|
348
|
+
return value;
|
|
349
|
+
};
|