toosoon-utils 2.4.2 → 3.0.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.
@@ -1,7 +1,30 @@
1
+ "use strict";
1
2
  // *********************
2
3
  // WIP
3
4
  // *********************
4
- export const defaultSettings = {
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 = {
5
28
  max: Infinity
6
29
  };
7
30
  /**
@@ -10,11 +33,12 @@ export const defaultSettings = {
10
33
  * @exports
11
34
  * @class Pool
12
35
  */
13
- export default class Pool {
14
- items = [];
15
- pool = [];
16
- settings = { ...defaultSettings };
17
- constructor(settings = { ...defaultSettings }) {
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);
18
42
  this.settings = Object.assign(this.settings, settings);
19
43
  }
20
44
  /**
@@ -22,46 +46,50 @@ export default class Pool {
22
46
  *
23
47
  * @param {PoolItem} item Item to add to the active items
24
48
  */
25
- add(item) {
49
+ Pool.prototype.add = function (item) {
26
50
  this.items.push(item);
27
- }
51
+ };
28
52
  /**
29
53
  * Remove an item from the active items
30
54
  *
31
55
  * @param {PoolItem} item Item to remove from the active items
32
56
  */
33
- remove(item) {
34
- this.items = this.items.filter((_item) => _item !== item);
35
- }
57
+ Pool.prototype.remove = function (item) {
58
+ this.items = this.items.filter(function (_item) { return _item !== item; });
59
+ };
36
60
  /**
37
61
  * Return an item from pool or create a new one
38
62
  *
39
63
  * @returns {PoolItem|undefined}
40
64
  */
41
- get() {
65
+ Pool.prototype.get = function () {
66
+ var _a, _b;
42
67
  if (this.items.length >= this.settings.max)
43
68
  return;
44
- const item = this.pool.pop() ?? this.create();
45
- item.setup?.();
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);
46
71
  this.add(item);
47
72
  return item;
48
- }
73
+ };
49
74
  /**
50
75
  * Release an item from the active items and add it to the pool
51
76
  *
52
77
  * @param {PoolItem} item Item to release
53
78
  */
54
- release(item) {
79
+ Pool.prototype.release = function (item) {
80
+ var _a;
55
81
  this.pool.push(item);
56
- item.reset?.();
82
+ (_a = item.reset) === null || _a === void 0 ? void 0 : _a.call(item);
57
83
  this.remove(item);
58
- }
84
+ };
59
85
  /**
60
86
  * Dispose all items
61
87
  */
62
- dispose() {
63
- [...this.items, ...this.pool].forEach((item) => item.dispose?.());
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); });
64
90
  this.items = [];
65
91
  this.pool = [];
66
- }
67
- }
92
+ };
93
+ return Pool;
94
+ }());
95
+ exports.default = Pool;
@@ -1,6 +1,20 @@
1
- import { lerp, triLerp } from '../maths';
2
- import { hclToRgb, hsbToRgb, hslToRgb, normalizeColor, rgbToHcl, rgbToHsb, rgbToHsl } from '../colors';
3
- export const defaultSettings = {
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 = {
4
18
  colorSpace: 'rgb'
5
19
  };
6
20
  /**
@@ -9,18 +23,20 @@ export const defaultSettings = {
9
23
  * @exports
10
24
  * @class ColorScale
11
25
  */
12
- export default class ColorScale {
13
- /**
14
- * Array of colors composing the color scale
15
- */
16
- colors = [];
26
+ var ColorScale = /** @class */ (function () {
17
27
  /**
18
28
  * @param {ColorRepresentation} input Input color representation
19
29
  * @param {ColorRepresentation} target Target color representation
20
30
  * @param {number} [length=5] Amount of colors composing the color scale
21
31
  * @param {ColorScaleSettings} [settings] Color scale generation settings
22
32
  */
23
- constructor(input, target, length = 5, settings = { ...defaultSettings }) {
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 = [];
24
40
  this.colors = ColorScale.generate(input, target, length, settings);
25
41
  }
26
42
  /**
@@ -32,16 +48,17 @@ export default class ColorScale {
32
48
  * @param {ColorScaleSettings} [settings] Color scale generation settings
33
49
  * @returns {Array<[number, number, number]>} Color scale colors
34
50
  */
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);
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);
41
58
  colors.push(ColorScale.interpolate(inputColor, targetColor, value, settings));
42
59
  }
43
60
  return colors;
44
- }
61
+ };
45
62
  /**
46
63
  * Static method for interpolating between colors
47
64
  *
@@ -51,41 +68,43 @@ export default class ColorScale {
51
68
  * @param {ColorScaleSettings} [settings] Color scale settings
52
69
  * @returns {[number,number,number]} Interpolated color
53
70
  */
54
- static interpolate(inputColor, targetColor, value, settings = { ...defaultSettings }) {
71
+ ColorScale.interpolate = function (inputColor, targetColor, value, settings) {
72
+ var _a, _b, _c, _d;
73
+ if (settings === void 0) { settings = __assign({}, exports.defaultSettings); }
55
74
  switch (settings.colorSpace) {
56
75
  case 'rgb': {
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
+ 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]);
60
79
  return [r, g, b];
61
80
  }
62
81
  case 'hsl': {
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]);
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]);
69
88
  }
70
89
  case 'hsb': {
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]);
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]);
77
96
  }
78
97
  case 'hcl':
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;
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;
89
108
  // HCL color palettes
90
109
  // -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html
91
110
  if (settings.mode === 'qualitative') {
@@ -99,7 +118,7 @@ export default class ColorScale {
99
118
  * - Chroma: Constant
100
119
  * - Luminance: Constant
101
120
  */
102
- h = lerp(value, h1, h2);
121
+ h = (0, maths_1.lerp)(value, h1, h2);
103
122
  c = c1;
104
123
  l = l1;
105
124
  }
@@ -113,9 +132,9 @@ export default class ColorScale {
113
132
  * - Chroma: Linear (+power) | Triangular (+power)
114
133
  * - Luminance: Linear (+power)
115
134
  */
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);
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);
119
138
  }
120
139
  else if (settings.mode === 'diverging') {
121
140
  /**
@@ -127,16 +146,18 @@ export default class ColorScale {
127
146
  * - Chroma: Linear (+power) | Triangular (+power)
128
147
  * - Luminance: Linear (+power)
129
148
  */
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);
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);
133
152
  }
134
153
  else {
135
- h = lerp(value, h1, h2);
136
- c = lerp(value, c1, c2);
137
- l = lerp(value, l1, l2);
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);
138
157
  }
139
- return hclToRgb([h, c, l]);
158
+ return (0, colors_1.hclToRgb)([h, c, l]);
140
159
  }
141
- }
142
- }
160
+ };
161
+ return ColorScale;
162
+ }());
163
+ exports.default = ColorScale;
@@ -1,20 +1,22 @@
1
- import { now } from '../functions';
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var functions_1 = require("../functions");
2
4
  /**
3
5
  * Utility class for controlling FPS calls
4
6
  *
5
7
  * @exports
6
8
  * @class FrameRate
7
9
  */
8
- export default class FrameRate {
9
- _fps;
10
- interval = 0;
11
- time = 0;
12
- elapsedTime = 0;
13
- lastUpdate = 0;
10
+ var FrameRate = /** @class */ (function () {
14
11
  /**
15
12
  * @param {number} [fps=30] Frame per second limit
16
13
  */
17
- constructor(fps = 30) {
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;
18
20
  this._fps = fps;
19
21
  this.fps = fps;
20
22
  }
@@ -23,20 +25,26 @@ export default class FrameRate {
23
25
  *
24
26
  * @returns {boolean}
25
27
  */
26
- update() {
27
- this.time = now();
28
+ FrameRate.prototype.update = function () {
29
+ this.time = (0, functions_1.now)();
28
30
  this.elapsedTime = this.time - this.lastUpdate;
29
31
  if (this.elapsedTime < this.interval) {
30
32
  return false;
31
33
  }
32
34
  this.lastUpdate = this.time - (this.elapsedTime % this.interval);
33
35
  return true;
34
- }
35
- get fps() {
36
- return this._fps;
37
- }
38
- set fps(fps) {
39
- this._fps = fps;
40
- this.interval = 1000 / fps;
41
- }
42
- }
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;