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 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) are exist with additionnal parameters for a `seed` string and a PRNG `algorithm` function.
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
 
@@ -2,11 +2,11 @@ export type PoolSettings = {
2
2
  max?: number;
3
3
  };
4
4
  export declare const defaultSettings: Required<PoolSettings>;
5
- interface PoolItem {
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
  *
@@ -1,30 +1,7 @@
1
- "use strict";
2
1
  // *********************
3
2
  // WIP
4
3
  // *********************
5
- var __assign = (this && this.__assign) || function () {
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
- var Pool = /** @class */ (function () {
37
- function Pool(settings) {
38
- if (settings === void 0) { settings = __assign({}, exports.defaultSettings); }
39
- this.items = [];
40
- this.pool = [];
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
- Pool.prototype.add = function (item) {
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
- Pool.prototype.remove = function (item) {
58
- this.items = this.items.filter(function (_item) { return _item !== item; });
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
- Pool.prototype.get = function () {
66
- var _a, _b;
41
+ get() {
67
42
  if (this.items.length >= this.settings.max)
68
43
  return;
69
- var item = (_a = this.pool.pop()) !== null && _a !== void 0 ? _a : this.create();
70
- (_b = item.setup) === null || _b === void 0 ? void 0 : _b.call(item);
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
- Pool.prototype.release = function (item) {
80
- var _a;
54
+ release(item) {
81
55
  this.pool.push(item);
82
- (_a = item.reset) === null || _a === void 0 ? void 0 : _a.call(item);
56
+ item.reset?.();
83
57
  this.remove(item);
84
- };
58
+ }
85
59
  /**
86
60
  * Dispose all items
87
61
  */
88
- Pool.prototype.dispose = function () {
89
- __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); });
62
+ dispose() {
63
+ [...this.items, ...this.pool].forEach((item) => item.dispose?.());
90
64
  this.items = [];
91
65
  this.pool = [];
92
- };
93
- return Pool;
94
- }());
95
- exports.default = Pool;
66
+ }
67
+ }
@@ -1,4 +1,4 @@
1
- import { ColorRepresentation } from '../types';
1
+ import type { ColorRepresentation } from '../types';
2
2
  export type ColorScaleSettings = {
3
3
  colorSpace: 'rgb';
4
4
  } | {
@@ -1,20 +1,6 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
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
- var ColorScale = /** @class */ (function () {
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
- function ColorScale(input, target, length, settings) {
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
- ColorScale.generate = function (input, target, length, settings) {
52
- if (settings === void 0) { settings = __assign({}, exports.defaultSettings); }
53
- var colors = [];
54
- var inputColor = (0, colors_1.normalizeColor)(input);
55
- var targetColor = (0, colors_1.normalizeColor)(target);
56
- for (var i = 0; i < length; i++) {
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
- ColorScale.interpolate = function (inputColor, targetColor, value, settings) {
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
- var r = (0, maths_1.lerp)(value, inputColor[0], targetColor[0]);
77
- var g = (0, maths_1.lerp)(value, inputColor[1], targetColor[1]);
78
- var b = (0, maths_1.lerp)(value, inputColor[2], targetColor[2]);
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
- var inputHsl = (0, colors_1.rgbToHsl)(inputColor);
83
- var targetHsl = (0, colors_1.rgbToHsl)(targetColor);
84
- var h1_1 = inputHsl[0];
85
- var s1 = inputHsl[1];
86
- var l1_1 = inputHsl[2];
87
- var h2_1 = targetHsl[0] + ((_a = settings.hueOffset) !== null && _a !== void 0 ? _a : 0);
88
- var s2 = targetHsl[1] + ((_b = settings.saturationOffset) !== null && _b !== void 0 ? _b : 0);
89
- var l2_1 = targetHsl[2] + ((_c = settings.lightnessOffset) !== null && _c !== void 0 ? _c : 0);
90
- var h_1 = (0, maths_1.lerp)(value, h1_1, h2_1);
91
- var s = (0, maths_1.lerp)(value, s1, s2);
92
- var l_1 = (0, maths_1.lerp)(value, l1_1, l2_1);
93
- return (0, colors_1.hslToRgb)([h_1, s, l_1]);
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
- var inputHsb = (0, colors_1.rgbToHsb)(inputColor);
97
- var targetHsb = (0, colors_1.rgbToHsb)(targetColor);
98
- var h1_2 = inputHsb[0];
99
- var s1 = inputHsb[1];
100
- var b1 = inputHsb[2];
101
- var h2_2 = targetHsb[0] + ((_d = settings.hueOffset) !== null && _d !== void 0 ? _d : 0);
102
- var s2 = targetHsb[1] + ((_e = settings.saturationOffset) !== null && _e !== void 0 ? _e : 0);
103
- var b2 = targetHsb[2] + ((_f = settings.brightnessOffset) !== null && _f !== void 0 ? _f : 0);
104
- var h_2 = (0, maths_1.lerp)(value, h1_2, h2_2);
105
- var s = (0, maths_1.lerp)(value, s1, s2);
106
- var b = (0, maths_1.lerp)(value, b1, b2);
107
- return (0, colors_1.hsbToRgb)([h_2, s, b]);
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
- var inputHcl = (0, colors_1.rgbToHcl)(inputColor);
111
- var targetHcl = (0, colors_1.rgbToHcl)(targetColor);
112
- var powerValue = Math.pow(value, (_g = settings.powerStrength) !== null && _g !== void 0 ? _g : 1);
113
- var h1 = inputHcl[0];
114
- var c1 = inputHcl[1];
115
- var l1 = inputHcl[2];
116
- var h2 = targetHcl[0] + ((_h = settings.hueOffset) !== null && _h !== void 0 ? _h : 0);
117
- var c2 = targetHcl[1] + ((_j = settings.chromaOffset) !== null && _j !== void 0 ? _j : 0);
118
- var l2 = targetHcl[2] + ((_k = settings.luminanceOffset) !== null && _k !== void 0 ? _k : 0);
119
- var h = void 0, c = void 0, l = void 0;
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 = (0, maths_1.lerp)(value, h1, h2);
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 = (0, maths_1.lerp)(value, h1, h2);
149
- c = settings.triangular ? (0, maths_1.triLerp)(powerValue, c1, c2, settings.triangular) : (0, maths_1.lerp)(powerValue, c1, c2);
150
- l = (0, maths_1.lerp)(powerValue, l1, l2);
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 : (0, maths_1.lerp)(0.5, h1, h2);
163
- c = settings.triangular ? (0, maths_1.triLerp)(powerValue, c1, c2, settings.triangular) : (0, maths_1.lerp)(powerValue, c1, c2);
164
- l = (0, maths_1.lerp)(powerValue, l1, l2);
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 = (0, maths_1.lerp)(value, h1, h2);
168
- c = (0, maths_1.lerp)(value, c1, c2);
169
- l = (0, maths_1.lerp)(value, l1, l2);
148
+ h = lerp(value, h1, h2);
149
+ c = lerp(value, c1, c2);
150
+ l = lerp(value, l1, l2);
170
151
  }
171
152
  }
172
- return (0, colors_1.hclToRgb)([h, c, l]);
153
+ return hclToRgb([h, c, l]);
173
154
  }
174
- };
175
- return ColorScale;
176
- }());
177
- exports.default = ColorScale;
155
+ }
156
+ }
@@ -1,22 +1,20 @@
1
- "use strict";
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
- var FrameRate = /** @class */ (function () {
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
- function FrameRate(fps) {
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
- FrameRate.prototype.update = function () {
28
- this._time = (0, functions_1.now)();
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
- Object.defineProperty(FrameRate.prototype, "fps", {
37
- get: function () {
38
- return this._fps;
39
- },
40
- set: function (fps) {
41
- this._fps = fps;
42
- this._interval = 1000 / fps;
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
@@ -1,4 +1,4 @@
1
- import { ColorRepresentation } from './types';
1
+ import type { ColorRepresentation } from './types';
2
2
  /**
3
3
  * Normalize a color representation into RGB
4
4
  *