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.
@@ -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,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,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
- ColorScale.interpolate = function (inputColor, targetColor, value, settings) {
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
- 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 h_1 = (0, maths_1.lerp)(value, inputHsl[0], targetHsl[0]);
85
- var s = (0, maths_1.lerp)(value, inputHsl[1], targetHsl[1]);
86
- var l_1 = (0, maths_1.lerp)(value, inputHsl[2], targetHsl[2]);
87
- return (0, colors_1.hslToRgb)([h_1, s, l_1]);
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
- var inputHsb = (0, colors_1.rgbToHsb)(inputColor);
91
- var targetHsb = (0, colors_1.rgbToHsb)(targetColor);
92
- var h_2 = (0, maths_1.lerp)(value, inputHsb[0], targetHsb[0]);
93
- var s = (0, maths_1.lerp)(value, inputHsb[1], targetHsb[1]);
94
- var b = (0, maths_1.lerp)(value, inputHsb[2], targetHsb[2]);
95
- return (0, colors_1.hsbToRgb)([h_2, s, b]);
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
- var inputHcl = (0, colors_1.rgbToHcl)(inputColor);
99
- var targetHcl = (0, colors_1.rgbToHcl)(targetColor);
100
- var powerValue = Math.pow(value, (_a = settings.powerStrength) !== null && _a !== void 0 ? _a : 1);
101
- var h1 = inputHcl[0];
102
- var c1 = inputHcl[1];
103
- var l1 = inputHcl[2];
104
- var h2 = targetHcl[0] + ((_b = settings.hueOffset) !== null && _b !== void 0 ? _b : 0);
105
- var c2 = targetHcl[1] + ((_c = settings.chromaOffset) !== null && _c !== void 0 ? _c : 0);
106
- var l2 = targetHcl[2] + ((_d = settings.luminanceOffset) !== null && _d !== void 0 ? _d : 0);
107
- var h = void 0, c = void 0, l = void 0;
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 = (0, maths_1.lerp)(value, h1, h2);
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 = (0, maths_1.lerp)(value, h1, h2);
136
- c = settings.triangular ? (0, maths_1.triLerp)(powerValue, c1, c2, settings.triangular) : (0, maths_1.lerp)(powerValue, c1, c2);
137
- l = (0, maths_1.lerp)(powerValue, l1, l2);
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 : (0, maths_1.lerp)(0.5, h1, h2);
150
- c = settings.triangular ? (0, maths_1.triLerp)(powerValue, c1, c2, settings.triangular) : (0, maths_1.lerp)(powerValue, c1, c2);
151
- l = (0, maths_1.lerp)(powerValue, l1, l2);
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 = (0, maths_1.lerp)(value, h1, h2);
155
- c = (0, maths_1.lerp)(value, c1, c2);
156
- l = (0, maths_1.lerp)(value, l1, l2);
135
+ h = lerp(value, h1, h2);
136
+ c = lerp(value, c1, c2);
137
+ l = lerp(value, l1, l2);
157
138
  }
158
- return (0, colors_1.hclToRgb)([h, c, l]);
139
+ return hclToRgb([h, c, l]);
159
140
  }
160
- };
161
- return ColorScale;
162
- }());
163
- exports.default = ColorScale;
141
+ }
142
+ }
@@ -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
  this.fps = fps;
22
20
  }
@@ -25,26 +23,20 @@ var FrameRate = /** @class */ (function () {
25
23
  *
26
24
  * @returns {boolean}
27
25
  */
28
- FrameRate.prototype.update = function () {
29
- this.time = (0, functions_1.now)();
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
- Object.defineProperty(FrameRate.prototype, "fps", {
38
- get: function () {
39
- return this._fps;
40
- },
41
- set: function (fps) {
42
- this._fps = fps;
43
- this.interval = 1000 / fps;
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
+ }