toosoon-utils 2.4.1 → 2.4.2
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.js +22 -50
- package/lib/classes/color-scale.js +55 -76
- package/lib/classes/frame-rate.js +19 -27
- 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.js +17 -20
- package/lib/geometry.js +20 -33
- package/lib/maths.js +22 -51
- package/lib/prng.js +36 -60
- package/lib/random.js +26 -55
- package/lib/strings.js +2 -7
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.js +1 -2
- package/package.json +2 -1
- package/tsconfig.json +3 -3
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,43 +51,41 @@ 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;
|
|
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
|
-
return
|
|
63
|
+
const inputHsl = rgbToHsl(inputColor);
|
|
64
|
+
const targetHsl = rgbToHsl(targetColor);
|
|
65
|
+
const h = lerp(value, inputHsl[0], targetHsl[0]);
|
|
66
|
+
const s = lerp(value, inputHsl[1], targetHsl[1]);
|
|
67
|
+
const l = lerp(value, inputHsl[2], targetHsl[2]);
|
|
68
|
+
return hslToRgb([h, s, l]);
|
|
88
69
|
}
|
|
89
70
|
case 'hsb': {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return
|
|
71
|
+
const inputHsb = rgbToHsb(inputColor);
|
|
72
|
+
const targetHsb = rgbToHsb(targetColor);
|
|
73
|
+
const h = lerp(value, inputHsb[0], targetHsb[0]);
|
|
74
|
+
const s = lerp(value, inputHsb[1], targetHsb[1]);
|
|
75
|
+
const b = lerp(value, inputHsb[2], targetHsb[2]);
|
|
76
|
+
return hsbToRgb([h, s, b]);
|
|
96
77
|
}
|
|
97
78
|
case 'hcl':
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
79
|
+
const inputHcl = rgbToHcl(inputColor);
|
|
80
|
+
const targetHcl = rgbToHcl(targetColor);
|
|
81
|
+
const powerValue = Math.pow(value, settings.powerStrength ?? 1);
|
|
82
|
+
const h1 = inputHcl[0];
|
|
83
|
+
const c1 = inputHcl[1];
|
|
84
|
+
const l1 = inputHcl[2];
|
|
85
|
+
const h2 = targetHcl[0] + (settings.hueOffset ?? 0);
|
|
86
|
+
const c2 = targetHcl[1] + (settings.chromaOffset ?? 0);
|
|
87
|
+
const l2 = targetHcl[2] + (settings.luminanceOffset ?? 0);
|
|
88
|
+
let h, c, l;
|
|
108
89
|
// HCL color palettes
|
|
109
90
|
// -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html
|
|
110
91
|
if (settings.mode === 'qualitative') {
|
|
@@ -118,7 +99,7 @@ var ColorScale = /** @class */ (function () {
|
|
|
118
99
|
* - Chroma: Constant
|
|
119
100
|
* - Luminance: Constant
|
|
120
101
|
*/
|
|
121
|
-
h =
|
|
102
|
+
h = lerp(value, h1, h2);
|
|
122
103
|
c = c1;
|
|
123
104
|
l = l1;
|
|
124
105
|
}
|
|
@@ -132,9 +113,9 @@ var ColorScale = /** @class */ (function () {
|
|
|
132
113
|
* - Chroma: Linear (+power) | Triangular (+power)
|
|
133
114
|
* - Luminance: Linear (+power)
|
|
134
115
|
*/
|
|
135
|
-
h =
|
|
136
|
-
c = settings.triangular ?
|
|
137
|
-
l =
|
|
116
|
+
h = lerp(value, h1, h2);
|
|
117
|
+
c = settings.triangular ? triLerp(powerValue, c1, c2, settings.triangular) : lerp(powerValue, c1, c2);
|
|
118
|
+
l = lerp(powerValue, l1, l2);
|
|
138
119
|
}
|
|
139
120
|
else if (settings.mode === 'diverging') {
|
|
140
121
|
/**
|
|
@@ -146,18 +127,16 @@ var ColorScale = /** @class */ (function () {
|
|
|
146
127
|
* - Chroma: Linear (+power) | Triangular (+power)
|
|
147
128
|
* - Luminance: Linear (+power)
|
|
148
129
|
*/
|
|
149
|
-
h = value < 0.5 ? h1 : value > 0.5 ? h2 :
|
|
150
|
-
c = settings.triangular ?
|
|
151
|
-
l =
|
|
130
|
+
h = value < 0.5 ? h1 : value > 0.5 ? h2 : lerp(0.5, h1, h2);
|
|
131
|
+
c = settings.triangular ? triLerp(powerValue, c1, c2, settings.triangular) : lerp(powerValue, c1, c2);
|
|
132
|
+
l = lerp(powerValue, l1, l2);
|
|
152
133
|
}
|
|
153
134
|
else {
|
|
154
|
-
h =
|
|
155
|
-
c =
|
|
156
|
-
l =
|
|
135
|
+
h = lerp(value, h1, h2);
|
|
136
|
+
c = lerp(value, c1, c2);
|
|
137
|
+
l = lerp(value, l1, l2);
|
|
157
138
|
}
|
|
158
|
-
return
|
|
139
|
+
return hclToRgb([h, c, l]);
|
|
159
140
|
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
}());
|
|
163
|
-
exports.default = ColorScale;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -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
|
this.fps = fps;
|
|
22
20
|
}
|
|
@@ -25,26 +23,20 @@ var FrameRate = /** @class */ (function () {
|
|
|
25
23
|
*
|
|
26
24
|
* @returns {boolean}
|
|
27
25
|
*/
|
|
28
|
-
|
|
29
|
-
this.time =
|
|
26
|
+
update() {
|
|
27
|
+
this.time = now();
|
|
30
28
|
this.elapsedTime = this.time - this.lastUpdate;
|
|
31
29
|
if (this.elapsedTime < this.interval) {
|
|
32
30
|
return false;
|
|
33
31
|
}
|
|
34
32
|
this.lastUpdate = this.time - (this.elapsedTime % this.interval);
|
|
35
33
|
return true;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
enumerable: false,
|
|
46
|
-
configurable: true
|
|
47
|
-
});
|
|
48
|
-
return FrameRate;
|
|
49
|
-
}());
|
|
50
|
-
exports.default = FrameRate;
|
|
34
|
+
}
|
|
35
|
+
get fps() {
|
|
36
|
+
return this._fps;
|
|
37
|
+
}
|
|
38
|
+
set fps(fps) {
|
|
39
|
+
this._fps = fps;
|
|
40
|
+
this.interval = 1000 / fps;
|
|
41
|
+
}
|
|
42
|
+
}
|