toosoon-utils 1.1.0 → 1.2.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # TOOSOON UTILS
2
2
 
3
- TOOSOON utility functions.
3
+ Utility functions & classes.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,375 +18,882 @@ $ npm install toosoon-utils
18
18
 
19
19
  ## Usage
20
20
 
21
- ```js
21
+ ```ts
22
22
  import { lerp } from 'toosoon-utils/maths';
23
23
 
24
24
  console.log(lerp(0.5, 0, 5)); // 2.5
25
25
  ```
26
26
 
27
- ## Functions
27
+ ## Utility functions
28
28
 
29
29
  ### Colors
30
30
 
31
+ ##### normalizeHexString(hex)
32
+
33
+ Normalize an hexadecimal string.
34
+
35
+ - `hex`: Hexadecimal string.
36
+
31
37
  ```ts
32
- // Normalize an hexadecimal string
33
- normalizeHexString(hex: string) => string;
38
+ normalizeHexString(hex: string): string;
34
39
  ```
35
40
 
41
+ ##### rgbToHex(rgb)
42
+
43
+ Convert RGB to hexadecimal.
44
+
45
+ - `rgb`: RGB color.
46
+
36
47
  ```ts
37
- // Convert RGB to hexadecimal
38
- rgbToHex([r, g, b]: [number, number, number]) => number;
48
+ rgbToHex([r, g, b]: [number, number, number]): number;
39
49
  ```
40
50
 
51
+ ##### rgbToHexString(rgb)
52
+
53
+ Convert RGB to hexadecimal string.
54
+
55
+ - `rgb`: RGB color.
56
+
41
57
  ```ts
42
- // Convert RGB to hexadecimal string
43
- rgbToHexString([r, g, b]: [number, number, number]) => string;
58
+ rgbToHexString([r, g, b]: [number, number, number]): string;
44
59
  ```
45
60
 
61
+ ##### hexToRgb(hex)
62
+
63
+ Convert hexadecimal to RGB.
64
+
65
+ - `hex`: Hexadecimal color.
66
+
46
67
  ```ts
47
- // Convert hexadecimal to RGB
48
- hexToRgb(hex: number | string) => [number, number, number];
68
+ hexToRgb(hex: number | string): [number, number, number];
49
69
  ```
50
70
 
71
+ ##### lighten(hex, amount)
72
+
73
+ Lighten a color.
74
+
75
+ - `hex`: Hexadecimal color.
76
+ - `[amount=0]`: Amount of the color offset.
77
+
51
78
  ```ts
52
- // Lighten a color
53
- lighten(hex: string, amount?: number) => string;
79
+ lighten(hex: string, amount?: number): string;
54
80
  ```
55
81
 
82
+ ##### darken(hex, amount)
83
+
84
+ Darken a color.
85
+
86
+ - `hex`: Hexadecimal color.
87
+ - `[amount=0]`: Amount of the color offset.
88
+
56
89
  ```ts
57
- // Darken a color
58
- darken(hex: string, amount?: number) => string;
90
+ darken(hex: string, amount?: number): string;
59
91
  ```
60
92
 
93
+ ##### normalizeHslString(hsl)
94
+
95
+ Normalize an HSL string.
96
+
97
+ - `hsl`: HSL string (format: `'hsl(360, 100%, 100%)'`).
98
+
61
99
  ```ts
62
- // Normalize an HSL string
63
- normalizeHslString(hsl: string) => [number, number, number];
100
+ normalizeHslString(hsl: string): [number, number, number];
64
101
  ```
65
102
 
103
+ ##### rgbToHsl(rgb)
104
+
105
+ Convert RGB to HSL.
106
+
107
+ - `rgb`: RGB color.
108
+
66
109
  ```ts
67
- // Convert RGB to HSL
68
- rgbToHsl([r, g, b]: [number, number, number]) => [number, number, number];
110
+ rgbToHsl([r, g, b]: [number, number, number]): [number, number, number];
69
111
  ```
70
112
 
113
+ ##### hslToRgb(hsl)
114
+
115
+ Convert HSL to RGB.
116
+
117
+ - `hsl`: HSL color.
118
+
71
119
  ```ts
72
- // Convert HSL to RGB
73
- hslToRgb([h, s, l]: [number, number, number]) => [number, number, number];
120
+ hslToRgb([h, s, l]: [number, number, number]): [number, number, number];
74
121
  ```
75
122
 
123
+ ##### rgbToHsb(rgb)
124
+
125
+ Convert RGB to HSB.
126
+
127
+ - `rgb`: RGB color.
128
+
76
129
  ```ts
77
- // Convert RGB to HSB
78
- rgbToHsb([r, g, b]: [number, number, number]) => [number, number, number];
130
+ rgbToHsb([r, g, b]: [number, number, number]): [number, number, number];
79
131
  ```
80
132
 
133
+ ##### hsbToRgb(hsb)
134
+
135
+ Convert HSB to RGB.
136
+
137
+ - `hsb`: HSB color.
138
+
81
139
  ```ts
82
- // Convert HSB to RGB
83
- hsbToRgb([h, s, b]: [number, number, number]) => [number, number, number];
140
+ hsbToRgb([h, s, b]: [number, number, number]): [number, number, number];
84
141
  ```
85
142
 
143
+ ##### labToHcl(lab)
144
+
145
+ Convert LAB to HCL.
146
+
147
+ - `lab`: LAB color.
148
+
86
149
  ```ts
87
- // Convert LAB to HCL
88
- labToHcl([l, a, b]: [number, number, number]) => [number, number, number];
150
+ labToHcl([l, a, b]: [number, number, number]): [number, number, number];
89
151
  ```
90
152
 
153
+ ##### hclToLab(hcl)
154
+
155
+ Convert HCL to LAB.
156
+
157
+ - `hcl`: HCL color.
158
+
91
159
  ```ts
92
- // Convert HCL to LAB
93
- hclToLab([h, c, l]: [number, number, number]) => [number, number, number];
160
+ hclToLab([h, c, l]: [number, number, number]): [number, number, number];
94
161
  ```
95
162
 
163
+ ##### labToRgb(lab)
164
+
165
+ Convert LAB to RGB.
166
+
167
+ - `lab`: LAB color.
168
+
96
169
  ```ts
97
- // Convert LAB to RGB
98
- labToRgb([l, a, b]: [number, number, number]) => [number, number, number];
170
+ labToRgb([l, a, b]: [number, number, number]): [number, number, number];
99
171
  ```
100
172
 
173
+ ##### rgbToLab(rgb)
174
+
175
+ Convert RGB to LAB.
176
+
177
+ - `rgb`: RGB color.
178
+
101
179
  ```ts
102
- // Convert RGB to LAB
103
- rgbToLab([r, g, b]: [number, number, number]) => [number, number, number];
180
+ rgbToLab([r, g, b]: [number, number, number]): [number, number, number];
104
181
  ```
105
182
 
183
+ ##### deltaE(labA, labB)
184
+
185
+ Get the delta from two LAB colors.
186
+
187
+ - `labA`: First LAB color.
188
+ - `labB`: Second LAB color.
189
+
106
190
  ```ts
107
- // Get the delta from two LAB colors
108
- deltaE(labA: [number, number, number], labB: [number, number, number]) => number;
191
+ deltaE(labA: [number, number, number], labB: [number, number, number]): number;
109
192
  ```
110
193
 
194
+ ##### rgbToHcl(rgb)
195
+
196
+ Convert RGB to HCL.
197
+
198
+ - `rgb`: RGB color.
199
+
111
200
  ```ts
112
- // Convert RGB to HCL
113
- rgbToHcl([r, g, b]: [number, number, number]) => [number, number, number];
201
+ rgbToHcl([r, g, b]: [number, number, number]): [number, number, number];
114
202
  ```
115
203
 
204
+ ##### hclToRgb(hcl)
205
+
206
+ Convert HCL to RGB.
207
+
208
+ - `hcl`: HCL color.
209
+
116
210
  ```ts
117
- // Convert HCL to RGB
118
- hclToRgb([h, c, l]: [number, number, number]) => [number, number, number];
211
+ hclToRgb([h, c, l]: [number, number, number]): [number, number, number];
119
212
  ```
120
213
 
121
214
  ### DOM
122
215
 
216
+ ##### closest(element, selector)
217
+
218
+ Find the closest parent that matches a selector.
219
+
220
+ - `element`: Target element.
221
+ - `selector`: Selector or parent to match.
222
+
123
223
  ```ts
124
- // Find the closest parent that matches a selector
125
- closest(element: Element, selector: Element | string) => Element | null;
224
+ closest(element: Element, selector: Element | string): Element | null;
126
225
  ```
127
226
 
227
+ ##### createCanvas(width, height)
228
+
229
+ Create a canvas and 2d context.
230
+
231
+ - `width`: Width of the canvas.
232
+ - `height`: Height of the canvas.
233
+
128
234
  ```ts
129
- // Create a canvas and 2d context
130
- createCanvas(width: number, height: number) => { canvas: HTMLCanvasElement; ctx: CanvasRenderingContext2D };
235
+ createCanvas(width: number, height: number): { canvas: HTMLCanvasElement; ctx: CanvasRenderingContext2D };
131
236
  ```
132
237
 
238
+ ##### injectStyles(styles)
239
+
240
+ Inject CSS styles in `document.head`.
241
+
242
+ - `styles`: CSS styles to inject.
243
+
133
244
  ```ts
134
- // Inject CSS styles in `document.head`
135
- injectStyles(cssContent: string) => void;
245
+ injectStyles(styles: string): void;
136
246
  ```
137
247
 
138
248
  ### Files
139
249
 
250
+ ##### download(blob, filename)
251
+
252
+ Download a Blob object into user files.
253
+
254
+ - `blob`: Blob object to download.
255
+ - `filename`: Downloaded file name.
256
+
140
257
  ```ts
141
- // Download a Blob object into user files
142
- download(blob: Blob, filename: string) => void;
258
+ download(blob: Blob, filename: string): void;
143
259
  ```
144
260
 
261
+ ##### upload(onLoad)
262
+
263
+ Upload a file from user files.
264
+
265
+ - `onLoad`: Callback called once the file is loaded.
266
+ - `[accept='']` MIME type the file input should accept.
267
+
145
268
  ```ts
146
- // Upload a file from user files
147
- upload(onLoad: (dataUrl: string) => void, accept?: string) => void;
269
+ upload(onLoad: (dataUrl: string) => void, accept?: string): void;
148
270
  ```
149
271
 
150
272
  ### Functions
151
273
 
274
+ ##### noop()
275
+
276
+ No-op function.
277
+
152
278
  ```ts
153
- // No-op function
154
- noop() => void;
279
+ noop(): void;
155
280
  ```
156
281
 
282
+ ##### wait(timeout)
283
+
284
+ Promise wrapped setTimeout.
285
+
286
+ - `[timeout=0]`: Time to wait (in milliseconds).
287
+
157
288
  ```ts
158
- // Promise wrapped setTimeout
159
- wait(timeout: number) => Promise<void>;
289
+ wait(timeout?: number): Promise<void>;
160
290
  ```
161
291
 
292
+ ##### defer()
293
+
294
+ Deferred promise implementation.
295
+
162
296
  ```ts
163
- // Deferred promise implementation
164
- defer<T>() => Deferred<T>;
297
+ defer<T>(): Deferred<T>;
165
298
  ```
166
299
 
167
- ### Geometry
300
+ ##### now()
301
+
302
+ Polyfill for `now()` functions.
168
303
 
169
304
  ```ts
170
- // Convert a radians value into degrees
171
- toDegrees(radians: number) => number;
305
+ now(): number;
172
306
  ```
173
307
 
308
+ ### Geometry
309
+
310
+ ##### toDegrees(radians)
311
+
312
+ Convert a radians value into degrees.
313
+
314
+ - `radians`: Angle in radians.
315
+
174
316
  ```ts
175
- // Convert a degrees value into radians
176
- toRadians(degrees: number) => number;
317
+ toDegrees(radians: number): number;
177
318
  ```
178
319
 
320
+ ##### toRadians(degrees)
321
+
322
+ Convert a degrees value into radians.
323
+
324
+ - `degrees`: Angle in degrees.
325
+
179
326
  ```ts
180
- // Calculate the angle from a point to another
181
- angle(x1: number, y1: number, x2: number, y2: number) => number;
327
+ toRadians(degrees: number): number;
182
328
  ```
183
329
 
330
+ ##### angle(x1, y1, x2, y2)
331
+
332
+ Calculate the angle from a point to another.
333
+
334
+ - `x1`: X value of the first point.
335
+ - `y1`: Y value of the first point.
336
+ - `x2`: X value of the second point.
337
+ - `y2`: Y value of the second point.
338
+
184
339
  ```ts
185
- // Find the closest angle between to angles
186
- closestAngle(source: number, target: number) => number;
340
+ angle(x1: number, y1: number, x2: number, y2: number): number;
187
341
  ```
188
342
 
343
+ ##### closestAngle(source, target)
344
+
345
+ Find the closest angle between to angles.
346
+
347
+ - `source`: Source angle in radians.
348
+ - `target`: Target angle in radians.
349
+
189
350
  ```ts
190
- // Calculate the distance between two points
191
- distance(x1: number, y1: number, x2: number, y2: number) => number;
351
+ closestAngle(source: number, target: number): number;
192
352
  ```
193
353
 
354
+ ##### distance(x1, y1, x2, y2)
355
+
356
+ Calculate the distance between two points.
357
+
358
+ - `x1`: X coord of the first point.
359
+ - `y1`: Y coord of the first point.
360
+ - `x2`: X coord of the second point.
361
+ - `y2`: Y coord of the second point.
362
+
194
363
  ```ts
195
- // Calculate the length of the diagonal of a rectangle
196
- diagonal(width: number, height: number) => number;
364
+ distance(x1: number, y1: number, x2: number, y2: number): number;
197
365
  ```
198
366
 
367
+ ##### diagonal(width, height)
368
+
369
+ Calculate the length of the diagonal of a rectangle.
370
+
371
+ - `width`: Width of the rectangle.
372
+ - `height`: Height of the rectangle.
373
+
199
374
  ```ts
200
- // Convert radians to a 3D point on the surface of a unit sphere
201
- radToSphere(radius: number, phi: number, theta: number, target?: Vector3) => Vector3;
375
+ diagonal(width: number, height: number): number;
202
376
  ```
203
377
 
378
+ ##### radToSphere(radius, phi, theta)
379
+
380
+ Convert radians to a 3D point on the surface of a unit sphere.
381
+
382
+ - `radius`: Radius of the sphere
383
+ - `phi`: Polar angle from the y (up) axis [0, PI]
384
+ - `theta`: Equator angle around the y (up) axis [0, 2*PI]
385
+ - `[target]`: Target vector
386
+
204
387
  ```ts
205
- // Make a target fit a container (cover mode)
206
- cover(target: FitInput, container: FitInput) => FitOutput;
388
+ radToSphere(radius: number, phi: number, theta: number, target?: Vector3): Vector3;
207
389
  ```
208
390
 
391
+ ##### cover(target, container)
392
+
393
+ Make a target fit a container (cover mode).
394
+
395
+ - `target`: Dimension of the target.
396
+ - `container`: Dimension of the container.
397
+
209
398
  ```ts
210
- // Make a target fit a container (contain mode)
211
- contain(target: FitInput, container: FitInput) => FitOutput;
399
+ cover(target: object, container: object): object;
212
400
  ```
213
401
 
214
- ### Maths
402
+ ##### contain(target, container)
403
+
404
+ Make a target fit a container (contain mode).
405
+
406
+ - `target`: Dimension of the target.
407
+ - `container`: Dimension of the container.
215
408
 
216
409
  ```ts
217
- // Check if a number is even
218
- isEven(value: number) => boolean;
410
+ contain(target: object, container: object): object;
219
411
  ```
220
412
 
413
+ ### Maths
414
+
415
+ ##### isEven(value)
416
+
417
+ Check if a number is even.
418
+
419
+ - `value`: Value to check.
420
+
221
421
  ```ts
222
- // Check if a number is odd
223
- isOdd(value: number) => boolean;
422
+ isEven(value: number): boolean;
224
423
  ```
225
424
 
425
+ ##### isOdd(value)
426
+
427
+ Check if a number is odd.
428
+
429
+ - `value`: Value to check.
430
+
226
431
  ```ts
227
- // Check if a number is a power of 2
228
- isPowerOf2(value: number) => boolean;
432
+ isOdd(value: number): boolean;
229
433
  ```
230
434
 
435
+ ##### isPowerOf2(value)
436
+
437
+ Check if a number is a power of 2.
438
+
439
+ - `value`: Value to check.
440
+
231
441
  ```ts
232
- // Find closest power of 2 that fits a number
233
- toPowerOf2(value: number, mode?: 'floor' | 'ceil' | 'round') => number;
442
+ isPowerOf2(value: number): boolean;
234
443
  ```
235
444
 
445
+ ##### toPowerOf2(value)
446
+
447
+ Find closest power of 2 that fits a number.
448
+
449
+ - `value`: Incoming value.
450
+ - `[mode='ceil']`: Can be `'floor'`, `'ceil'` or `'round'`.
451
+
236
452
  ```ts
237
- // Return the sign (positive or negative) of a number
238
- sign(number: number) => number;
453
+ toPowerOf2(value: number, mode?: string): number;
239
454
  ```
240
455
 
456
+ ##### sign(value)
457
+
458
+ Return the sign (positive or negative) of a number.
459
+
460
+ - `value`: Value to check.
461
+
241
462
  ```ts
242
- // Clamp a value between two bounds
243
- clamp(value: number, min?: number, max?: number) => number;
463
+ sign(value: number): number;
244
464
  ```
245
465
 
466
+ ##### clamp(value, min, max)
467
+
468
+ Clamp a value between two bounds.
469
+
470
+ - `value`: Value to clamp.
471
+ - `[min=0]`: Minimum boundary.
472
+ - `[max=1]`: Maximum boundary.
473
+
246
474
  ```ts
247
- // Linear interpolation between two values (lerping)
248
- lerp(value: number, min: number, max: number) => number;
475
+ clamp(value: number, min?: number, max?: number): number;
249
476
  ```
250
477
 
478
+ ##### lerp(value, min, max)
479
+
480
+ Linear interpolation between two values (lerping).
481
+
482
+ - `value`: Normalized value to interpolate.
483
+ - `min`: Minimum value.
484
+ - `max`: Maximum value.
485
+
251
486
  ```ts
252
- // Triangular interpolation between two values
253
- triLerp(value: number, min: number, max: number, target: number) => number;
487
+ lerp(value: number, min: number, max: number): number;
254
488
  ```
255
489
 
490
+ ##### triLerp(value, min, max, target)
491
+
492
+ Triangular interpolation between two values.
493
+
494
+ - `value`: Normalized value to interpolate.
495
+ - `min`: Minimum value.
496
+ - `max`: Maximum value.
497
+ - `target`: Triangle target value.
498
+
256
499
  ```ts
257
- // Exponential interpolation between two values
258
- expLerp(value: number, min: number, max: number) => number;
500
+ triLerp(value: number, min: number, max: number, target: number): number;
259
501
  ```
260
502
 
503
+ ##### expLerp(value, currentMin, currentMax, targetMin, targetMax)
504
+
505
+ Exponential interpolation between two values.
506
+
507
+ - `value`: Value to interpolate.
508
+ - `currentMin`: Lower bound of the value's current range.
509
+ - `currentMax`: Upper bound of the value's current range.
510
+ - `targetMin`: Lower bound of the value's target range.
511
+ - `targetMax`: Upper bound of the value's target range.
512
+
261
513
  ```ts
262
- // Normalize a value between two bounds
263
- normalize(value: number, min: number, max: number) => number;
514
+ expLerp(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number;
264
515
  ```
265
516
 
517
+ ##### normalize(value, min, max)
518
+
519
+ Normalize a value between two bounds.
520
+
521
+ - `value`: Value to normalize.
522
+ - `min`: Minimum boundary.
523
+ - `max`: Maximum boundary.
524
+
266
525
  ```ts
267
- // Re-map a number from one range to another
268
- map(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number) => number;
526
+ normalize(value: number, min: number, max: number): number;
269
527
  ```
270
528
 
529
+ ##### map(value, currentMin, currentMax, targetMin, targetMax)
530
+
531
+ Re-map a number from one range to another.
532
+
533
+ - `value`: Value to re-map.
534
+ - `currentMin`: Lower bound of the value's current range.
535
+ - `currentMax`: Upper bound of the value's current range.
536
+ - `targetMin`: Lower bound of the value's target range.
537
+ - `targetMax`: Upper bound of the value's target range.
538
+
271
539
  ```ts
272
- // Round a number up to a nearest multiple
273
- roundTo(value: number, multiple?: number) => number;
540
+ map(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number;
274
541
  ```
275
542
 
543
+ ##### roundTo(value, multiple)
544
+
545
+ Round a number up to a nearest multiple.
546
+
547
+ - `value`: Value to round.
548
+ - `[multiple=1]`: Multiple to round to.
549
+
276
550
  ```ts
277
- // Modulo absolute a value based on a length
278
- modAbs(value: number, length: number) => number;
551
+ roundTo(value: number, multiple?: number): number;
279
552
  ```
280
553
 
554
+ ##### modAbs(value, length)
555
+
556
+ Modulo absolute a value based on a length.
557
+
558
+ - `value`: Value to modulate.
559
+ - `length`: Total length.
560
+
281
561
  ```ts
282
- // Move back and forth a value between 0 and length, so that it is never larger than length and never smaller than 0
283
- pingPong(value: number, length: number) => number;
562
+ modAbs(value: number, length: number): number;
284
563
  ```
285
564
 
565
+ ##### pingPong(value, length)
566
+
567
+ Move back and forth a value between 0 and length, so that it is never larger than length and never smaller than 0.
568
+
569
+ - `value`: Value to modulate.
570
+ - `length`: Total length.
571
+
286
572
  ```ts
287
- // Smooth a value using cubic Hermite interpolation
288
- smoothstep(value: number, min?: number, max?: number) => number;
573
+ pingPong(value: number, length: number): number;
289
574
  ```
290
575
 
576
+ ##### smoothstep(value, min, max)
577
+
578
+ Smooth a value using cubic Hermite interpolation.
579
+
580
+ - `value`: Value to smooth.
581
+ - `[min=0]`: Minimum boundary.
582
+ - `[max=1]`: Maximum boundary.
583
+
291
584
  ```ts
292
- // Re-map the [0, 1] interval into [0, 1] parabola, such that corners are remaped to 0 and the center to 1
293
- parabola(x: number, power?: number) => number;
585
+ smoothstep(value: number, min?: number, max?: number): number;
294
586
  ```
295
587
 
588
+ ##### parabola(x, power)
589
+
590
+ Re-map the [0, 1] interval into [0, 1] parabola, such that corners are remaped to 0 and the center to 1.
591
+
592
+ - `x`: Normalized coordinate on X axis.
593
+ - `[power=1]`: Parabola power.
594
+
296
595
  ```ts
297
- // Return the sum of numbers
298
- sum(array: number[]) => number;
596
+ parabola(x: number, power?: number): number;
299
597
  ```
300
598
 
599
+ ##### sum(array)
600
+
601
+ Return the sum of numbers.
602
+
603
+ - `array`: Array of numbers.
604
+
301
605
  ```ts
302
- // Return the average of numbers
303
- average(array: number[]) => number;
606
+ sum(array: number[]): number;
304
607
  ```
305
608
 
609
+ ##### average(array)
610
+
611
+ Return the average of numbers.
612
+
613
+ - `array`: Array of numbers.
614
+
306
615
  ```ts
307
- // Smoothly interpolate a number toward another
308
- damp(value: number, target: number, damping: number, delta: number) => number;
616
+ average(array: number[]): number;
309
617
  ```
310
618
 
311
- ### Now
619
+ ##### damp(value, target, damping, delta)
620
+
621
+ Smoothly interpolate a number toward another.
622
+
623
+ - `value`: Value to interpolate.
624
+ - `target`: Destination of the interpolation.
625
+ - `damping`: A higher value will make the movement more sudden, and a lower value will make the movement more gradual.
626
+ - `delta`: Delta time (in seconds).
312
627
 
313
628
  ```ts
314
- // Polyfill for "now()" functions
315
- now() => number;
629
+ damp(value: number, target: number, damping: number, delta: number): number;
316
630
  ```
317
631
 
318
632
  ### Random
319
633
 
634
+ ##### randomBoolean(probability)
635
+
636
+ Generate a random boolean (true or false).
637
+
638
+ - `[probability=0.5]`: Probability to get `true`.
639
+
320
640
  ```ts
321
- // Generate a random boolean (true or false)
322
- randomBoolean(probability?: number) => boolean;
641
+ randomBoolean(probability?: number): boolean;
323
642
  ```
324
643
 
644
+ ##### randomSign(probability)
645
+
646
+ Generate a random sign (1 or -1).
647
+
648
+ - `[probability=0.5]`: Probability to get 1.
649
+
325
650
  ```ts
326
- // Generate a random sign (1 or -1)
327
- randomSign(probability?: number) => number;
651
+ randomSign(probability?: number): number;
328
652
  ```
329
653
 
654
+ ##### randomFloat(min, max)
655
+
656
+ Generate a random floating-point number within a specified range.
657
+
658
+ - `[min=0]`: Minimum boundary.
659
+ - `[max=1]`: Maximum boundary.
660
+ - `[precision=2]`: Number of digits after the decimal point.
661
+
330
662
  ```ts
331
- // Generate a random float number
332
- randomFloat(min?: number, max?: number, precision?: number) => number;
663
+ randomFloat(min?: number, max?: number, precision?: number): number;
333
664
  ```
334
665
 
666
+ ##### randomInt(min, max)
667
+
668
+ Generate a random integer number within a specified range.
669
+
670
+ - `min`: Minimum boundary.
671
+ - `max`: Maximum boundary.
672
+
335
673
  ```ts
336
- // Generate a random integer number
337
- randomInt(min: number, max: number) => number;
674
+ randomInt(min: number, max: number): number;
338
675
  ```
339
676
 
677
+ ##### randomHexColor()
678
+
679
+ Generate a random hexadecimal color.
680
+
340
681
  ```ts
341
- // Generate a random hexadecimal color
342
- randomHexColor() => string;
682
+ randomHexColor(): string;
343
683
  ```
344
684
 
685
+ ##### randomItem(array)
686
+
687
+ Pick a random item from a given array.
688
+
689
+ - `array`: Array to pick the item from.
690
+
345
691
  ```ts
346
- // Pick a random item from an array
347
- randomItem<T>(array: T[] = []) => T | undefined;
692
+ randomItem<T>(array: T[]): T | undefined;
348
693
  ```
349
694
 
695
+ ##### randomObjectProperty(object)
696
+
697
+ Pick a random property value from a given object.
698
+
699
+ - `object`: Object to pick the property from.
700
+
350
701
  ```ts
351
- // Pick a random property from an object
352
- randomObjectProperty<T>(object: { [key: string]: T }) => T | undefined;
702
+ randomObjectProperty<T>(object: { [key: string]: T }): T | undefined;
353
703
  ```
354
704
 
705
+ ##### randomIndex(weights)
706
+
707
+ Select a random index from an array of weighted items.
708
+
709
+ - `weights`: Array of weights.
710
+
355
711
  ```ts
356
- // Return a random index from an array of weights
357
- randomIndex(weights?: number[]) => number;
712
+ randomIndex(weights?: number[]): number;
358
713
  ```
359
714
 
715
+ ##### onCircle(radius)
716
+
717
+ Produce a random 2D point around the perimiter of a unit circle.
718
+
719
+ - `[radius=1]`: Radius of the circle.
720
+ - `[target]`: Target vector.
721
+
360
722
  ```ts
361
- // Produce a random 2D point around the perimiter of a unit circle
362
- onCircle(radius?: number, target?: Vector2) => Vector2;
723
+ onCircle(radius?: number, target?: Vector2): Vector2;
363
724
  ```
364
725
 
726
+ ##### insideCircle(radius)
727
+
728
+ Produce a random 2D point inside a unit circle.
729
+
730
+ - `[radius=1]`: Radius of the circle.
731
+ - `[target]` Target vector.
732
+
365
733
  ```ts
366
- // Produce a random 2D point inside a unit circle
367
- insideCircle(radius?: number, target?: Vector2) => Vector2;
734
+ insideCircle(radius?: number, target?: Vector2): Vector2;
368
735
  ```
369
736
 
737
+ ##### onSphere(radius)
738
+
739
+ Produce a random 3D point on the surface of a unit sphere.
740
+
741
+ - `[radius=1]`: Radius of the sphere.
742
+ - `[target]`: Target vector.
743
+
370
744
  ```ts
371
- // Produce a random 3D point on the surface of a unit sphere
372
- onSphere(radius?: number, target?: Vector3) => Vector3;
745
+ onSphere(radius?: number, target?: Vector3): Vector3;
373
746
  ```
374
747
 
748
+ ##### insideSphere(radius)
749
+
750
+ Produce a random 3D point inside a unit sphere.
751
+
752
+ - `[radius=1]`: Radius of the sphere.
753
+ - `[target]`: Target vector.
754
+
375
755
  ```ts
376
- // Produce a random 3D point inside a unit sphere
377
- insideSphere(radius?: number, target?: Vector3) => Vector3;
756
+ insideSphere(radius?: number, target?: Vector3): Vector3;
378
757
  ```
379
758
 
380
759
  ### Strings
381
760
 
761
+ ##### capitalize(string)
762
+
763
+ Capitalize a string.
764
+
765
+ - `string`: String to capitalize.
766
+
767
+ ```ts
768
+ capitalize(string: string): string;
769
+ ```
770
+
771
+ ##### cleanPath(path)
772
+
773
+ Clean a path by removing params.
774
+
775
+ - `path`: Path to clean.
776
+
382
777
  ```ts
383
- // Capitalize a string
384
- capitalize(string: string) => string;
778
+ cleanPath(path: string): string;
385
779
  ```
386
780
 
781
+ ## Utility classes
782
+
783
+ ### Color scale
784
+
785
+ Utility class for generating color scales and interpolating between colors.
786
+
787
+ - [new ColorScale(input, target)](#color-scale-constructor)
788
+ - [.colors](#color-scale-colors): `Array<[number, number, number]>`
789
+ - `static` [.generate(input, target, length)](#color-scale-static-generate-method): `Array<[number, number, number]>`
790
+ - `static` [.interpolate(inputColor, targetColor, value)](#color-scale-static-interpolate-method): `[number, number, number]`
791
+
792
+ #### Constructor <a id="color-scale-constructor"></a>
793
+
794
+ | Parameter | Type | Default | Description |
795
+ | --------------------- | ---------------------------------- | ----------------------- | ------------------------------------------- |
796
+ | input | `ColorRepresentation` | | Input color representation. |
797
+ | target | `ColorRepresentation` | | Target color representation. |
798
+ | length | `number` | `5` | Amount of colors composing the color scale. |
799
+ | settings | `ColorScaleSettings` | `{ colorSpace: 'rgb' }` | Color scale generation settings. |
800
+ | [settings.colorSpace] | `'rgb' \| 'hsl' \| 'hsb' \| 'hcl'` | `'rgb'` | Color scale color space. |
801
+
802
+ ##### HCL color scales
803
+
804
+ HCL color scales come with a bunch of other settings, so when `settings.colorSpace` is equal to `'hcl'`, the following settings are parameterable:
805
+
806
+ | Parameter | Type | Default | Description |
807
+ | -------------------------- | ---------------------------------------------- | ------- | ---------------------------------------- |
808
+ | [settings.mode] | `'qualitative' \| 'sequential' \| 'diverging'` | | Color scale mode. |
809
+ | [settings.triangular] | `number` | | Triangular interpolation `target` value. |
810
+ | [settings.powerStrength] | `number` | `1` | Interpolation power strength value. |
811
+ | [settings.hueOffset] | `number` | `0` | Target color hue offset. |
812
+ | [settings.chromaOffset] | `number` | `0` | Target color chroma offset. |
813
+ | [settings.luminanceOffset] | `number` | `0` | Target color luminance offset. |
814
+
815
+ Learn more about [HCL-Based Color Palettes](https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html).
816
+
817
+ #### Properties
818
+
819
+ ##### colors <a id="color-scale-colors"></a>
820
+
821
+ Array of colors composing the color scale.
822
+
823
+ ```ts
824
+ ColorScale.colors: Array<[number, number, number]>;
825
+ ```
826
+
827
+ #### Methods
828
+
829
+ ##### `static` generate(input, target, length) <a id="color-scale-static-generate-method"></a>
830
+
831
+ Static method for generating a color scale.
832
+
833
+ - `input`: Input color representation.
834
+ - `target`: Target color representation.
835
+ - `length`: Amount of colors composing the color scale.
836
+ - `[settings]`: Color scale generation settings.
837
+
838
+ ```ts
839
+ static ColorScale.generate(
840
+ input: ColorRepresentation,
841
+ target: ColorRepresentation,
842
+ length: number,
843
+ settings?: ColorScaleSettings
844
+ ): Array<[number, number, number]>;
845
+ ```
846
+
847
+ ##### `static` interpolate(inputColor, targetColor, value) <a id="color-scale-static-interpolate-method"></a>
848
+
849
+ Static method for interpolating between colors.
850
+
851
+ - `inputColor`: Input color.
852
+ - `targetColor`: Target color.
853
+ - `value`: Interpolation normalized value.
854
+ - `[settings]`: Color scale settings.
855
+
856
+ ```ts
857
+ static ColorScale.interpolate(
858
+ inputColor: [number, number, number],
859
+ targetColor: [number, number, number],
860
+ value: number,
861
+ settings?: ColorScaleSettings
862
+ ): [number, number, number];
863
+ ```
864
+
865
+ ### Frame rate
866
+
867
+ Utility class for controlling FPS calls.
868
+
869
+ - [new FrameRate()](#frame-rate-constructor)
870
+ - [.fps](#frame-rate-fps): `number`
871
+ - [.update()](#frame-rate-update-method): `boolean`
872
+
873
+ #### Constructor <a id="frame-rate-constructor"></a>
874
+
875
+ | Parameter | Type | Default | Description |
876
+ | --------- | -------- | ------- | ----------------------- |
877
+ | fps | `number` | `30` | Frame per second limit. |
878
+
879
+ #### Properties
880
+
881
+ ##### fps <a id="frame-rate-fps"></a>
882
+
883
+ Frame per second limit.
884
+
885
+ ```ts
886
+ FrameRate.fps: number;
887
+ ```
888
+
889
+ #### Methods
890
+
891
+ ##### update() <a id="frame-rate-update-method"></a>
892
+
893
+ Return true if elapsed time since last update is higher than current FPS.
894
+
387
895
  ```ts
388
- // Clean a path by removing params
389
- cleanPath(path: string) => string;
896
+ FrameRate.update(): boolean;
390
897
  ```
391
898
 
392
899
  ## Constants