toosoon-utils 3.1.0 → 4.0.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 +1 -1
- package/lib/classes/_pool.d.ts +2 -2
- package/lib/classes/_pool.js +22 -50
- package/lib/classes/color-scale.d.ts +1 -1
- package/lib/classes/color-scale.js +67 -88
- package/lib/classes/frame-rate.js +19 -27
- package/lib/colors.d.ts +1 -1
- package/lib/colors.js +85 -123
- package/lib/constants.js +6 -9
- package/lib/dom.js +10 -17
- package/lib/files.js +8 -15
- package/lib/functions.d.ts +1 -1
- package/lib/functions.js +27 -40
- package/lib/geometry.d.ts +11 -11
- package/lib/geometry.js +30 -43
- package/lib/maths.js +22 -51
- package/lib/prng.js +43 -70
- package/lib/query.js +9 -15
- package/lib/random.d.ts +1 -1
- package/lib/random.js +30 -62
- package/lib/strings.js +17 -30
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +6 -6
- package/lib/types.js +1 -2
- package/package.json +3 -1
- package/tsconfig.json +17 -11
package/README.md
CHANGED
|
@@ -847,7 +847,7 @@ xoshiro128ss(a: number, b: number, c: number, d: number): number;
|
|
|
847
847
|
|
|
848
848
|
#### PRNG functions
|
|
849
849
|
|
|
850
|
-
Thanks to the above algorithms, a seed-based version of most of the [random functions](#random)
|
|
850
|
+
Thanks to the above algorithms, a seed-based version of most of the [random functions](#random) exist with additionnal parameters for a `seed` string and a PRNG `algorithm` function.
|
|
851
851
|
|
|
852
852
|
PRNG parameters:
|
|
853
853
|
|
package/lib/classes/_pool.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ export type PoolSettings = {
|
|
|
2
2
|
max?: number;
|
|
3
3
|
};
|
|
4
4
|
export declare const defaultSettings: Required<PoolSettings>;
|
|
5
|
-
|
|
5
|
+
type PoolItem = {
|
|
6
6
|
setup?: () => void;
|
|
7
7
|
reset?: () => void;
|
|
8
8
|
dispose?: () => void;
|
|
9
|
-
}
|
|
9
|
+
};
|
|
10
10
|
/**
|
|
11
11
|
* Abstract class for manipulating pool items
|
|
12
12
|
*
|
package/lib/classes/_pool.js
CHANGED
|
@@ -1,30 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
// *********************
|
|
3
2
|
// WIP
|
|
4
3
|
// *********************
|
|
5
|
-
|
|
6
|
-
__assign = Object.assign || function(t) {
|
|
7
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
8
|
-
s = arguments[i];
|
|
9
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
10
|
-
t[p] = s[p];
|
|
11
|
-
}
|
|
12
|
-
return t;
|
|
13
|
-
};
|
|
14
|
-
return __assign.apply(this, arguments);
|
|
15
|
-
};
|
|
16
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
17
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
18
|
-
if (ar || !(i in from)) {
|
|
19
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
20
|
-
ar[i] = from[i];
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.defaultSettings = void 0;
|
|
27
|
-
exports.defaultSettings = {
|
|
4
|
+
export const defaultSettings = {
|
|
28
5
|
max: Infinity
|
|
29
6
|
};
|
|
30
7
|
/**
|
|
@@ -33,12 +10,11 @@ exports.defaultSettings = {
|
|
|
33
10
|
* @exports
|
|
34
11
|
* @class Pool
|
|
35
12
|
*/
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.settings = __assign({}, exports.defaultSettings);
|
|
13
|
+
export default class Pool {
|
|
14
|
+
items = [];
|
|
15
|
+
pool = [];
|
|
16
|
+
settings = { ...defaultSettings };
|
|
17
|
+
constructor(settings = { ...defaultSettings }) {
|
|
42
18
|
this.settings = Object.assign(this.settings, settings);
|
|
43
19
|
}
|
|
44
20
|
/**
|
|
@@ -46,50 +22,46 @@ var Pool = /** @class */ (function () {
|
|
|
46
22
|
*
|
|
47
23
|
* @param {PoolItem} item Item to add to the active items
|
|
48
24
|
*/
|
|
49
|
-
|
|
25
|
+
add(item) {
|
|
50
26
|
this.items.push(item);
|
|
51
|
-
}
|
|
27
|
+
}
|
|
52
28
|
/**
|
|
53
29
|
* Remove an item from the active items
|
|
54
30
|
*
|
|
55
31
|
* @param {PoolItem} item Item to remove from the active items
|
|
56
32
|
*/
|
|
57
|
-
|
|
58
|
-
this.items = this.items.filter(
|
|
59
|
-
}
|
|
33
|
+
remove(item) {
|
|
34
|
+
this.items = this.items.filter((_item) => _item !== item);
|
|
35
|
+
}
|
|
60
36
|
/**
|
|
61
37
|
* Return an item from pool or create a new one
|
|
62
38
|
*
|
|
63
39
|
* @returns {PoolItem|undefined}
|
|
64
40
|
*/
|
|
65
|
-
|
|
66
|
-
var _a, _b;
|
|
41
|
+
get() {
|
|
67
42
|
if (this.items.length >= this.settings.max)
|
|
68
43
|
return;
|
|
69
|
-
|
|
70
|
-
|
|
44
|
+
const item = this.pool.pop() ?? this.create();
|
|
45
|
+
item.setup?.();
|
|
71
46
|
this.add(item);
|
|
72
47
|
return item;
|
|
73
|
-
}
|
|
48
|
+
}
|
|
74
49
|
/**
|
|
75
50
|
* Release an item from the active items and add it to the pool
|
|
76
51
|
*
|
|
77
52
|
* @param {PoolItem} item Item to release
|
|
78
53
|
*/
|
|
79
|
-
|
|
80
|
-
var _a;
|
|
54
|
+
release(item) {
|
|
81
55
|
this.pool.push(item);
|
|
82
|
-
|
|
56
|
+
item.reset?.();
|
|
83
57
|
this.remove(item);
|
|
84
|
-
}
|
|
58
|
+
}
|
|
85
59
|
/**
|
|
86
60
|
* Dispose all items
|
|
87
61
|
*/
|
|
88
|
-
|
|
89
|
-
|
|
62
|
+
dispose() {
|
|
63
|
+
[...this.items, ...this.pool].forEach((item) => item.dispose?.());
|
|
90
64
|
this.items = [];
|
|
91
65
|
this.pool = [];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
}());
|
|
95
|
-
exports.default = Pool;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -1,20 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
-
exports.defaultSettings = void 0;
|
|
15
|
-
var maths_1 = require("../maths");
|
|
16
|
-
var colors_1 = require("../colors");
|
|
17
|
-
exports.defaultSettings = {
|
|
1
|
+
import { lerp, triLerp } from '../maths';
|
|
2
|
+
import { hclToRgb, hsbToRgb, hslToRgb, normalizeColor, rgbToHcl, rgbToHsb, rgbToHsl } from '../colors';
|
|
3
|
+
export const defaultSettings = {
|
|
18
4
|
colorSpace: 'rgb'
|
|
19
5
|
};
|
|
20
6
|
/**
|
|
@@ -23,20 +9,18 @@ exports.defaultSettings = {
|
|
|
23
9
|
* @exports
|
|
24
10
|
* @class ColorScale
|
|
25
11
|
*/
|
|
26
|
-
|
|
12
|
+
export default class ColorScale {
|
|
13
|
+
/**
|
|
14
|
+
* Array of colors composing the color scale
|
|
15
|
+
*/
|
|
16
|
+
colors = [];
|
|
27
17
|
/**
|
|
28
18
|
* @param {ColorRepresentation} input Input color representation
|
|
29
19
|
* @param {ColorRepresentation} target Target color representation
|
|
30
20
|
* @param {number} [length=5] Amount of colors composing the color scale
|
|
31
21
|
* @param {ColorScaleSettings} [settings] Color scale generation settings
|
|
32
22
|
*/
|
|
33
|
-
|
|
34
|
-
if (length === void 0) { length = 5; }
|
|
35
|
-
if (settings === void 0) { settings = __assign({}, exports.defaultSettings); }
|
|
36
|
-
/**
|
|
37
|
-
* Array of colors composing the color scale
|
|
38
|
-
*/
|
|
39
|
-
this.colors = [];
|
|
23
|
+
constructor(input, target, length = 5, settings = { ...defaultSettings }) {
|
|
40
24
|
this.colors = ColorScale.generate(input, target, length, settings);
|
|
41
25
|
}
|
|
42
26
|
/**
|
|
@@ -48,17 +32,16 @@ var ColorScale = /** @class */ (function () {
|
|
|
48
32
|
* @param {ColorScaleSettings} [settings] Color scale generation settings
|
|
49
33
|
* @returns {Array<[number, number, number]>} Color scale colors
|
|
50
34
|
*/
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
var value = i / Math.floor(length);
|
|
35
|
+
static generate(input, target, length, settings = { ...defaultSettings }) {
|
|
36
|
+
const colors = [];
|
|
37
|
+
const inputColor = normalizeColor(input);
|
|
38
|
+
const targetColor = normalizeColor(target);
|
|
39
|
+
for (let i = 0; i < length; i++) {
|
|
40
|
+
const value = i / Math.floor(length);
|
|
58
41
|
colors.push(ColorScale.interpolate(inputColor, targetColor, value, settings));
|
|
59
42
|
}
|
|
60
43
|
return colors;
|
|
61
|
-
}
|
|
44
|
+
}
|
|
62
45
|
/**
|
|
63
46
|
* Static method for interpolating between colors
|
|
64
47
|
*
|
|
@@ -68,55 +51,53 @@ var ColorScale = /** @class */ (function () {
|
|
|
68
51
|
* @param {ColorScaleSettings} [settings] Color scale settings
|
|
69
52
|
* @returns {[number,number,number]} Interpolated color
|
|
70
53
|
*/
|
|
71
|
-
|
|
72
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
73
|
-
if (settings === void 0) { settings = __assign({}, exports.defaultSettings); }
|
|
54
|
+
static interpolate(inputColor, targetColor, value, settings = { ...defaultSettings }) {
|
|
74
55
|
switch (settings.colorSpace) {
|
|
75
56
|
case 'rgb': {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
57
|
+
const r = lerp(value, inputColor[0], targetColor[0]);
|
|
58
|
+
const g = lerp(value, inputColor[1], targetColor[1]);
|
|
59
|
+
const b = lerp(value, inputColor[2], targetColor[2]);
|
|
79
60
|
return [r, g, b];
|
|
80
61
|
}
|
|
81
62
|
case 'hsl': {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return
|
|
63
|
+
const inputHsl = rgbToHsl(inputColor);
|
|
64
|
+
const targetHsl = rgbToHsl(targetColor);
|
|
65
|
+
const h1 = inputHsl[0];
|
|
66
|
+
const s1 = inputHsl[1];
|
|
67
|
+
const l1 = inputHsl[2];
|
|
68
|
+
const h2 = targetHsl[0] + (settings.hueOffset ?? 0);
|
|
69
|
+
const s2 = targetHsl[1] + (settings.saturationOffset ?? 0);
|
|
70
|
+
const l2 = targetHsl[2] + (settings.lightnessOffset ?? 0);
|
|
71
|
+
const h = lerp(value, h1, h2);
|
|
72
|
+
const s = lerp(value, s1, s2);
|
|
73
|
+
const l = lerp(value, l1, l2);
|
|
74
|
+
return hslToRgb([h, s, l]);
|
|
94
75
|
}
|
|
95
76
|
case 'hsb': {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return
|
|
77
|
+
const inputHsb = rgbToHsb(inputColor);
|
|
78
|
+
const targetHsb = rgbToHsb(targetColor);
|
|
79
|
+
const h1 = inputHsb[0];
|
|
80
|
+
const s1 = inputHsb[1];
|
|
81
|
+
const b1 = inputHsb[2];
|
|
82
|
+
const h2 = targetHsb[0] + (settings.hueOffset ?? 0);
|
|
83
|
+
const s2 = targetHsb[1] + (settings.saturationOffset ?? 0);
|
|
84
|
+
const b2 = targetHsb[2] + (settings.brightnessOffset ?? 0);
|
|
85
|
+
const h = lerp(value, h1, h2);
|
|
86
|
+
const s = lerp(value, s1, s2);
|
|
87
|
+
const b = lerp(value, b1, b2);
|
|
88
|
+
return hsbToRgb([h, s, b]);
|
|
108
89
|
}
|
|
109
90
|
case 'hcl':
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
91
|
+
const inputHcl = rgbToHcl(inputColor);
|
|
92
|
+
const targetHcl = rgbToHcl(targetColor);
|
|
93
|
+
const powerValue = Math.pow(value, settings.powerStrength ?? 1);
|
|
94
|
+
const h1 = inputHcl[0];
|
|
95
|
+
const c1 = inputHcl[1];
|
|
96
|
+
const l1 = inputHcl[2];
|
|
97
|
+
const h2 = targetHcl[0] + (settings.hueOffset ?? 0);
|
|
98
|
+
const c2 = targetHcl[1] + (settings.chromaOffset ?? 0);
|
|
99
|
+
const l2 = targetHcl[2] + (settings.luminanceOffset ?? 0);
|
|
100
|
+
let h, c, l;
|
|
120
101
|
// HCL color palettes
|
|
121
102
|
// -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html
|
|
122
103
|
switch (settings.mode) {
|
|
@@ -131,7 +112,7 @@ var ColorScale = /** @class */ (function () {
|
|
|
131
112
|
* - Luminance: Constant
|
|
132
113
|
*/
|
|
133
114
|
case 'qualitative': {
|
|
134
|
-
h =
|
|
115
|
+
h = lerp(value, h1, h2);
|
|
135
116
|
c = c1;
|
|
136
117
|
l = l1;
|
|
137
118
|
}
|
|
@@ -145,9 +126,9 @@ var ColorScale = /** @class */ (function () {
|
|
|
145
126
|
* - Luminance: Linear (+power)
|
|
146
127
|
*/
|
|
147
128
|
case 'sequential': {
|
|
148
|
-
h =
|
|
149
|
-
c = settings.triangular ?
|
|
150
|
-
l =
|
|
129
|
+
h = lerp(value, h1, h2);
|
|
130
|
+
c = settings.triangular ? triLerp(powerValue, c1, c2, settings.triangular) : lerp(powerValue, c1, c2);
|
|
131
|
+
l = lerp(powerValue, l1, l2);
|
|
151
132
|
}
|
|
152
133
|
/**
|
|
153
134
|
* Diverging
|
|
@@ -159,19 +140,17 @@ var ColorScale = /** @class */ (function () {
|
|
|
159
140
|
* - Luminance: Linear (+power)
|
|
160
141
|
*/
|
|
161
142
|
case 'diverging': {
|
|
162
|
-
h = value < 0.5 ? h1 : value > 0.5 ? h2 :
|
|
163
|
-
c = settings.triangular ?
|
|
164
|
-
l =
|
|
143
|
+
h = value < 0.5 ? h1 : value > 0.5 ? h2 : lerp(0.5, h1, h2);
|
|
144
|
+
c = settings.triangular ? triLerp(powerValue, c1, c2, settings.triangular) : lerp(powerValue, c1, c2);
|
|
145
|
+
l = lerp(powerValue, l1, l2);
|
|
165
146
|
}
|
|
166
147
|
default: {
|
|
167
|
-
h =
|
|
168
|
-
c =
|
|
169
|
-
l =
|
|
148
|
+
h = lerp(value, h1, h2);
|
|
149
|
+
c = lerp(value, c1, c2);
|
|
150
|
+
l = lerp(value, l1, l2);
|
|
170
151
|
}
|
|
171
152
|
}
|
|
172
|
-
return
|
|
153
|
+
return hclToRgb([h, c, l]);
|
|
173
154
|
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
}());
|
|
177
|
-
exports.default = ColorScale;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -1,22 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var functions_1 = require("../functions");
|
|
1
|
+
import { now } from '../functions';
|
|
4
2
|
/**
|
|
5
3
|
* Utility class for controlling FPS calls
|
|
6
4
|
*
|
|
7
5
|
* @exports
|
|
8
6
|
* @class FrameRate
|
|
9
7
|
*/
|
|
10
|
-
|
|
8
|
+
export default class FrameRate {
|
|
9
|
+
_fps;
|
|
10
|
+
_interval = 0;
|
|
11
|
+
_time = 0;
|
|
12
|
+
_elapsedTime = 0;
|
|
13
|
+
_lastUpdate = 0;
|
|
11
14
|
/**
|
|
12
15
|
* @param {number} [fps=30] Frame per second limit
|
|
13
16
|
*/
|
|
14
|
-
|
|
15
|
-
if (fps === void 0) { fps = 30; }
|
|
16
|
-
this._interval = 0;
|
|
17
|
-
this._time = 0;
|
|
18
|
-
this._elapsedTime = 0;
|
|
19
|
-
this._lastUpdate = 0;
|
|
17
|
+
constructor(fps = 30) {
|
|
20
18
|
this.fps = fps;
|
|
21
19
|
}
|
|
22
20
|
/**
|
|
@@ -24,26 +22,20 @@ var FrameRate = /** @class */ (function () {
|
|
|
24
22
|
*
|
|
25
23
|
* @returns {boolean}
|
|
26
24
|
*/
|
|
27
|
-
|
|
28
|
-
this._time =
|
|
25
|
+
update() {
|
|
26
|
+
this._time = now();
|
|
29
27
|
this._elapsedTime = this._time - this._lastUpdate;
|
|
30
28
|
if (this._elapsedTime < this._interval) {
|
|
31
29
|
return false;
|
|
32
30
|
}
|
|
33
31
|
this._lastUpdate = this._time - (this._elapsedTime % this._interval);
|
|
34
32
|
return true;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
enumerable: false,
|
|
45
|
-
configurable: true
|
|
46
|
-
});
|
|
47
|
-
return FrameRate;
|
|
48
|
-
}());
|
|
49
|
-
exports.default = FrameRate;
|
|
33
|
+
}
|
|
34
|
+
get fps() {
|
|
35
|
+
return this._fps;
|
|
36
|
+
}
|
|
37
|
+
set fps(fps) {
|
|
38
|
+
this._fps = fps;
|
|
39
|
+
this._interval = 1000 / fps;
|
|
40
|
+
}
|
|
41
|
+
}
|
package/lib/colors.d.ts
CHANGED