toosoon-utils 4.2.3 → 4.3.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.
Files changed (42) hide show
  1. package/README.md +499 -575
  2. package/lib/colors.d.ts +147 -66
  3. package/lib/colors.js +149 -63
  4. package/lib/constants.js +1 -1
  5. package/lib/dom.d.ts +1 -1
  6. package/lib/dom.js +1 -1
  7. package/lib/extras/colors/Color.d.ts +406 -0
  8. package/lib/extras/colors/Color.js +546 -0
  9. package/lib/extras/colors/ColorPalette.d.ts +105 -0
  10. package/lib/extras/colors/ColorPalette.js +124 -0
  11. package/lib/extras/colors/ColorScale.d.ts +257 -0
  12. package/lib/extras/colors/ColorScale.js +347 -0
  13. package/lib/extras/colors/_ColorScale.d.ts +62 -0
  14. package/lib/extras/colors/_ColorScale.js +156 -0
  15. package/lib/extras/colors/index.d.ts +3 -0
  16. package/lib/extras/colors/index.js +3 -0
  17. package/lib/extras/frame-rate/FrameRate.d.ts +1 -1
  18. package/lib/extras/frame-rate/FrameRate.js +2 -2
  19. package/lib/extras/geometry/Vector.d.ts +1 -1
  20. package/lib/extras/geometry/Vector2.d.ts +17 -11
  21. package/lib/extras/geometry/Vector2.js +29 -23
  22. package/lib/extras/geometry/Vector3.d.ts +5 -5
  23. package/lib/extras/geometry/Vector3.js +10 -10
  24. package/lib/extras/paths/Path.d.ts +3 -3
  25. package/lib/extras/paths/Path.js +10 -10
  26. package/lib/extras/paths/PathContext.d.ts +5 -10
  27. package/lib/extras/paths/PathContext.js +70 -99
  28. package/lib/extras/paths/PathSVG.d.ts +31 -25
  29. package/lib/extras/paths/PathSVG.js +36 -39
  30. package/lib/extras/paths/index.d.ts +1 -1
  31. package/lib/geometry.js +1 -1
  32. package/lib/maths.d.ts +19 -13
  33. package/lib/maths.js +23 -17
  34. package/lib/prng.d.ts +4 -4
  35. package/lib/prng.js +4 -4
  36. package/lib/random.d.ts +4 -4
  37. package/lib/random.js +4 -4
  38. package/lib/strings.d.ts +14 -8
  39. package/lib/strings.js +14 -8
  40. package/lib/tsconfig.tsbuildinfo +1 -1
  41. package/lib/types.d.ts +15 -8
  42. package/package.json +14 -14
@@ -0,0 +1,406 @@
1
+ import type { ColorName, ColorHex, ColorRgb, ColorHsl, ColorHsb, ColorHcl, ColorLab, ColorRepresentation } from '../../types';
2
+ /**
3
+ * Parameters used for color interpolation
4
+ */
5
+ export type ColorInterpolationsParameters = {
6
+ rgb: Parameters<typeof Color.lerpRgb>[3];
7
+ hsl: Parameters<typeof Color.lerpHsl>[3];
8
+ hsb: Parameters<typeof Color.lerpHsb>[3];
9
+ qualitative: Parameters<typeof Color.interpolateQualitative>[3];
10
+ sequential: Parameters<typeof Color.interpolateSequential>[3];
11
+ diverging: Parameters<typeof Color.interpolateDiverging>[3];
12
+ };
13
+ /**
14
+ * Type of interpolation
15
+ */
16
+ export type ColorInterpolation = keyof ColorInterpolationsParameters;
17
+ /**
18
+ * Type of hue interpolation
19
+ */
20
+ export type HueInterpolationMode = 'direct' | 'shortest' | 'longest';
21
+ /**
22
+ * Utility class for manipulating colors
23
+ *
24
+ * @exports
25
+ * @class Color
26
+ */
27
+ export default class Color {
28
+ readonly isColor = true;
29
+ readonly type: string;
30
+ /**
31
+ * Red value of this color in the RGB color space
32
+ */
33
+ r: number;
34
+ /**
35
+ * Green value of this color in the RGB color space
36
+ */
37
+ g: number;
38
+ /**
39
+ * Blue value of this color in the RGB color space
40
+ */
41
+ b: number;
42
+ [Symbol.iterator](): Iterator<number>;
43
+ /**
44
+ * @param {ColorRepresentation} [color=0x000000] Color representation of this color
45
+ */
46
+ constructor(color?: ColorRepresentation);
47
+ /**
48
+ * Set this color RGB values
49
+ *
50
+ * @param {Color|ColorRepresentation} color Color to set
51
+ * @returns {this}
52
+ */
53
+ set(color: Color | ColorRepresentation): this;
54
+ /**
55
+ * Set this color values from a given color name
56
+ *
57
+ * @param {ColorName} colorName Color name of the color to set
58
+ * @returns {this}
59
+ */
60
+ setColorName(colorName: ColorName): this;
61
+ /**
62
+ * Set this color values from a given RGB color
63
+ *
64
+ * Note:
65
+ * - RGB values are contained in the interval [0, 1]
66
+ *
67
+ * @param {ColorRgb} rgb RGB color
68
+ * @returns {this}
69
+ */
70
+ setRgb([r, g, b]: ColorRgb): this;
71
+ /**
72
+ * Set this color values from a given hexadecimal color
73
+ *
74
+ * @param {ColorHex} hex Hexadecimal color
75
+ * @returns {this}
76
+ */
77
+ setHex(hex: ColorHex): this;
78
+ /**
79
+ * Set this color values from a given HSL color
80
+ *
81
+ * Note:
82
+ * - HSL values are contained in the intervals:
83
+ * - Hue: [0, 360]
84
+ * - Saturation: [0, 1]
85
+ * - Lightness: [0, 1]
86
+ *
87
+ * @param {ColorHsl|string} hsl HSL color
88
+ * @returns {this}
89
+ */
90
+ setHsl(hsl: ColorHsl | string): this;
91
+ /**
92
+ * Set this color values from a given HSB color
93
+ *
94
+ * Note:
95
+ * - HSB values are contained in the intervals:
96
+ * - Hue: [0, 360]
97
+ * - Saturation: [0, 1]
98
+ * - Brightness: [0, 1]
99
+ *
100
+ * @param {ColorHsb} hsb HSB color
101
+ * @returns {this}
102
+ */
103
+ setHsb(hsb: ColorHsb): this;
104
+ /**
105
+ * Set this color values from a given L*a*b* color
106
+ *
107
+ * Note:
108
+ * - L*a*b* values are contained in the intervals:
109
+ * - Lightness: [0 à 100]
110
+ * - a (green, red): [~-128, ~+128]
111
+ * - b (blue, yellow): [~-128, ~+128]
112
+ *
113
+ * @param {ColorLab} lab L*a*b* color
114
+ * @returns {this}
115
+ */
116
+ setLab(lab: ColorLab): this;
117
+ /**
118
+ * Set this color values from a given HCL color
119
+ *
120
+ * Note:
121
+ * - HCL values are contained in the intervals:
122
+ * - Hue: [0, 360]
123
+ * - Chroma: [0, ~150]
124
+ * - Lightness: [0, 100]
125
+ *
126
+ * @param {ColorHcl} hcl HCL color
127
+ * @returns {this}
128
+ */
129
+ setHcl(hcl: ColorHcl): this;
130
+ /**
131
+ * Linearly interpolate this color values to given color values
132
+ *
133
+ * @param {number} t Normalized time value to interpolate
134
+ * @param {Color|ColorRgb} color Color to interpolate values towards
135
+ * @returns {this}
136
+ */
137
+ lerp(t: number, [r, g, b]: Color | ColorRgb): this;
138
+ /**
139
+ * Linearly interpolate this color RGB values towards given RGB values
140
+ *
141
+ * @param {number} t Normalized time value to interpolate
142
+ * @param {ColorRgb} rgb RGB values to interpolate towards
143
+ * @param {object} [params] Interpolation parameters
144
+ * @param {number} [params.power] Interpolation exponent
145
+ * @returns {this}
146
+ */
147
+ lerpRgb(t: number, rgb: ColorRgb, params?: Parameters<typeof Color.lerpRgb>[3]): this;
148
+ /**
149
+ * Linearly interpolate this color HSL values towards given HSL values
150
+ *
151
+ * @param {number} t Normalized time value to interpolate
152
+ * @param {ColorHsl} hsl HSL values to interpolate towards
153
+ * @param {object} [params] Interpolation parameters
154
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [h, s, l]
155
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
156
+ * @returns {this}
157
+ */
158
+ lerpHsl(t: number, hsl: ColorHsl, params?: Parameters<typeof Color.lerpHsl>[3]): this;
159
+ /**
160
+ * Linearly interpolate this color HSB values towards given HSB values
161
+ *
162
+ * @param {number} t Normalized time value to interpolate
163
+ * @param {ColorHsb} hsb HSB values to interpolate towards
164
+ * @param {object} [params] Interpolation parameters
165
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [h, s, b]
166
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
167
+ * @returns {this}
168
+ */
169
+ lerpHsb(t: number, hsb: ColorHsb, params?: Parameters<typeof Color.lerpHsb>[3]): this;
170
+ /**
171
+ * Interpolate this color HCL values towards given HCL values following HCL Qualitative color palettes algorithm
172
+ *
173
+ * @param {number} t Normalized time value to interpolate
174
+ * @param {ColorHcl} hcl HCL values to interpolate towards
175
+ * @param {object} [params] Interpolation parameters
176
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
177
+ * @returns {this}
178
+ */
179
+ interpolateQualitative(t: number, hcl: ColorHcl, params?: Parameters<typeof Color.interpolateQualitative>[3]): this;
180
+ /**
181
+ * Interpolate this color HCL values towards given HCL values following HCL Sequential color palettes algorithm
182
+ *
183
+ * @param {number} t Normalized time value to interpolate
184
+ * @param {ColorHcl} hcl HCL values to interpolate towards
185
+ * @param {object} [params] Interpolation parameters
186
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : [c, l]
187
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
188
+ * @param {number} [params.chromaMax] Maximum chroma value
189
+ * @returns {this}
190
+ */
191
+ interpolateSequential(t: number, hcl: ColorHcl, params?: Parameters<typeof Color.interpolateSequential>[3]): this;
192
+ /**
193
+ * Interpolate this color HCL values towards given HCL values following HCL Diverging color palettes algorithm
194
+ *
195
+ * @param {number} t Normalized time value to interpolate
196
+ * @param {ColorHcl} hcl HCL values to interpolate towards
197
+ * @param {object} [params] Interpolation parameters
198
+ * @param {number|number[]} [params.power] Interpolation exponent(s) : ([c, l])
199
+ * @returns {this}
200
+ */
201
+ interpolateDiverging(t: number, hcl: ColorHcl, params?: Parameters<typeof Color.interpolateDiverging>[3]): this;
202
+ /**
203
+ * Check if this color is equal with a given color
204
+ *
205
+ * @param {Color|ColorRgb} color Color to check
206
+ * @returns {boolean} True if this color is equal with the given color, false otherwise
207
+ */
208
+ equals(color: Color | ColorRgb): boolean;
209
+ /**
210
+ * Return this color RGB values into an array
211
+ *
212
+ * @returns {ColorRgb}
213
+ */
214
+ toArray(): ColorRgb;
215
+ /**
216
+ * Set this color RGB values from a given array
217
+ *
218
+ * @param {number[]} values Values to set
219
+ * @returns {this}
220
+ */
221
+ fromArray([r, g, b]: number[]): this;
222
+ /**
223
+ * Copy the RGB values of a given color to this color
224
+ *
225
+ * @param {Color|ColorRgb} color Color to copy values from
226
+ * @returns {this}
227
+ */
228
+ copy([r, g, b]: Color | ColorRgb): this;
229
+ /**
230
+ * Create a new color with copied RGB values from this color
231
+ *
232
+ * @returns {Color}
233
+ */
234
+ clone(): Color;
235
+ /**
236
+ * RGB values of this color
237
+ */
238
+ set rgb(rgb: ColorRgb);
239
+ get rgb(): ColorRgb;
240
+ /**
241
+ * Hexadecimal value of this color
242
+ */
243
+ set hex(hex: ColorHex);
244
+ get hex(): number;
245
+ /**
246
+ * Hexadecimal string representing this color
247
+ */
248
+ get hexString(): string;
249
+ /**
250
+ * HSL values of this color
251
+ */
252
+ set hsl(hsl: ColorHsl);
253
+ get hsl(): ColorHsl;
254
+ /**
255
+ * HSL string representing this color (format: 'hsl(360, 100%, 100%)')
256
+ */
257
+ get hslString(): string;
258
+ /**
259
+ * HSB values of this color
260
+ */
261
+ set hsb(hsb: ColorHsb);
262
+ get hsb(): ColorHsb;
263
+ /**
264
+ * L*a*b* values of this color
265
+ */
266
+ set lab(lab: ColorLab);
267
+ get lab(): ColorLab;
268
+ /**
269
+ * HCL values of this color
270
+ */
271
+ set hcl(hcl: ColorHcl);
272
+ get hcl(): ColorHcl;
273
+ /**
274
+ * Linearly interpolate a color between two colors in the RGB color space
275
+ *
276
+ * @param {number} t Normalized time value to interpolate
277
+ * @param {Color|ColorRgb} rgb1 Start color
278
+ * @param {Color|ColorRgb} rgb2 End color
279
+ * @param {object} [params] Interpolation parameters
280
+ * @param {number} [params.power=1] Interpolation exponent
281
+ * @returns {ColorRgb} Interpolated RGB color
282
+ */
283
+ static lerpRgb(t: number, [r1, g1, b1]: Color | ColorRgb, [r2, g2, b2]: Color | ColorRgb, { power }?: {
284
+ power?: number;
285
+ }): ColorRgb;
286
+ /**
287
+ * Linearly interpolate a color between two colors in the HSL color space
288
+ *
289
+ * @param {number} t Normalized time value to interpolate
290
+ * @param {ColorHsl} hsl1 Start color
291
+ * @param {ColorHsl} hsl2 End color
292
+ * @param {object} [params] Interpolation parameters
293
+ * @param {number|number[]} [params.power=1] Interpolation exponent(s) : [h, s, l]
294
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
295
+ * @returns {ColorHsl} Interpolated HSL color
296
+ */
297
+ static lerpHsl(t: number, [h1, s1, l1]: ColorHsl, [h2, s2, l2]: ColorHsl, { power, hueMode }?: {
298
+ power?: number | [number, number, number];
299
+ hueMode?: HueInterpolationMode;
300
+ }): ColorHsl;
301
+ /**
302
+ * Linearly interpolate a color between two colors in the HSB color space
303
+ *
304
+ * @param {number} t Normalized time value to interpolate
305
+ * @param {ColorHsb} hsb1 Start color
306
+ * @param {ColorHsb} hsb2 End color
307
+ * @param {object} [params] Interpolation parameters
308
+ * @param {number|number[]} [params.power=1] Interpolation exponent(s) : [h, s, b]
309
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
310
+ * @returns {ColorHsb} Interpolated HSB color
311
+ */
312
+ static lerpHsb(t: number, [h1, s1, b1]: ColorHsb, [h2, s2, b2]: ColorHsb, { power, hueMode }?: {
313
+ power?: number | [number, number, number];
314
+ hueMode?: HueInterpolationMode;
315
+ }): ColorHsb;
316
+ /**
317
+ * Interpolate a color between 2 colors following HCL Qualitative color palettes algorithm
318
+ * -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html#qualitative-palettes
319
+ *
320
+ * Qualitative color palettes:
321
+ * - Hue: Linear
322
+ * - Chroma: Constant
323
+ * - Luminance: Constant
324
+ *
325
+ * Designed for coding categorical information,
326
+ * where no particular ordering of categories is available
327
+ * and every color should receive the same perceptual weight.
328
+ *
329
+ * @param {number} t Normalized time value to interpolate
330
+ * @param {ColorHcl} hcl1 Start color
331
+ * @param {ColorHcl} hcl2 End color
332
+ * @param {object} [params] Interpolation parameters
333
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
334
+ * @returns {ColorHcl} Interpolated HCL color
335
+ */
336
+ static interpolateQualitative(t: number, [h1, c1, l1]: ColorHcl, [h2]: ColorHcl, { hueMode }?: {
337
+ hueMode?: HueInterpolationMode;
338
+ }): ColorHcl;
339
+ /**
340
+ * Interpolate a color between 2 colors following HCL Sequential color palettes algorithm
341
+ * -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html#sequential-palettes-single-hue
342
+ * -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html#sequential-palettes-multi-hue
343
+ *
344
+ * Sequential color palettes:
345
+ * - Hue: Constant | Linear
346
+ * - Chroma: Linear (+power) | Triangular (+power)
347
+ * - Luminance: Linear (+power)
348
+ *
349
+ * Designed for coding ordered/numeric information,
350
+ * going from high to low (or vice versa).
351
+ *
352
+ * @param {number} t Normalized time value to interpolate
353
+ * @param {ColorHcl} hcl1 Start color
354
+ * @param {ColorHcl} hcl2 End color
355
+ * @param {object} [params] Interpolation parameters
356
+ * @param {number|number[]} [params.power=1] Interpolation exponent(s) : [c, l]
357
+ * @param {string} [params.hueMode] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
358
+ * @param {number} [params.chromaMax] Maximum chroma value
359
+ * @returns {ColorHcl} Interpolated HCL color
360
+ */
361
+ static interpolateSequential(t: number, [h1, c1, l1]: ColorHcl, [h2, c2, l2]: ColorHcl, { power, hueMode, chromaMax }?: {
362
+ power?: number | [number, number];
363
+ hueMode?: HueInterpolationMode;
364
+ chromaMax?: number;
365
+ }): ColorHcl;
366
+ /**
367
+ * Interpolate a color between 2 colors following HCL Diverging color palettes algorithm
368
+ * -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html#diverging-palettes
369
+ *
370
+ * Diverging color palettes:
371
+ * - Hue: Constants (x2)
372
+ * - Chroma: Linear (+power) | Triangular (+power)
373
+ * - Luminance: Linear (+power)
374
+ *
375
+ * Designed for coding ordered/numeric information around a central neutral value,
376
+ * where colors diverge from neutral to two extremes.
377
+ *
378
+ * @param {number} t Normalized time value to interpolate
379
+ * @param {ColorHcl} hcl1 Start color
380
+ * @param {ColorHcl} hcl2 End color
381
+ * @param {object} [params] Interpolation parameters
382
+ * @param {number|number[]} [params.power=1] Interpolation exponent(s) : ([c, l])
383
+ * @returns {ColorHcl} Interpolated HCL color
384
+ */
385
+ static interpolateDiverging(t: number, [h1, c1, l1]: ColorHcl, [h2, c2, l2]: ColorHcl, { power }?: {
386
+ power?: number | [number, number];
387
+ }): ColorHcl;
388
+ /**
389
+ * Interpolate a hue between two hue angles
390
+ *
391
+ * @param {number} t Normalized time value to interpolate
392
+ * @param {number} h1 Start hue angle (in degrees)
393
+ * @param {number} h2 End hue angle (in degrees)
394
+ * @param {string} [mode='direct'] Hue interpolation mode. Can be 'direct' | 'shortest' | 'longest'
395
+ * @returns {number} Interpolated hue
396
+ */
397
+ static lerpHue(t: number, h1: number, h2: number, mode?: HueInterpolationMode): number;
398
+ /**
399
+ * Check if two colors are equal to each other
400
+ *
401
+ * @param {Color|ColorRgb} color1 First color
402
+ * @param {Color|ColorRgb} color2 Second color
403
+ * @returns {boolean} True if the given colors are equal, false otherwise
404
+ */
405
+ static equals([r1, g1, b1]: Color | ColorRgb, [r2, g2, b2]: Color | ColorRgb): boolean;
406
+ }