@hema-to/regl-scatterplot 1.14.1-hemato.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/LICENSE +21 -0
- package/README.md +1110 -0
- package/dist/regl-scatterplot.esm.d.ts +126 -0
- package/dist/regl-scatterplot.esm.js +9962 -0
- package/dist/regl-scatterplot.js +9385 -0
- package/dist/regl-scatterplot.min.js +8 -0
- package/dist/types.d.ts +306 -0
- package/package.json +100 -0
- package/src/bg.fs +13 -0
- package/src/bg.vs +16 -0
- package/src/constants.js +189 -0
- package/src/index.js +4654 -0
- package/src/kdbush-class.js +369 -0
- package/src/kdbush-worker.js +20 -0
- package/src/kdbush.js +69 -0
- package/src/lasso-manager/constants.js +7 -0
- package/src/lasso-manager/create-long-press-animations.js +257 -0
- package/src/lasso-manager/create-long-press-elements.js +68 -0
- package/src/lasso-manager/index.js +781 -0
- package/src/lasso-manager/utils.js +40 -0
- package/src/point-simple.fs +10 -0
- package/src/point-update.fs +21 -0
- package/src/point-update.vs +13 -0
- package/src/point.fs +22 -0
- package/src/point.vs +124 -0
- package/src/renderer.js +252 -0
- package/src/spline-curve-worker.js +271 -0
- package/src/spline-curve.js +23 -0
- package/src/types.d.ts +306 -0
- package/src/utils.js +598 -0
package/src/utils.js
ADDED
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
import createOriginalRegl from 'regl';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_IMAGE_LOAD_TIMEOUT,
|
|
5
|
+
GL_EXTENSIONS,
|
|
6
|
+
IMAGE_LOAD_ERROR,
|
|
7
|
+
W_NAMES,
|
|
8
|
+
Z_NAMES,
|
|
9
|
+
} from './constants.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Get the max value of an array. helper method to be used with `Array.reduce()`.
|
|
13
|
+
* @param {number} max Accumulator holding the max value.
|
|
14
|
+
* @param {number} x Current value.
|
|
15
|
+
* @return {number} Max value.
|
|
16
|
+
*/
|
|
17
|
+
export const arrayMax = (max, x) => (max > x ? max : x);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Check if all GL extensions are supported and enabled and warn otherwise
|
|
21
|
+
* @param {import('regl').Regl} regl Regl instance to be tested
|
|
22
|
+
* @param {boolean} silent If `true` the function will not print `console.warn` statements
|
|
23
|
+
* @return {boolean} If `true` all required GL extensions are supported
|
|
24
|
+
*/
|
|
25
|
+
export const checkReglExtensions = (regl, silent) => {
|
|
26
|
+
if (!regl) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return GL_EXTENSIONS.reduce((every, extension) => {
|
|
30
|
+
if (!regl.hasExtension(extension)) {
|
|
31
|
+
if (!silent) {
|
|
32
|
+
// biome-ignore lint/suspicious/noConsole: This is a legitimately useful warning
|
|
33
|
+
console.warn(
|
|
34
|
+
`WebGL: ${extension} extension not supported. Scatterplot might not render properly`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return every;
|
|
40
|
+
}, true);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a new Regl instance with `GL_EXTENSIONS` enables
|
|
45
|
+
* @param {HTMLCanvasElement} canvas Canvas element to be rendered on
|
|
46
|
+
* @return {import('regl').Regl} New Regl instance
|
|
47
|
+
*/
|
|
48
|
+
export const createRegl = (canvas) => {
|
|
49
|
+
const gl = canvas.getContext('webgl', {
|
|
50
|
+
antialias: true,
|
|
51
|
+
preserveDrawingBuffer: true,
|
|
52
|
+
});
|
|
53
|
+
const extensions = [];
|
|
54
|
+
|
|
55
|
+
// Needed to run the tests properly as the headless-gl doesn't support all
|
|
56
|
+
// extensions, which is fine for the functional tests.
|
|
57
|
+
for (const extension of GL_EXTENSIONS) {
|
|
58
|
+
if (gl.getExtension(extension)) {
|
|
59
|
+
extensions.push(extension);
|
|
60
|
+
} else {
|
|
61
|
+
// biome-ignore lint/suspicious/noConsole: This is a legitimately useful warning
|
|
62
|
+
console.warn(
|
|
63
|
+
`WebGL: ${extension} extension not supported. Scatterplot might not render properly`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return createOriginalRegl({ gl, extensions });
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* L2 distance between a pair of 2D points
|
|
73
|
+
* @param {number} x1 X coordinate of the first point
|
|
74
|
+
* @param {number} y1 Y coordinate of the first point
|
|
75
|
+
* @param {number} x2 X coordinate of the second point
|
|
76
|
+
* @param {number} y2 Y coordinate of the first point
|
|
77
|
+
* @return {number} L2 distance
|
|
78
|
+
*/
|
|
79
|
+
export const dist = (x1, y1, x2, y2) =>
|
|
80
|
+
Math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2);
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Get the bounding box of a set of 2D positions
|
|
84
|
+
* @param {array} positions2d 2D positions to be checked
|
|
85
|
+
* @return {array} Quadruple of form `[xMin, yMin, xMax, yMax]` defining the
|
|
86
|
+
* bounding box
|
|
87
|
+
*/
|
|
88
|
+
// biome-ignore lint/style/useNamingConvention: BBox stands for BoundingBox
|
|
89
|
+
export const getBBox = (positions2d) => {
|
|
90
|
+
let xMin = Number.POSITIVE_INFINITY;
|
|
91
|
+
let xMax = Number.NEGATIVE_INFINITY;
|
|
92
|
+
let yMin = Number.POSITIVE_INFINITY;
|
|
93
|
+
let yMax = Number.NEGATIVE_INFINITY;
|
|
94
|
+
|
|
95
|
+
for (let i = 0; i < positions2d.length; i += 2) {
|
|
96
|
+
xMin = positions2d[i] < xMin ? positions2d[i] : xMin;
|
|
97
|
+
xMax = positions2d[i] > xMax ? positions2d[i] : xMax;
|
|
98
|
+
yMin = positions2d[i + 1] < yMin ? positions2d[i + 1] : yMin;
|
|
99
|
+
yMax = positions2d[i + 1] > yMax ? positions2d[i + 1] : yMax;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return [xMin, yMin, xMax, yMax];
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Test whether a bounding box is actually specifying an area
|
|
107
|
+
* @param {array} bBox The bounding box to be checked
|
|
108
|
+
* @return {array} `true` if the bounding box is valid
|
|
109
|
+
*/
|
|
110
|
+
// biome-ignore lint/style/useNamingConvention: BBox stands for BoundingBox
|
|
111
|
+
export const isValidBBox = ([xMin, yMin, xMax, yMax]) =>
|
|
112
|
+
Number.isFinite(xMin) &&
|
|
113
|
+
Number.isFinite(yMin) &&
|
|
114
|
+
Number.isFinite(xMax) &&
|
|
115
|
+
Number.isFinite(yMax) &&
|
|
116
|
+
xMax - xMin > 0 &&
|
|
117
|
+
yMax - yMin > 0;
|
|
118
|
+
|
|
119
|
+
const REGEX_HEX_TO_RGB = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Convert a HEX-encoded color to an RGB-encoded color
|
|
123
|
+
* @param {string} hex HEX-encoded color string.
|
|
124
|
+
* @param {boolean} isNormalize If `true` the returned RGB values will be
|
|
125
|
+
* normalized to `[0,1]`.
|
|
126
|
+
* @return {array} Triple holding the RGB values.
|
|
127
|
+
*/
|
|
128
|
+
export const hexToRgb = (hex, isNormalize = false) =>
|
|
129
|
+
hex
|
|
130
|
+
.replace(REGEX_HEX_TO_RGB, (_m, r, g, b) => `#${r}${r}${g}${g}${b}${b}`)
|
|
131
|
+
.substring(1)
|
|
132
|
+
.match(/.{2}/g)
|
|
133
|
+
.map((x) => Number.parseInt(x, 16) / 255 ** isNormalize);
|
|
134
|
+
|
|
135
|
+
export const isConditionalArray = (a, condition, { minLength = 0 } = {}) =>
|
|
136
|
+
Array.isArray(a) && a.length >= minLength && a.every(condition);
|
|
137
|
+
|
|
138
|
+
export const isPositiveNumber = (x) => !Number.isNaN(+x) && +x >= 0;
|
|
139
|
+
|
|
140
|
+
export const isStrictlyPositiveNumber = (x) => !Number.isNaN(+x) && +x > 0;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Create a function to limit choices to a predefined list
|
|
144
|
+
* @param {array} choices Array of acceptable choices
|
|
145
|
+
* @param {*} defaultOption Default choice
|
|
146
|
+
* @return {function} Function limiting the choices
|
|
147
|
+
*/
|
|
148
|
+
export const limit = (choices, defaultChoice) => (choice) =>
|
|
149
|
+
choices.indexOf(choice) >= 0 ? choice : defaultChoice;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Promised-based image loading
|
|
153
|
+
* @param {string} src Remote image source, i.e., a URL
|
|
154
|
+
* @param {boolean} isCrossOrigin If `true` allow loading image from a source of another origin.
|
|
155
|
+
* @return {Promise<HTMLImageElement>} Promise resolving to the image once its loaded
|
|
156
|
+
*/
|
|
157
|
+
export const loadImage = (
|
|
158
|
+
src,
|
|
159
|
+
isCrossOrigin = false,
|
|
160
|
+
timeout = DEFAULT_IMAGE_LOAD_TIMEOUT,
|
|
161
|
+
) =>
|
|
162
|
+
new Promise((resolve, reject) => {
|
|
163
|
+
const image = new Image();
|
|
164
|
+
if (isCrossOrigin) {
|
|
165
|
+
image.crossOrigin = 'anonymous';
|
|
166
|
+
}
|
|
167
|
+
image.src = src;
|
|
168
|
+
image.onload = () => {
|
|
169
|
+
resolve(image);
|
|
170
|
+
};
|
|
171
|
+
const rejectPromise = () => {
|
|
172
|
+
reject(new Error(IMAGE_LOAD_ERROR));
|
|
173
|
+
};
|
|
174
|
+
image.onerror = rejectPromise;
|
|
175
|
+
setTimeout(rejectPromise, timeout);
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @deprecated Please use `scatterplot.createTextureFromUrl(url)`
|
|
180
|
+
*
|
|
181
|
+
* Create a Regl texture from an URL.
|
|
182
|
+
* @param {import('regl').Regl} regl Regl instance used for creating the texture.
|
|
183
|
+
* @param {string} url Source URL of the image.
|
|
184
|
+
* @return {Promise<import('regl').Texture2D>} Promise resolving to the texture object.
|
|
185
|
+
*/
|
|
186
|
+
export const createTextureFromUrl = (
|
|
187
|
+
regl,
|
|
188
|
+
url,
|
|
189
|
+
timeout = DEFAULT_IMAGE_LOAD_TIMEOUT,
|
|
190
|
+
) =>
|
|
191
|
+
new Promise((resolve, reject) => {
|
|
192
|
+
loadImage(
|
|
193
|
+
url,
|
|
194
|
+
url.indexOf(window.location.origin) !== 0 && url.indexOf('base64') === -1,
|
|
195
|
+
timeout,
|
|
196
|
+
)
|
|
197
|
+
.then((image) => {
|
|
198
|
+
resolve(regl.texture(image));
|
|
199
|
+
})
|
|
200
|
+
.catch((error) => {
|
|
201
|
+
reject(error);
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Convert a HEX-encoded color to an RGBA-encoded color
|
|
207
|
+
* @param {string} hex HEX-encoded color string.
|
|
208
|
+
* @param {boolean} isNormalize If `true` the returned RGBA values will be
|
|
209
|
+
* normalized to `[0,1]`.
|
|
210
|
+
* @return {array} Triple holding the RGBA values.
|
|
211
|
+
*/
|
|
212
|
+
export const hexToRgba = (hex, isNormalize = false) => [
|
|
213
|
+
...hexToRgb(hex, isNormalize),
|
|
214
|
+
255 ** !isNormalize,
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
const REGEX_IS_HEX = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i;
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Tests if a string is a valid HEX color encoding
|
|
221
|
+
* @param {string} hex HEX-encoded color string.
|
|
222
|
+
* @return {boolean} If `true` the string is a valid HEX color encoding.
|
|
223
|
+
*/
|
|
224
|
+
export const isHex = (hex) => REGEX_IS_HEX.test(hex);
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Tests if a number is in `[0,1]`.
|
|
228
|
+
* @param {number} x Number to be tested.
|
|
229
|
+
* @return {boolean} If `true` the number is in `[0,1]`.
|
|
230
|
+
*/
|
|
231
|
+
export const isNormFloat = (x) => x >= 0 && x <= 1;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Tests if an array consist of normalized numbers that are in `[0,1]` only.
|
|
235
|
+
* @param {array} a Array to be tested
|
|
236
|
+
* @return {boolean} If `true` the array contains only numbers in `[0,1]`.
|
|
237
|
+
*/
|
|
238
|
+
export const isNormFloatArray = (a) => Array.isArray(a) && a.every(isNormFloat);
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Computes the cross product to determine the orientation of three points
|
|
242
|
+
* @param {number} x1 X-coordinate of first point
|
|
243
|
+
* @param {number} y1 Y-coordinate of first point
|
|
244
|
+
* @param {number} x2 X-coordinate of second point
|
|
245
|
+
* @param {number} y2 Y-coordinate of second point
|
|
246
|
+
* @param {number} px X-coordinate of test point
|
|
247
|
+
* @param {number} py Y-coordinate of test point
|
|
248
|
+
* @return {number} Positive if counterclockwise, negative if clockwise
|
|
249
|
+
*/
|
|
250
|
+
function crossProduct(x1, y1, x2, y2, px, py) {
|
|
251
|
+
return (x2 - x1) * (py - y1) - (px - x1) * (y2 - y1);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Determines if a point lies within a polygon using the non-zero winding rule.
|
|
256
|
+
* This handles self-intersecting polygons and overlapping areas correctly.
|
|
257
|
+
* @param {Array} polygon 1D list of vertices defining the polygon [x1,y1,x2,y2,...]
|
|
258
|
+
* @param {Array} point Tuple of the form [x,y] to be tested
|
|
259
|
+
* @return {boolean} True if point lies within the polygon
|
|
260
|
+
*/
|
|
261
|
+
export const isPointInPolygon = (polygon, [px, py] = []) => {
|
|
262
|
+
let winding = 0;
|
|
263
|
+
|
|
264
|
+
for (let i = 0, j = polygon.length - 2; i < polygon.length; i += 2) {
|
|
265
|
+
const x1 = polygon[i];
|
|
266
|
+
const y1 = polygon[i + 1];
|
|
267
|
+
const x2 = polygon[j];
|
|
268
|
+
const y2 = polygon[j + 1];
|
|
269
|
+
|
|
270
|
+
if (y1 <= py) {
|
|
271
|
+
if (y2 > py) {
|
|
272
|
+
const orientation = crossProduct(x1, y1, x2, y2, px, py);
|
|
273
|
+
if (orientation > 0) {
|
|
274
|
+
winding++;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
} else if (y2 <= py) {
|
|
278
|
+
const orientation = crossProduct(x1, y1, x2, y2, px, py);
|
|
279
|
+
if (orientation < 0) {
|
|
280
|
+
winding--;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
j = i;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return winding !== 0;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Tests if a variable is a string
|
|
292
|
+
* @param {*} s Variable to be tested
|
|
293
|
+
* @return {boolean} If `true` variable is a string
|
|
294
|
+
*/
|
|
295
|
+
export const isString = (s) => typeof s === 'string' || s instanceof String;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Tests if a number is an interger and in `[0,255]`.
|
|
299
|
+
* @param {number} x Number to be tested.
|
|
300
|
+
* @return {boolean} If `true` the number is an interger and in `[0,255]`.
|
|
301
|
+
*/
|
|
302
|
+
export const isUint8 = (x) => Number.isInteger(x) && x >= 0 && x <= 255;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Tests if an array consist of Uint8 numbers only.
|
|
306
|
+
* @param {array} a Array to be tested.
|
|
307
|
+
* @return {boolean} If `true` the array contains only Uint8 numbers.
|
|
308
|
+
*/
|
|
309
|
+
export const isUint8Array = (a) => Array.isArray(a) && a.every(isUint8);
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Tests if an array is encoding an RGB color.
|
|
313
|
+
* @param {array} rgb Array to be tested
|
|
314
|
+
* @return {boolean} If `true` the array hold a triple of Uint8 numbers or
|
|
315
|
+
* a triple of normalized floats.
|
|
316
|
+
*/
|
|
317
|
+
export const isRgb = (rgb) =>
|
|
318
|
+
rgb.length === 3 && (isNormFloatArray(rgb) || isUint8Array(rgb));
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Tests if an array is encoding an RGBA color.
|
|
322
|
+
* @param {array} rgb Array to be tested
|
|
323
|
+
* @return {boolean} If `true` the array hold a quadruple of Uint8 numbers or
|
|
324
|
+
* a quadruple of normalized floats.
|
|
325
|
+
*/
|
|
326
|
+
export const isRgba = (rgba) =>
|
|
327
|
+
rgba.length === 4 && (isNormFloatArray(rgba) || isUint8Array(rgba));
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Test if a color is multiple colors
|
|
331
|
+
* @param {*} color To be tested
|
|
332
|
+
* @return {boolean} If `true`, `color` is an array of colors.
|
|
333
|
+
*/
|
|
334
|
+
export const isMultipleColors = (color) =>
|
|
335
|
+
Array.isArray(color) &&
|
|
336
|
+
color.length > 0 &&
|
|
337
|
+
(Array.isArray(color[0]) || isString(color[0]));
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Test if two arrays contain the same primitive values
|
|
341
|
+
*/
|
|
342
|
+
export const isSameElements = (a, b) =>
|
|
343
|
+
Array.isArray(a) && Array.isArray(b) && a.every((value, i) => value === b[i]);
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Test if two arrays contain the same RGBA quadruples
|
|
347
|
+
*/
|
|
348
|
+
export const isSameRgbas = (a, b) => {
|
|
349
|
+
if (!(Array.isArray(a) && Array.isArray(b)) || a.length !== b.length) {
|
|
350
|
+
return false;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (a.length === 0) {
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// We need to test whether a and b are arrays of RGBA quadruples
|
|
358
|
+
if (!(Array.isArray(a[0]) && Array.isArray(b[0]))) {
|
|
359
|
+
return false;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return a.every(([r1, g1, b1, a1], i) => {
|
|
363
|
+
const [r2, g2, b2, a2] = b[i];
|
|
364
|
+
return r1 === r2 && g1 === g2 && b1 === b2 && a1 === a2;
|
|
365
|
+
});
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Fast version of `Math.max`. Based on
|
|
370
|
+
* https://jsperf.com/math-min-max-vs-ternary-vs-if/24 `Math.max` is not
|
|
371
|
+
* very fast
|
|
372
|
+
* @param {number} a Value A
|
|
373
|
+
* @param {number} b Value B
|
|
374
|
+
* @return {boolean} If `true` A is greater than B.
|
|
375
|
+
*/
|
|
376
|
+
export const max = (a, b) => (a > b ? a : b);
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Fast version of `Math.min`. Based on
|
|
380
|
+
* https://jsperf.com/math-min-max-vs-ternary-vs-if/24 `Math.max` is not
|
|
381
|
+
* very fast
|
|
382
|
+
* @param {number} a Value A
|
|
383
|
+
* @param {number} b Value B
|
|
384
|
+
* @return {boolean} If `true` A is smaller than B.
|
|
385
|
+
*/
|
|
386
|
+
export const min = (a, b) => (a < b ? a : b);
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Normalize an array
|
|
390
|
+
* @param {array} a Array to be normalized.
|
|
391
|
+
* @return {array} Normalized array.
|
|
392
|
+
*/
|
|
393
|
+
export const normNumArray = (a) =>
|
|
394
|
+
a.map((x) => x / a.reduce(arrayMax, Number.NEGATIVE_INFINITY));
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Convert a color to an RGBA color
|
|
398
|
+
* @param {*} color Color to be converted. Currently supports:
|
|
399
|
+
* HEX, RGB, or RGBA.
|
|
400
|
+
* @param {boolean} isNormalize If `true` the returned RGBA values will be
|
|
401
|
+
* normalized to `[0,1]`.
|
|
402
|
+
* @return {array} Quadruple defining an RGBA color.
|
|
403
|
+
*/
|
|
404
|
+
export const toRgba = (color, shouldNormalize) => {
|
|
405
|
+
if (isRgba(color)) {
|
|
406
|
+
const isNormalized = isNormFloatArray(color);
|
|
407
|
+
if (
|
|
408
|
+
(shouldNormalize && isNormalized) ||
|
|
409
|
+
!(shouldNormalize || isNormalized)
|
|
410
|
+
) {
|
|
411
|
+
return color;
|
|
412
|
+
}
|
|
413
|
+
if (shouldNormalize && !isNormalized) {
|
|
414
|
+
return color.map((x) => x / 255);
|
|
415
|
+
}
|
|
416
|
+
return color.map((x) => x * 255);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (isRgb(color)) {
|
|
420
|
+
const base = 255 ** !shouldNormalize;
|
|
421
|
+
const isNormalized = isNormFloatArray(color);
|
|
422
|
+
|
|
423
|
+
if (
|
|
424
|
+
(shouldNormalize && isNormalized) ||
|
|
425
|
+
!(shouldNormalize || isNormalized)
|
|
426
|
+
) {
|
|
427
|
+
return [...color, base];
|
|
428
|
+
}
|
|
429
|
+
if (shouldNormalize && !isNormalized) {
|
|
430
|
+
return [...color.map((x) => x / 255), base];
|
|
431
|
+
}
|
|
432
|
+
return [...color.map((x) => x * 255), base];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (isHex(color)) {
|
|
436
|
+
return hexToRgba(color, shouldNormalize);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// biome-ignore lint/suspicious/noConsole: This is a legitimately useful warning
|
|
440
|
+
console.warn(
|
|
441
|
+
'Only HEX, RGB, and RGBA are handled by this function. Returning white instead.',
|
|
442
|
+
);
|
|
443
|
+
return shouldNormalize ? [1, 1, 1, 1] : [255, 255, 255, 255];
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Flip the key-value pairs of an object
|
|
448
|
+
* @param {object} obj - Object to be flipped
|
|
449
|
+
* @return {object} Flipped object
|
|
450
|
+
*/
|
|
451
|
+
export const flipObj = (obj) =>
|
|
452
|
+
Object.entries(obj).reduce((out, [key, value]) => {
|
|
453
|
+
if (out[value]) {
|
|
454
|
+
out[value] = [...out[value], key];
|
|
455
|
+
} else {
|
|
456
|
+
out[value] = key;
|
|
457
|
+
}
|
|
458
|
+
return out;
|
|
459
|
+
}, {});
|
|
460
|
+
|
|
461
|
+
export const rgbBrightness = (rgb) =>
|
|
462
|
+
0.21 * rgb[0] + 0.72 * rgb[1] + 0.07 * rgb[2];
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Clip a number between min and max
|
|
466
|
+
* @param {number} value - The value to be clipped
|
|
467
|
+
* @param {number} minValue - The minimum value
|
|
468
|
+
* @param {number} maxValue - The maximum value
|
|
469
|
+
* @return {number} The clipped value
|
|
470
|
+
*/
|
|
471
|
+
export const clip = (value, minValue, maxValue) =>
|
|
472
|
+
Math.min(maxValue, Math.max(minValue, value));
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Convert object- or array-oriented points to array-oriented points
|
|
476
|
+
* @param {import('./types').Points} points - The point data
|
|
477
|
+
* @return {number[][]} Array-oriented points
|
|
478
|
+
*/
|
|
479
|
+
export const toArrayOrientedPoints = (points) =>
|
|
480
|
+
new Promise((resolve, reject) => {
|
|
481
|
+
if (!points || Array.isArray(points)) {
|
|
482
|
+
resolve(points);
|
|
483
|
+
} else {
|
|
484
|
+
const length =
|
|
485
|
+
Array.isArray(points.x) || ArrayBuffer.isView(points.x)
|
|
486
|
+
? points.x.length
|
|
487
|
+
: 0;
|
|
488
|
+
|
|
489
|
+
const getX =
|
|
490
|
+
(Array.isArray(points.x) || ArrayBuffer.isView(points.x)) &&
|
|
491
|
+
((i) => points.x[i]);
|
|
492
|
+
const getY =
|
|
493
|
+
(Array.isArray(points.y) || ArrayBuffer.isView(points.y)) &&
|
|
494
|
+
((i) => points.y[i]);
|
|
495
|
+
const getL =
|
|
496
|
+
(Array.isArray(points.line) || ArrayBuffer.isView(points.line)) &&
|
|
497
|
+
((i) => points.line[i]);
|
|
498
|
+
|
|
499
|
+
// biome-ignore lint/style/useNamingConvention: LO stands for line and order
|
|
500
|
+
const getLO =
|
|
501
|
+
(Array.isArray(points.lineOrder) ||
|
|
502
|
+
ArrayBuffer.isView(points.lineOrder)) &&
|
|
503
|
+
((i) => points.lineOrder[i]);
|
|
504
|
+
|
|
505
|
+
const components = Object.keys(points);
|
|
506
|
+
const getZ = (() => {
|
|
507
|
+
const z = components.find((c) => Z_NAMES.has(c));
|
|
508
|
+
return (
|
|
509
|
+
z &&
|
|
510
|
+
(Array.isArray(points[z]) || ArrayBuffer.isView(points[z])) &&
|
|
511
|
+
((i) => points[z][i])
|
|
512
|
+
);
|
|
513
|
+
})();
|
|
514
|
+
const getW = (() => {
|
|
515
|
+
const w = components.find((c) => W_NAMES.has(c));
|
|
516
|
+
return (
|
|
517
|
+
w &&
|
|
518
|
+
(Array.isArray(points[w]) || ArrayBuffer.isView(points[w])) &&
|
|
519
|
+
((i) => points[w][i])
|
|
520
|
+
);
|
|
521
|
+
})();
|
|
522
|
+
|
|
523
|
+
if (getX && getY && getZ && getW && getL && getLO) {
|
|
524
|
+
resolve(
|
|
525
|
+
points.x.map((x, i) => [
|
|
526
|
+
x,
|
|
527
|
+
getY(i),
|
|
528
|
+
getZ(i),
|
|
529
|
+
getW(i),
|
|
530
|
+
getL(i),
|
|
531
|
+
getLO(i),
|
|
532
|
+
]),
|
|
533
|
+
);
|
|
534
|
+
} else if (getX && getY && getZ && getW && getL) {
|
|
535
|
+
resolve(
|
|
536
|
+
Array.from({ length }, (_, i) => [
|
|
537
|
+
getX(i),
|
|
538
|
+
getY(i),
|
|
539
|
+
getZ(i),
|
|
540
|
+
getW(i),
|
|
541
|
+
getL(i),
|
|
542
|
+
]),
|
|
543
|
+
);
|
|
544
|
+
} else if (getX && getY && getZ && getW) {
|
|
545
|
+
resolve(
|
|
546
|
+
Array.from({ length }, (_, i) => [
|
|
547
|
+
getX(i),
|
|
548
|
+
getY(i),
|
|
549
|
+
getZ(i),
|
|
550
|
+
getW(i),
|
|
551
|
+
]),
|
|
552
|
+
);
|
|
553
|
+
} else if (getX && getY && getZ) {
|
|
554
|
+
resolve(Array.from({ length }, (_, i) => [getX(i), getY(i), getZ(i)]));
|
|
555
|
+
} else if (getX && getY) {
|
|
556
|
+
resolve(Array.from({ length }, (_, i) => [getX(i), getY(i)]));
|
|
557
|
+
} else {
|
|
558
|
+
reject(new Error('You need to specify at least x and y'));
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
export const isHorizontalLine = (annotation) =>
|
|
564
|
+
Number.isFinite(annotation.y) && !('x' in annotation);
|
|
565
|
+
|
|
566
|
+
export const isVerticalLine = (annotation) =>
|
|
567
|
+
Number.isFinite(annotation.x) && !('y' in annotation);
|
|
568
|
+
|
|
569
|
+
export const isDomRect = (annotation) =>
|
|
570
|
+
Number.isFinite(annotation.x) &&
|
|
571
|
+
Number.isFinite(annotation.y) &&
|
|
572
|
+
Number.isFinite(annotation.width) &&
|
|
573
|
+
Number.isFinite(annotation.height);
|
|
574
|
+
|
|
575
|
+
export const isRect = (annotation) =>
|
|
576
|
+
Number.isFinite(annotation.x1) &&
|
|
577
|
+
Number.isFinite(annotation.y1) &&
|
|
578
|
+
Number.isFinite(annotation.x2) &&
|
|
579
|
+
Number.isFinite(annotation.x2);
|
|
580
|
+
|
|
581
|
+
export const isPolygon = (annotation) =>
|
|
582
|
+
'vertices' in annotation && annotation.vertices.length > 1;
|
|
583
|
+
|
|
584
|
+
export const insertionSort = (array) => {
|
|
585
|
+
const end = array.length;
|
|
586
|
+
for (let i = 1; i < end; i++) {
|
|
587
|
+
// Choosing the first element in our unsorted subarray
|
|
588
|
+
const current = array[i];
|
|
589
|
+
// The last element of our sorted subarray
|
|
590
|
+
let j = i - 1;
|
|
591
|
+
while (j > -1 && current < array[j]) {
|
|
592
|
+
array[j + 1] = array[j];
|
|
593
|
+
j--;
|
|
594
|
+
}
|
|
595
|
+
array[j + 1] = current;
|
|
596
|
+
}
|
|
597
|
+
return array;
|
|
598
|
+
};
|