toosoon-utils 1.0.6 → 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.
@@ -0,0 +1,56 @@
1
+ export type PoolSettings = {
2
+ max?: number;
3
+ };
4
+ export declare const defaultSettings: Required<PoolSettings>;
5
+ interface PoolItem {
6
+ setup?: () => void;
7
+ reset?: () => void;
8
+ dispose?: () => void;
9
+ }
10
+ /**
11
+ * Abstract class for manipulating pool items
12
+ *
13
+ * @exports
14
+ * @class Pool
15
+ */
16
+ export default abstract class Pool<I extends PoolItem> {
17
+ items: I[];
18
+ pool: I[];
19
+ settings: Required<PoolSettings>;
20
+ constructor(settings?: PoolSettings);
21
+ /**
22
+ * Abstract method to implement custom item creation
23
+ *
24
+ * @returns {PoolItem}
25
+ */
26
+ protected abstract create(): I;
27
+ /**
28
+ * Add an item to the active items
29
+ *
30
+ * @param {PoolItem} item Item to add to the active items
31
+ */
32
+ protected add(item: I): void;
33
+ /**
34
+ * Remove an item from the active items
35
+ *
36
+ * @param {PoolItem} item Item to remove from the active items
37
+ */
38
+ protected remove(item: I): void;
39
+ /**
40
+ * Return an item from pool or create a new one
41
+ *
42
+ * @returns {PoolItem|undefined}
43
+ */
44
+ get(): I | undefined;
45
+ /**
46
+ * Release an item from the active items and add it to the pool
47
+ *
48
+ * @param {PoolItem} item Item to release
49
+ */
50
+ release(item: I): void;
51
+ /**
52
+ * Dispose all items
53
+ */
54
+ dispose(): void;
55
+ }
56
+ export {};
@@ -0,0 +1,92 @@
1
+ // *********************
2
+ // WIP
3
+ // *********************
4
+ var __assign = (this && this.__assign) || function () {
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 = {
25
+ max: Infinity
26
+ };
27
+ /**
28
+ * Abstract class for manipulating pool items
29
+ *
30
+ * @exports
31
+ * @class Pool
32
+ */
33
+ var Pool = /** @class */ (function () {
34
+ function Pool(settings) {
35
+ if (settings === void 0) { settings = __assign({}, defaultSettings); }
36
+ this.items = [];
37
+ this.pool = [];
38
+ this.settings = __assign({}, defaultSettings);
39
+ this.settings = Object.assign(this.settings, settings);
40
+ }
41
+ /**
42
+ * Add an item to the active items
43
+ *
44
+ * @param {PoolItem} item Item to add to the active items
45
+ */
46
+ Pool.prototype.add = function (item) {
47
+ this.items.push(item);
48
+ };
49
+ /**
50
+ * Remove an item from the active items
51
+ *
52
+ * @param {PoolItem} item Item to remove from the active items
53
+ */
54
+ Pool.prototype.remove = function (item) {
55
+ this.items = this.items.filter(function (_item) { return _item !== item; });
56
+ };
57
+ /**
58
+ * Return an item from pool or create a new one
59
+ *
60
+ * @returns {PoolItem|undefined}
61
+ */
62
+ Pool.prototype.get = function () {
63
+ var _a, _b;
64
+ if (this.items.length >= this.settings.max)
65
+ return;
66
+ var item = (_a = this.pool.pop()) !== null && _a !== void 0 ? _a : this.create();
67
+ (_b = item.setup) === null || _b === void 0 ? void 0 : _b.call(item);
68
+ this.add(item);
69
+ return item;
70
+ };
71
+ /**
72
+ * Release an item from the active items and add it to the pool
73
+ *
74
+ * @param {PoolItem} item Item to release
75
+ */
76
+ Pool.prototype.release = function (item) {
77
+ var _a;
78
+ this.pool.push(item);
79
+ (_a = item.reset) === null || _a === void 0 ? void 0 : _a.call(item);
80
+ this.remove(item);
81
+ };
82
+ /**
83
+ * Dispose all items
84
+ */
85
+ Pool.prototype.dispose = function () {
86
+ __spreadArray(__spreadArray([], this.items, true), this.pool, true).forEach(function (item) { var _a; return (_a = item.dispose) === null || _a === void 0 ? void 0 : _a.call(item); });
87
+ this.items = [];
88
+ this.pool = [];
89
+ };
90
+ return Pool;
91
+ }());
92
+ export default Pool;
@@ -0,0 +1,52 @@
1
+ import { ColorRepresentation } from '../types';
2
+ export type ColorScaleSettings = {
3
+ colorSpace: 'rgb' | 'hsl' | 'hsb';
4
+ } | {
5
+ colorSpace: 'hcl';
6
+ mode?: 'qualitative' | 'sequential' | 'diverging';
7
+ triangular?: number;
8
+ powerStrength?: number;
9
+ hueOffset?: number;
10
+ chromaOffset?: number;
11
+ luminanceOffset?: number;
12
+ };
13
+ export declare const defaultSettings: Required<ColorScaleSettings>;
14
+ /**
15
+ * Utility class for generating color scales and interpolating between colors
16
+ *
17
+ * @exports
18
+ * @class ColorScale
19
+ */
20
+ export default class ColorScale {
21
+ /**
22
+ * Array of colors composing the color scale
23
+ */
24
+ colors: Array<[number, number, number]>;
25
+ /**
26
+ * @param {ColorRepresentation} input Input color representation
27
+ * @param {ColorRepresentation} target Target color representation
28
+ * @param {number} [length=5] Amount of colors composing the color scale
29
+ * @param {ColorScaleSettings} [settings] Color scale generation settings
30
+ */
31
+ constructor(input: ColorRepresentation, target: ColorRepresentation, length?: number, settings?: ColorScaleSettings);
32
+ /**
33
+ * Static method for generating a color scale
34
+ *
35
+ * @param {ColorRepresentation} input Input color representation
36
+ * @param {ColorRepresentation} target Target color representation
37
+ * @param {number} length Amount of colors composing the color scale
38
+ * @param {ColorScaleSettings} [settings] Color scale generation settings
39
+ * @returns {Array<[number, number, number]>} Color scale colors
40
+ */
41
+ static generate(input: ColorRepresentation, target: ColorRepresentation, length: number, settings?: ColorScaleSettings): Array<[number, number, number]>;
42
+ /**
43
+ * Static method for interpolating between colors
44
+ *
45
+ * @param {[number,number,number]} inputColor Input color
46
+ * @param {[number,number,number]} targetColor Target color
47
+ * @param {number} value Interpolation normalized value
48
+ * @param {ColorScaleSettings} [settings] Color scale settings
49
+ * @returns {[number,number,number]} Interpolated color
50
+ */
51
+ static interpolate(inputColor: [number, number, number], targetColor: [number, number, number], value: number, settings?: ColorScaleSettings): [number, number, number];
52
+ }
@@ -0,0 +1,160 @@
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
+ import { lerp, triLerp } from '../maths';
13
+ import { hclToRgb, hsbToRgb, hslToRgb, normalizeColor, rgbToHcl, rgbToHsb, rgbToHsl } from '../colors';
14
+ export var defaultSettings = {
15
+ colorSpace: 'rgb'
16
+ };
17
+ /**
18
+ * Utility class for generating color scales and interpolating between colors
19
+ *
20
+ * @exports
21
+ * @class ColorScale
22
+ */
23
+ var ColorScale = /** @class */ (function () {
24
+ /**
25
+ * @param {ColorRepresentation} input Input color representation
26
+ * @param {ColorRepresentation} target Target color representation
27
+ * @param {number} [length=5] Amount of colors composing the color scale
28
+ * @param {ColorScaleSettings} [settings] Color scale generation settings
29
+ */
30
+ function ColorScale(input, target, length, settings) {
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 = [];
37
+ this.colors = ColorScale.generate(input, target, length, settings);
38
+ }
39
+ /**
40
+ * Static method for generating a color scale
41
+ *
42
+ * @param {ColorRepresentation} input Input color representation
43
+ * @param {ColorRepresentation} target Target color representation
44
+ * @param {number} length Amount of colors composing the color scale
45
+ * @param {ColorScaleSettings} [settings] Color scale generation settings
46
+ * @returns {Array<[number, number, number]>} Color scale colors
47
+ */
48
+ ColorScale.generate = function (input, target, length, settings) {
49
+ if (settings === void 0) { settings = __assign({}, defaultSettings); }
50
+ var colors = [];
51
+ var inputColor = normalizeColor(input);
52
+ var targetColor = normalizeColor(target);
53
+ for (var i = 0; i < length; i++) {
54
+ var value = i / Math.floor(length);
55
+ colors.push(ColorScale.interpolate(inputColor, targetColor, value, settings));
56
+ }
57
+ return colors;
58
+ };
59
+ /**
60
+ * Static method for interpolating between colors
61
+ *
62
+ * @param {[number,number,number]} inputColor Input color
63
+ * @param {[number,number,number]} targetColor Target color
64
+ * @param {number} value Interpolation normalized value
65
+ * @param {ColorScaleSettings} [settings] Color scale settings
66
+ * @returns {[number,number,number]} Interpolated color
67
+ */
68
+ ColorScale.interpolate = function (inputColor, targetColor, value, settings) {
69
+ var _a, _b, _c, _d;
70
+ if (settings === void 0) { settings = __assign({}, defaultSettings); }
71
+ switch (settings.colorSpace) {
72
+ case 'rgb': {
73
+ var r = lerp(value, inputColor[0], targetColor[0]);
74
+ var g = lerp(value, inputColor[1], targetColor[1]);
75
+ var b = lerp(value, inputColor[2], targetColor[2]);
76
+ return [r, g, b];
77
+ }
78
+ case 'hsl': {
79
+ var inputHsl = rgbToHsl(inputColor);
80
+ var targetHsl = rgbToHsl(targetColor);
81
+ var h_1 = lerp(value, inputHsl[0], targetHsl[0]);
82
+ var s = lerp(value, inputHsl[1], targetHsl[1]);
83
+ var l_1 = lerp(value, inputHsl[2], targetHsl[2]);
84
+ return hslToRgb([h_1, s, l_1]);
85
+ }
86
+ case 'hsb': {
87
+ var inputHsb = rgbToHsb(inputColor);
88
+ var targetHsb = rgbToHsb(targetColor);
89
+ var h_2 = lerp(value, inputHsb[0], targetHsb[0]);
90
+ var s = lerp(value, inputHsb[1], targetHsb[1]);
91
+ var b = lerp(value, inputHsb[2], targetHsb[2]);
92
+ return hsbToRgb([h_2, s, b]);
93
+ }
94
+ case 'hcl':
95
+ var inputHcl = rgbToHcl(inputColor);
96
+ var targetHcl = rgbToHcl(targetColor);
97
+ var powerValue = Math.pow(value, (_a = settings.powerStrength) !== null && _a !== void 0 ? _a : 1);
98
+ var h1 = inputHcl[0];
99
+ var c1 = inputHcl[1];
100
+ var l1 = inputHcl[2];
101
+ var h2 = targetHcl[0] + ((_b = settings.hueOffset) !== null && _b !== void 0 ? _b : 0);
102
+ var c2 = targetHcl[1] + ((_c = settings.chromaOffset) !== null && _c !== void 0 ? _c : 0);
103
+ var l2 = targetHcl[2] + ((_d = settings.luminanceOffset) !== null && _d !== void 0 ? _d : 0);
104
+ var h = void 0, c = void 0, l = void 0;
105
+ // HCL color palettes
106
+ // -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html
107
+ if (settings.mode === 'qualitative') {
108
+ /**
109
+ * Qualitative
110
+ * Designed for coding categorical information,
111
+ * where no particular ordering of categories is available
112
+ * and every color should receive the same perceptual weight.
113
+ *
114
+ * - Hue: Linear
115
+ * - Chroma: Constant
116
+ * - Luminance: Constant
117
+ */
118
+ h = lerp(value, h1, h2);
119
+ c = c1;
120
+ l = l1;
121
+ }
122
+ else if (settings.mode === 'sequential') {
123
+ /**
124
+ * Sequential
125
+ * Designed for coding ordered/numeric information,
126
+ * going from high to low (or vice versa).
127
+ *
128
+ * - Hue: Constant | Linear
129
+ * - Chroma: Linear (+power) | Triangular (+power)
130
+ * - Luminance: Linear (+power)
131
+ */
132
+ h = lerp(value, h1, h2);
133
+ c = settings.triangular ? triLerp(powerValue, c1, c2, settings.triangular) : lerp(powerValue, c1, c2);
134
+ l = lerp(powerValue, l1, l2);
135
+ }
136
+ else if (settings.mode === 'diverging') {
137
+ /**
138
+ * Diverging
139
+ * Designed for coding ordered/numeric information around a central neutral value,
140
+ * where colors diverge from neutral to two extremes.
141
+ *
142
+ * - Hue: Constants (x2)
143
+ * - Chroma: Linear (+power) | Triangular (+power)
144
+ * - Luminance: Linear (+power)
145
+ */
146
+ h = value < 0.5 ? h1 : value > 0.5 ? h2 : lerp(0.5, h1, h2);
147
+ c = settings.triangular ? triLerp(powerValue, c1, c2, settings.triangular) : lerp(powerValue, c1, c2);
148
+ l = lerp(powerValue, l1, l2);
149
+ }
150
+ else {
151
+ h = lerp(value, h1, h2);
152
+ c = lerp(value, c1, c2);
153
+ l = lerp(value, l1, l2);
154
+ }
155
+ return hclToRgb([h, c, l]);
156
+ }
157
+ };
158
+ return ColorScale;
159
+ }());
160
+ export default ColorScale;
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Utility class for controlling FPS calls
3
+ *
4
+ * @exports
5
+ * @class FrameRate
6
+ */
7
+ export default class FrameRate {
8
+ private _fps;
9
+ private interval;
10
+ private time;
11
+ private elapsedTime;
12
+ private lastUpdate;
13
+ /**
14
+ * @param {number} [fps=30] Frame per second limit
15
+ */
16
+ constructor(fps?: number);
17
+ /**
18
+ * Return true if elapsed time since last update is higher than current FPS
19
+ *
20
+ * @returns {boolean}
21
+ */
22
+ update(): boolean;
23
+ get fps(): number;
24
+ set fps(fps: number);
25
+ }
@@ -0,0 +1,48 @@
1
+ import { now } from '../functions';
2
+ /**
3
+ * Utility class for controlling FPS calls
4
+ *
5
+ * @exports
6
+ * @class FrameRate
7
+ */
8
+ var FrameRate = /** @class */ (function () {
9
+ /**
10
+ * @param {number} [fps=30] Frame per second limit
11
+ */
12
+ function FrameRate(fps) {
13
+ if (fps === void 0) { fps = 30; }
14
+ this.interval = 0;
15
+ this.time = 0;
16
+ this.elapsedTime = 0;
17
+ this.lastUpdate = 0;
18
+ this._fps = fps;
19
+ this.fps = fps;
20
+ }
21
+ /**
22
+ * Return true if elapsed time since last update is higher than current FPS
23
+ *
24
+ * @returns {boolean}
25
+ */
26
+ FrameRate.prototype.update = function () {
27
+ this.time = now();
28
+ this.elapsedTime = this.time - this.lastUpdate;
29
+ if (this.elapsedTime < this.interval) {
30
+ return false;
31
+ }
32
+ this.lastUpdate = this.time - (this.elapsedTime % this.interval);
33
+ return true;
34
+ };
35
+ Object.defineProperty(FrameRate.prototype, "fps", {
36
+ get: function () {
37
+ return this._fps;
38
+ },
39
+ set: function (fps) {
40
+ this._fps = fps;
41
+ this.interval = 1000 / fps;
42
+ },
43
+ enumerable: false,
44
+ configurable: true
45
+ });
46
+ return FrameRate;
47
+ }());
48
+ export default FrameRate;
package/lib/colors.d.ts CHANGED
@@ -1,7 +1,15 @@
1
+ import { ColorRepresentation } from './types';
2
+ /**
3
+ * Normalize a color representation into RGB
4
+ *
5
+ * @param {ColorRepresentation} color Color representation
6
+ * @returns {[number,number,number]} Normalized RGB color
7
+ */
8
+ export declare function normalizeColor(color: ColorRepresentation): [number, number, number];
1
9
  /**
2
10
  * Normalize an hexadecimal string
3
11
  *
4
- * @param {string} hex Hexadecimal string
12
+ * @param {string} hex Hexadecimal string
5
13
  * @returns {string} Normalized hexadecimal string
6
14
  */
7
15
  export declare function normalizeHexString(hex: string): string;