toosoon-utils 4.0.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/lib/classes/_pool.d.ts +2 -2
- package/lib/classes/_pool.js +22 -47
- package/lib/classes/color-scale.js +54 -72
- package/lib/classes/frame-rate.js +17 -23
- package/lib/colors.js +70 -86
- package/lib/constants.js +6 -6
- package/lib/dom.js +7 -8
- package/lib/files.js +7 -9
- package/lib/functions.d.ts +1 -1
- package/lib/functions.js +17 -26
- package/lib/geometry.d.ts +10 -10
- package/lib/geometry.js +18 -19
- package/lib/maths.js +8 -15
- package/lib/prng.js +31 -39
- package/lib/query.js +6 -6
- package/lib/random.js +23 -39
- package/lib/strings.js +7 -7
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +6 -6
- package/package.json +1 -1
- package/tsconfig.json +16 -12
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,27 +1,7 @@
|
|
|
1
1
|
// *********************
|
|
2
2
|
// WIP
|
|
3
3
|
// *********************
|
|
4
|
-
|
|
5
|
-
__assign = Object.assign || function(t) {
|
|
6
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
7
|
-
s = arguments[i];
|
|
8
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
9
|
-
t[p] = s[p];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
|
-
return __assign.apply(this, arguments);
|
|
14
|
-
};
|
|
15
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
16
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
17
|
-
if (ar || !(i in from)) {
|
|
18
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
19
|
-
ar[i] = from[i];
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
23
|
-
};
|
|
24
|
-
export var defaultSettings = {
|
|
4
|
+
export const defaultSettings = {
|
|
25
5
|
max: Infinity
|
|
26
6
|
};
|
|
27
7
|
/**
|
|
@@ -30,12 +10,11 @@ export var defaultSettings = {
|
|
|
30
10
|
* @exports
|
|
31
11
|
* @class Pool
|
|
32
12
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.settings = __assign({}, defaultSettings);
|
|
13
|
+
export default class Pool {
|
|
14
|
+
items = [];
|
|
15
|
+
pool = [];
|
|
16
|
+
settings = { ...defaultSettings };
|
|
17
|
+
constructor(settings = { ...defaultSettings }) {
|
|
39
18
|
this.settings = Object.assign(this.settings, settings);
|
|
40
19
|
}
|
|
41
20
|
/**
|
|
@@ -43,50 +22,46 @@ var Pool = /** @class */ (function () {
|
|
|
43
22
|
*
|
|
44
23
|
* @param {PoolItem} item Item to add to the active items
|
|
45
24
|
*/
|
|
46
|
-
|
|
25
|
+
add(item) {
|
|
47
26
|
this.items.push(item);
|
|
48
|
-
}
|
|
27
|
+
}
|
|
49
28
|
/**
|
|
50
29
|
* Remove an item from the active items
|
|
51
30
|
*
|
|
52
31
|
* @param {PoolItem} item Item to remove from the active items
|
|
53
32
|
*/
|
|
54
|
-
|
|
55
|
-
this.items = this.items.filter(
|
|
56
|
-
}
|
|
33
|
+
remove(item) {
|
|
34
|
+
this.items = this.items.filter((_item) => _item !== item);
|
|
35
|
+
}
|
|
57
36
|
/**
|
|
58
37
|
* Return an item from pool or create a new one
|
|
59
38
|
*
|
|
60
39
|
* @returns {PoolItem|undefined}
|
|
61
40
|
*/
|
|
62
|
-
|
|
63
|
-
var _a, _b;
|
|
41
|
+
get() {
|
|
64
42
|
if (this.items.length >= this.settings.max)
|
|
65
43
|
return;
|
|
66
|
-
|
|
67
|
-
|
|
44
|
+
const item = this.pool.pop() ?? this.create();
|
|
45
|
+
item.setup?.();
|
|
68
46
|
this.add(item);
|
|
69
47
|
return item;
|
|
70
|
-
}
|
|
48
|
+
}
|
|
71
49
|
/**
|
|
72
50
|
* Release an item from the active items and add it to the pool
|
|
73
51
|
*
|
|
74
52
|
* @param {PoolItem} item Item to release
|
|
75
53
|
*/
|
|
76
|
-
|
|
77
|
-
var _a;
|
|
54
|
+
release(item) {
|
|
78
55
|
this.pool.push(item);
|
|
79
|
-
|
|
56
|
+
item.reset?.();
|
|
80
57
|
this.remove(item);
|
|
81
|
-
}
|
|
58
|
+
}
|
|
82
59
|
/**
|
|
83
60
|
* Dispose all items
|
|
84
61
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
62
|
+
dispose() {
|
|
63
|
+
[...this.items, ...this.pool].forEach((item) => item.dispose?.());
|
|
87
64
|
this.items = [];
|
|
88
65
|
this.pool = [];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
}());
|
|
92
|
-
export default Pool;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -1,17 +1,6 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
import { lerp, triLerp } from '../maths';
|
|
13
2
|
import { hclToRgb, hsbToRgb, hslToRgb, normalizeColor, rgbToHcl, rgbToHsb, rgbToHsl } from '../colors';
|
|
14
|
-
export
|
|
3
|
+
export const defaultSettings = {
|
|
15
4
|
colorSpace: 'rgb'
|
|
16
5
|
};
|
|
17
6
|
/**
|
|
@@ -20,20 +9,18 @@ export var defaultSettings = {
|
|
|
20
9
|
* @exports
|
|
21
10
|
* @class ColorScale
|
|
22
11
|
*/
|
|
23
|
-
|
|
12
|
+
export default class ColorScale {
|
|
13
|
+
/**
|
|
14
|
+
* Array of colors composing the color scale
|
|
15
|
+
*/
|
|
16
|
+
colors = [];
|
|
24
17
|
/**
|
|
25
18
|
* @param {ColorRepresentation} input Input color representation
|
|
26
19
|
* @param {ColorRepresentation} target Target color representation
|
|
27
20
|
* @param {number} [length=5] Amount of colors composing the color scale
|
|
28
21
|
* @param {ColorScaleSettings} [settings] Color scale generation settings
|
|
29
22
|
*/
|
|
30
|
-
|
|
31
|
-
if (length === void 0) { length = 5; }
|
|
32
|
-
if (settings === void 0) { settings = __assign({}, defaultSettings); }
|
|
33
|
-
/**
|
|
34
|
-
* Array of colors composing the color scale
|
|
35
|
-
*/
|
|
36
|
-
this.colors = [];
|
|
23
|
+
constructor(input, target, length = 5, settings = { ...defaultSettings }) {
|
|
37
24
|
this.colors = ColorScale.generate(input, target, length, settings);
|
|
38
25
|
}
|
|
39
26
|
/**
|
|
@@ -45,17 +32,16 @@ var ColorScale = /** @class */ (function () {
|
|
|
45
32
|
* @param {ColorScaleSettings} [settings] Color scale generation settings
|
|
46
33
|
* @returns {Array<[number, number, number]>} Color scale colors
|
|
47
34
|
*/
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
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);
|
|
55
41
|
colors.push(ColorScale.interpolate(inputColor, targetColor, value, settings));
|
|
56
42
|
}
|
|
57
43
|
return colors;
|
|
58
|
-
}
|
|
44
|
+
}
|
|
59
45
|
/**
|
|
60
46
|
* Static method for interpolating between colors
|
|
61
47
|
*
|
|
@@ -65,55 +51,53 @@ var ColorScale = /** @class */ (function () {
|
|
|
65
51
|
* @param {ColorScaleSettings} [settings] Color scale settings
|
|
66
52
|
* @returns {[number,number,number]} Interpolated color
|
|
67
53
|
*/
|
|
68
|
-
|
|
69
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
70
|
-
if (settings === void 0) { settings = __assign({}, defaultSettings); }
|
|
54
|
+
static interpolate(inputColor, targetColor, value, settings = { ...defaultSettings }) {
|
|
71
55
|
switch (settings.colorSpace) {
|
|
72
56
|
case 'rgb': {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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]);
|
|
76
60
|
return [r, g, b];
|
|
77
61
|
}
|
|
78
62
|
case 'hsl': {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return hslToRgb([
|
|
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]);
|
|
91
75
|
}
|
|
92
76
|
case 'hsb': {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
return hsbToRgb([
|
|
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]);
|
|
105
89
|
}
|
|
106
90
|
case 'hcl':
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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;
|
|
117
101
|
// HCL color palettes
|
|
118
102
|
// -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html
|
|
119
103
|
switch (settings.mode) {
|
|
@@ -168,7 +152,5 @@ var ColorScale = /** @class */ (function () {
|
|
|
168
152
|
}
|
|
169
153
|
return hclToRgb([h, c, l]);
|
|
170
154
|
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
}());
|
|
174
|
-
export default ColorScale;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -5,16 +5,16 @@ import { now } from '../functions';
|
|
|
5
5
|
* @exports
|
|
6
6
|
* @class FrameRate
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
export default class FrameRate {
|
|
9
|
+
_fps;
|
|
10
|
+
_interval = 0;
|
|
11
|
+
_time = 0;
|
|
12
|
+
_elapsedTime = 0;
|
|
13
|
+
_lastUpdate = 0;
|
|
9
14
|
/**
|
|
10
15
|
* @param {number} [fps=30] Frame per second limit
|
|
11
16
|
*/
|
|
12
|
-
|
|
13
|
-
if (fps === void 0) { fps = 30; }
|
|
14
|
-
this._interval = 0;
|
|
15
|
-
this._time = 0;
|
|
16
|
-
this._elapsedTime = 0;
|
|
17
|
-
this._lastUpdate = 0;
|
|
17
|
+
constructor(fps = 30) {
|
|
18
18
|
this.fps = fps;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
@@ -22,7 +22,7 @@ var FrameRate = /** @class */ (function () {
|
|
|
22
22
|
*
|
|
23
23
|
* @returns {boolean}
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
update() {
|
|
26
26
|
this._time = now();
|
|
27
27
|
this._elapsedTime = this._time - this._lastUpdate;
|
|
28
28
|
if (this._elapsedTime < this._interval) {
|
|
@@ -30,18 +30,12 @@ var FrameRate = /** @class */ (function () {
|
|
|
30
30
|
}
|
|
31
31
|
this._lastUpdate = this._time - (this._elapsedTime % this._interval);
|
|
32
32
|
return true;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
enumerable: false,
|
|
43
|
-
configurable: true
|
|
44
|
-
});
|
|
45
|
-
return FrameRate;
|
|
46
|
-
}());
|
|
47
|
-
export 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
|
+
}
|