toosoon-utils 4.0.0 → 4.0.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.
@@ -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,27 +1,7 @@
1
1
  // *********************
2
2
  // WIP
3
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 = {
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
- 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);
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
- Pool.prototype.add = function (item) {
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
- Pool.prototype.remove = function (item) {
55
- this.items = this.items.filter(function (_item) { return _item !== item; });
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
- Pool.prototype.get = function () {
63
- var _a, _b;
41
+ get() {
64
42
  if (this.items.length >= this.settings.max)
65
43
  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);
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
- Pool.prototype.release = function (item) {
77
- var _a;
54
+ release(item) {
78
55
  this.pool.push(item);
79
- (_a = item.reset) === null || _a === void 0 ? void 0 : _a.call(item);
56
+ item.reset?.();
80
57
  this.remove(item);
81
- };
58
+ }
82
59
  /**
83
60
  * Dispose all items
84
61
  */
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); });
62
+ dispose() {
63
+ [...this.items, ...this.pool].forEach((item) => item.dispose?.());
87
64
  this.items = [];
88
65
  this.pool = [];
89
- };
90
- return Pool;
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 var defaultSettings = {
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
- var ColorScale = /** @class */ (function () {
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
- 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 = [];
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
- 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);
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
- ColorScale.interpolate = function (inputColor, targetColor, value, settings) {
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
- 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]);
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
- var inputHsl = rgbToHsl(inputColor);
80
- var targetHsl = rgbToHsl(targetColor);
81
- var h1_1 = inputHsl[0];
82
- var s1 = inputHsl[1];
83
- var l1_1 = inputHsl[2];
84
- var h2_1 = targetHsl[0] + ((_a = settings.hueOffset) !== null && _a !== void 0 ? _a : 0);
85
- var s2 = targetHsl[1] + ((_b = settings.saturationOffset) !== null && _b !== void 0 ? _b : 0);
86
- var l2_1 = targetHsl[2] + ((_c = settings.lightnessOffset) !== null && _c !== void 0 ? _c : 0);
87
- var h_1 = lerp(value, h1_1, h2_1);
88
- var s = lerp(value, s1, s2);
89
- var l_1 = lerp(value, l1_1, l2_1);
90
- return 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]);
91
75
  }
92
76
  case 'hsb': {
93
- var inputHsb = rgbToHsb(inputColor);
94
- var targetHsb = rgbToHsb(targetColor);
95
- var h1_2 = inputHsb[0];
96
- var s1 = inputHsb[1];
97
- var b1 = inputHsb[2];
98
- var h2_2 = targetHsb[0] + ((_d = settings.hueOffset) !== null && _d !== void 0 ? _d : 0);
99
- var s2 = targetHsb[1] + ((_e = settings.saturationOffset) !== null && _e !== void 0 ? _e : 0);
100
- var b2 = targetHsb[2] + ((_f = settings.brightnessOffset) !== null && _f !== void 0 ? _f : 0);
101
- var h_2 = lerp(value, h1_2, h2_2);
102
- var s = lerp(value, s1, s2);
103
- var b = lerp(value, b1, b2);
104
- return 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]);
105
89
  }
106
90
  case 'hcl':
107
- var inputHcl = rgbToHcl(inputColor);
108
- var targetHcl = rgbToHcl(targetColor);
109
- var powerValue = Math.pow(value, (_g = settings.powerStrength) !== null && _g !== void 0 ? _g : 1);
110
- var h1 = inputHcl[0];
111
- var c1 = inputHcl[1];
112
- var l1 = inputHcl[2];
113
- var h2 = targetHcl[0] + ((_h = settings.hueOffset) !== null && _h !== void 0 ? _h : 0);
114
- var c2 = targetHcl[1] + ((_j = settings.chromaOffset) !== null && _j !== void 0 ? _j : 0);
115
- var l2 = targetHcl[2] + ((_k = settings.luminanceOffset) !== null && _k !== void 0 ? _k : 0);
116
- 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;
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
- return ColorScale;
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
- var FrameRate = /** @class */ (function () {
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
- 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;
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
- FrameRate.prototype.update = function () {
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
- Object.defineProperty(FrameRate.prototype, "fps", {
35
- get: function () {
36
- return this._fps;
37
- },
38
- set: function (fps) {
39
- this._fps = fps;
40
- this._interval = 1000 / fps;
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
+ }