toosoon-utils 2.4.0 → 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.
- package/lib/classes/_pool.js +22 -47
- package/lib/classes/color-scale.js +42 -60
- package/lib/classes/frame-rate.js +17 -23
- package/lib/colors.js +70 -86
- package/lib/constants.js +6 -6
- package/lib/dom.js +7 -8
- package/lib/files.js +7 -9
- package/lib/functions.js +9 -10
- package/lib/geometry.js +8 -9
- package/lib/maths.js +8 -15
- package/lib/prng.js +24 -30
- package/lib/random.js +19 -33
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/tsconfig.json +2 -3
package/lib/classes/_pool.js
CHANGED
|
@@ -1,27 +1,7 @@
|
|
|
1
1
|
// *********************
|
|
2
2
|
// WIP
|
|
3
3
|
// *********************
|
|
4
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
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
|
-
|
|
55
|
-
this.items = this.items.filter(
|
|
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
|
-
|
|
63
|
-
var _a, _b;
|
|
41
|
+
get() {
|
|
64
42
|
if (this.items.length >= this.settings.max)
|
|
65
43
|
return;
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
|
|
77
|
-
var _a;
|
|
54
|
+
release(item) {
|
|
78
55
|
this.pool.push(item);
|
|
79
|
-
|
|
56
|
+
item.reset?.();
|
|
80
57
|
this.remove(item);
|
|
81
|
-
}
|
|
58
|
+
}
|
|
82
59
|
/**
|
|
83
60
|
* Dispose all items
|
|
84
61
|
*/
|
|
85
|
-
|
|
86
|
-
|
|
62
|
+
dispose() {
|
|
63
|
+
[...this.items, ...this.pool].forEach((item) => item.dispose?.());
|
|
87
64
|
this.items = [];
|
|
88
65
|
this.pool = [];
|
|
89
|
-
}
|
|
90
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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,43 +51,41 @@ var ColorScale = /** @class */ (function () {
|
|
|
65
51
|
* @param {ColorScaleSettings} [settings] Color scale settings
|
|
66
52
|
* @returns {[number,number,number]} Interpolated color
|
|
67
53
|
*/
|
|
68
|
-
|
|
69
|
-
var _a, _b, _c, _d;
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return hslToRgb([
|
|
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]);
|
|
85
69
|
}
|
|
86
70
|
case 'hsb': {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return hsbToRgb([
|
|
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]);
|
|
93
77
|
}
|
|
94
78
|
case 'hcl':
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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;
|
|
105
89
|
// HCL color palettes
|
|
106
90
|
// -> https://colorspace.r-forge.r-project.org/articles/hcl_palettes.html
|
|
107
91
|
if (settings.mode === 'qualitative') {
|
|
@@ -154,7 +138,5 @@ var ColorScale = /** @class */ (function () {
|
|
|
154
138
|
}
|
|
155
139
|
return hclToRgb([h, c, l]);
|
|
156
140
|
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
}());
|
|
160
|
-
export default ColorScale;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -5,16 +5,16 @@ import { now } from '../functions';
|
|
|
5
5
|
* @exports
|
|
6
6
|
* @class FrameRate
|
|
7
7
|
*/
|
|
8
|
-
|
|
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
|
-
|
|
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
|
this.fps = fps;
|
|
20
20
|
}
|
|
@@ -23,7 +23,7 @@ var FrameRate = /** @class */ (function () {
|
|
|
23
23
|
*
|
|
24
24
|
* @returns {boolean}
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
update() {
|
|
27
27
|
this.time = now();
|
|
28
28
|
this.elapsedTime = this.time - this.lastUpdate;
|
|
29
29
|
if (this.elapsedTime < this.interval) {
|
|
@@ -31,18 +31,12 @@ var FrameRate = /** @class */ (function () {
|
|
|
31
31
|
}
|
|
32
32
|
this.lastUpdate = this.time - (this.elapsedTime % this.interval);
|
|
33
33
|
return true;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
enumerable: false,
|
|
44
|
-
configurable: true
|
|
45
|
-
});
|
|
46
|
-
return FrameRate;
|
|
47
|
-
}());
|
|
48
|
-
export 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
|
+
}
|
package/lib/colors.js
CHANGED
|
@@ -8,9 +8,8 @@ import { clamp } from './maths';
|
|
|
8
8
|
* @returns {[number,number,number]} Normalized RGB color
|
|
9
9
|
*/
|
|
10
10
|
export function normalizeColor(color) {
|
|
11
|
-
var _a;
|
|
12
11
|
if (typeof color === 'string') {
|
|
13
|
-
return hexToRgb(
|
|
12
|
+
return hexToRgb(W3CX11[color] ?? color);
|
|
14
13
|
}
|
|
15
14
|
else if (typeof color === 'number') {
|
|
16
15
|
return hexToRgb(color);
|
|
@@ -29,8 +28,8 @@ export function normalizeColor(color) {
|
|
|
29
28
|
* @returns {string} Normalized hexadecimal string
|
|
30
29
|
*/
|
|
31
30
|
export function normalizeHexString(hex) {
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
let match;
|
|
32
|
+
let result = '000000';
|
|
34
33
|
hex = hex.toLocaleLowerCase();
|
|
35
34
|
if ((match = hex.match(/(#|0x)?([a-f0-9]{6})/i))) {
|
|
36
35
|
result = match[2];
|
|
@@ -44,7 +43,7 @@ export function normalizeHexString(hex) {
|
|
|
44
43
|
parseInt(match[2]).toString(16).padStart(2, '0') +
|
|
45
44
|
parseInt(match[3]).toString(16).padStart(2, '0');
|
|
46
45
|
}
|
|
47
|
-
return
|
|
46
|
+
return `#${result}`;
|
|
48
47
|
}
|
|
49
48
|
/**
|
|
50
49
|
* Convert RGB to hexadecimal
|
|
@@ -53,8 +52,7 @@ export function normalizeHexString(hex) {
|
|
|
53
52
|
* @param {[number, number, number]} rgb RGB color
|
|
54
53
|
* @returns {number} Hexadecimal color
|
|
55
54
|
*/
|
|
56
|
-
export function rgbToHex(
|
|
57
|
-
var r = _a[0], g = _a[1], b = _a[2];
|
|
55
|
+
export function rgbToHex([r, g, b]) {
|
|
58
56
|
return ((r * 255) << 16) ^ ((g * 255) << 8) ^ ((b * 255) << 0);
|
|
59
57
|
}
|
|
60
58
|
/**
|
|
@@ -64,13 +62,12 @@ export function rgbToHex(_a) {
|
|
|
64
62
|
* @param {[number, number, number]} rgb RGB color
|
|
65
63
|
* @returns {string} Hexadecimal string
|
|
66
64
|
*/
|
|
67
|
-
export function rgbToHexString(
|
|
68
|
-
var r = _a[0], g = _a[1], b = _a[2];
|
|
65
|
+
export function rgbToHexString([r, g, b]) {
|
|
69
66
|
r = clamp(Math.round(r * 255), 0, 255);
|
|
70
67
|
g = clamp(Math.round(g * 255), 0, 255);
|
|
71
68
|
b = clamp(Math.round(b * 255), 0, 255);
|
|
72
|
-
|
|
73
|
-
return
|
|
69
|
+
const result = (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1);
|
|
70
|
+
return `#${result}`;
|
|
74
71
|
}
|
|
75
72
|
/**
|
|
76
73
|
* Convert hexadecimal to RGB
|
|
@@ -87,9 +84,9 @@ export function hexToRgb(hex) {
|
|
|
87
84
|
hex = normalizeHexString(hex).replace(/^#/, '');
|
|
88
85
|
hex = parseInt(hex, 16);
|
|
89
86
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
const r = ((hex >> 16) & 255) / 255;
|
|
88
|
+
const g = ((hex >> 8) & 255) / 255;
|
|
89
|
+
const b = (hex & 255) / 255;
|
|
93
90
|
return [r, g, b];
|
|
94
91
|
}
|
|
95
92
|
/**
|
|
@@ -99,18 +96,17 @@ export function hexToRgb(hex) {
|
|
|
99
96
|
* @param {number} [amount=0] Amount of the color offset
|
|
100
97
|
* @returns {string} Computed hexadecimal
|
|
101
98
|
*/
|
|
102
|
-
export function lighten(hex, amount) {
|
|
103
|
-
|
|
104
|
-
var prefix = '';
|
|
99
|
+
export function lighten(hex, amount = 0) {
|
|
100
|
+
let prefix = '';
|
|
105
101
|
if (hex[0] === '#') {
|
|
106
102
|
hex = hex.slice(1);
|
|
107
103
|
prefix = '#';
|
|
108
104
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
105
|
+
const value = parseInt(hex, 16);
|
|
106
|
+
const r = clamp((value >> 16) + amount, 0, 255);
|
|
107
|
+
const b = clamp(((value >> 8) & 0x00ff) + amount, 0, 255);
|
|
108
|
+
const g = clamp((value & 0x0000ff) + amount, 0, 255);
|
|
109
|
+
let result = g | (b << 8) | (r << 16);
|
|
114
110
|
if (r === 0 && g === 0 && b === 0 && amount !== 0) {
|
|
115
111
|
result = '000000';
|
|
116
112
|
}
|
|
@@ -123,8 +119,7 @@ export function lighten(hex, amount) {
|
|
|
123
119
|
* @param {number} [amount=0] Amount of the color offset
|
|
124
120
|
* @returns {string} Computed hexadecimal
|
|
125
121
|
*/
|
|
126
|
-
export function darken(hex, amount) {
|
|
127
|
-
if (amount === void 0) { amount = 0; }
|
|
122
|
+
export function darken(hex, amount = 0) {
|
|
128
123
|
return lighten(hex, -amount);
|
|
129
124
|
}
|
|
130
125
|
// ***************************************************
|
|
@@ -138,8 +133,7 @@ export function darken(hex, amount) {
|
|
|
138
133
|
* @returns {[number, number, number]} Normalized HSL color
|
|
139
134
|
*/
|
|
140
135
|
export function normalizeHslString(hsl) {
|
|
141
|
-
|
|
142
|
-
var _c = (_b = (_a = hsl.match(/\d+/g)) === null || _a === void 0 ? void 0 : _a.map(Number)) !== null && _b !== void 0 ? _b : [0, 0, 0], h = _c[0], s = _c[1], l = _c[2];
|
|
136
|
+
const [h, s, l] = hsl.match(/\d+/g)?.map(Number) ?? [0, 0, 0];
|
|
143
137
|
return [h, s / 100, l / 100];
|
|
144
138
|
}
|
|
145
139
|
/**
|
|
@@ -151,11 +145,10 @@ export function normalizeHslString(hsl) {
|
|
|
151
145
|
* @param {[number, number, number]} rgb RGB color
|
|
152
146
|
* @returns {[number, number, number]} HSL color
|
|
153
147
|
*/
|
|
154
|
-
export function rgbToHsl(
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
var h = s ? (l === r ? (g - b) / s : l === g ? 2 + (b - r) / s : 4 + (r - g) / s) : 0;
|
|
148
|
+
export function rgbToHsl([r, g, b]) {
|
|
149
|
+
const l = Math.max(r, g, b);
|
|
150
|
+
const s = l - Math.min(r, g, b);
|
|
151
|
+
const h = s ? (l === r ? (g - b) / s : l === g ? 2 + (b - r) / s : 4 + (r - g) / s) : 0;
|
|
159
152
|
return [
|
|
160
153
|
60 * h < 0 ? 60 * h + 360 : 60 * h,
|
|
161
154
|
s ? (l <= 0.5 ? s / (2 * l - s) : s / (2 - (2 * l - s))) : 0,
|
|
@@ -171,11 +164,10 @@ export function rgbToHsl(_a) {
|
|
|
171
164
|
* @param {[number, number, number]} hsl HSL color
|
|
172
165
|
* @returns {[number, number, number]} RGB color
|
|
173
166
|
*/
|
|
174
|
-
export function hslToRgb(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
var f = function (v) { return l - a * Math.max(-1, Math.min(k(v) - 3, Math.min(9 - k(v), 1))); };
|
|
167
|
+
export function hslToRgb([h, s, l]) {
|
|
168
|
+
const a = s * Math.min(l, 1 - l);
|
|
169
|
+
const k = (v) => (v + h / 30) % 12;
|
|
170
|
+
const f = (v) => l - a * Math.max(-1, Math.min(k(v) - 3, Math.min(9 - k(v), 1)));
|
|
179
171
|
return [f(0), f(8), f(4)];
|
|
180
172
|
}
|
|
181
173
|
// ***************************************************
|
|
@@ -190,12 +182,11 @@ export function hslToRgb(_a) {
|
|
|
190
182
|
* @param {[number, number, number]} rgb RGB color
|
|
191
183
|
* @returns {[number, number, number]} HSB color
|
|
192
184
|
*/
|
|
193
|
-
export function rgbToHsb(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
var h = delta === 0 ? 0 : delta && max === r ? (g - b) / delta : max === g ? 2 + (b - r) / delta : 4 + (r - g) / delta;
|
|
185
|
+
export function rgbToHsb([r, g, b]) {
|
|
186
|
+
const max = Math.max(r, g, b);
|
|
187
|
+
const min = Math.min(r, g, b);
|
|
188
|
+
const delta = max - min;
|
|
189
|
+
const h = delta === 0 ? 0 : delta && max === r ? (g - b) / delta : max === g ? 2 + (b - r) / delta : 4 + (r - g) / delta;
|
|
199
190
|
return [60 * (h < 0 ? h + 6 : h), max && delta / max, max];
|
|
200
191
|
}
|
|
201
192
|
/**
|
|
@@ -207,10 +198,9 @@ export function rgbToHsb(_a) {
|
|
|
207
198
|
* @param {[number, number, number]} hsb HSB color
|
|
208
199
|
* @returns {[number, number, number]} RGB color
|
|
209
200
|
*/
|
|
210
|
-
export function hsbToRgb(
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
var f = function (v) { return b * (1 - s * Math.max(0, Math.min(k(v), 4 - k(v), 1))); };
|
|
201
|
+
export function hsbToRgb([h, s, b]) {
|
|
202
|
+
const k = (v) => (v + h / 60) % 6;
|
|
203
|
+
const f = (v) => b * (1 - s * Math.max(0, Math.min(k(v), 4 - k(v), 1)));
|
|
214
204
|
return [f(5), f(3), f(1)];
|
|
215
205
|
}
|
|
216
206
|
// *********************************************
|
|
@@ -223,10 +213,9 @@ export function hsbToRgb(_a) {
|
|
|
223
213
|
* @param {[number, number, number]} lab LAB color
|
|
224
214
|
* @returns {[number, number, number]} HCL color
|
|
225
215
|
*/
|
|
226
|
-
export function labToHcl(
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
var h = abToHue(a, b);
|
|
216
|
+
export function labToHcl([l, a, b]) {
|
|
217
|
+
const c = Math.sqrt(a * a + b * b);
|
|
218
|
+
const h = abToHue(a, b);
|
|
230
219
|
return [h, c, l];
|
|
231
220
|
}
|
|
232
221
|
/**
|
|
@@ -236,10 +225,9 @@ export function labToHcl(_a) {
|
|
|
236
225
|
* @param {[number, number, number]} hcl HCL color
|
|
237
226
|
* @returns {[number, number, number]} LAB color space
|
|
238
227
|
*/
|
|
239
|
-
export function hclToLab(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
var b = c * Math.sin(toRadians(h));
|
|
228
|
+
export function hclToLab([h, c, l]) {
|
|
229
|
+
const a = c * Math.cos(toRadians(h));
|
|
230
|
+
const b = c * Math.sin(toRadians(h));
|
|
243
231
|
return [l, a, b];
|
|
244
232
|
}
|
|
245
233
|
/**
|
|
@@ -263,7 +251,7 @@ function abToHue(a, b) {
|
|
|
263
251
|
if (a === 0 && b < 0) {
|
|
264
252
|
return 270;
|
|
265
253
|
}
|
|
266
|
-
|
|
254
|
+
let xBias = 0;
|
|
267
255
|
if (a > 0 && b > 0) {
|
|
268
256
|
xBias = 0;
|
|
269
257
|
}
|
|
@@ -278,21 +266,20 @@ function abToHue(a, b) {
|
|
|
278
266
|
// ******************************************
|
|
279
267
|
// LAB & RGB color spaces
|
|
280
268
|
// ******************************************
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
269
|
+
const f1 = (v) => (v * v * v > 0.008856 ? v * v * v : (v - 16 / 116) / 7.787);
|
|
270
|
+
const f2 = (v) => (v > 0.0031308 ? 1.055 * Math.pow(v, 1 / 2.4) - 0.055 : 12.92 * v);
|
|
271
|
+
const f3 = (v) => (v > 0.04045 ? Math.pow((v + 0.055) / 1.055, 2.4) : v / 12.92);
|
|
272
|
+
const f4 = (v) => (v > 0.008856 ? Math.pow(v, 1 / 3) : 7.787 * v + 16 / 116);
|
|
285
273
|
/**
|
|
286
274
|
* Converts LAB to RGB
|
|
287
275
|
*
|
|
288
276
|
* @param {[number, number, number]} lab LAB color
|
|
289
277
|
* @returns {[number, number, number]} RGB color
|
|
290
278
|
*/
|
|
291
|
-
export function labToRgb(
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
var z = y - b / 200;
|
|
279
|
+
export function labToRgb([l, a, b]) {
|
|
280
|
+
let y = (l + 16) / 116;
|
|
281
|
+
let x = a / 500 + y;
|
|
282
|
+
let z = y - b / 200;
|
|
296
283
|
x = 0.95047 * f1(x);
|
|
297
284
|
y = 1.0 * f1(y);
|
|
298
285
|
z = 1.08883 * f1(z);
|
|
@@ -308,14 +295,13 @@ export function labToRgb(_a) {
|
|
|
308
295
|
* @param {[number, number, number]} rgb RGB color
|
|
309
296
|
* @returns {[number, number, number]} LAB color
|
|
310
297
|
*/
|
|
311
|
-
export function rgbToLab(
|
|
312
|
-
var r = _a[0], g = _a[1], b = _a[2];
|
|
298
|
+
export function rgbToLab([r, g, b]) {
|
|
313
299
|
r = f3(r);
|
|
314
300
|
g = f3(g);
|
|
315
301
|
b = f3(b);
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
302
|
+
let x = f4((r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047);
|
|
303
|
+
let y = f4((r * 0.2126 + g * 0.7152 + b * 0.0722) / 1);
|
|
304
|
+
let z = f4((r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883);
|
|
319
305
|
return [116 * y - 16, 500 * (x - y), 200 * (y - z)];
|
|
320
306
|
}
|
|
321
307
|
/**
|
|
@@ -326,20 +312,20 @@ export function rgbToLab(_a) {
|
|
|
326
312
|
* @returns {number} Delta
|
|
327
313
|
*/
|
|
328
314
|
export function deltaE(labA, labB) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
315
|
+
const deltaL = labA[0] - labB[0];
|
|
316
|
+
const deltaA = labA[1] - labB[1];
|
|
317
|
+
const deltaB = labA[2] - labB[2];
|
|
318
|
+
const c1 = Math.sqrt(labA[1] * labA[1] + labA[2] * labA[2]);
|
|
319
|
+
const c2 = Math.sqrt(labB[1] * labB[1] + labB[2] * labB[2]);
|
|
320
|
+
const deltaC = c1 - c2;
|
|
321
|
+
let deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC;
|
|
336
322
|
deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH);
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
323
|
+
const sc = 1.0 + 0.045 * c1;
|
|
324
|
+
const sh = 1.0 + 0.015 * c1;
|
|
325
|
+
const deltaLKlsl = deltaL / 1;
|
|
326
|
+
const deltaCkcsc = deltaC / sc;
|
|
327
|
+
const deltaHkhsh = deltaH / sh;
|
|
328
|
+
const i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh;
|
|
343
329
|
return i < 0 ? 0 : Math.sqrt(i);
|
|
344
330
|
}
|
|
345
331
|
// *********************************************
|
|
@@ -351,8 +337,7 @@ export function deltaE(labA, labB) {
|
|
|
351
337
|
* @param {[number, number, number]} rgb RGB color
|
|
352
338
|
* @returns {[number, number, number]} HCL color
|
|
353
339
|
*/
|
|
354
|
-
export function rgbToHcl(
|
|
355
|
-
var r = _a[0], g = _a[1], b = _a[2];
|
|
340
|
+
export function rgbToHcl([r, g, b]) {
|
|
356
341
|
return labToHcl(rgbToLab([r, g, b]));
|
|
357
342
|
}
|
|
358
343
|
/**
|
|
@@ -361,7 +346,6 @@ export function rgbToHcl(_a) {
|
|
|
361
346
|
* @param {[number, number, number]} hcl RGB color
|
|
362
347
|
* @returns {[number, number, number]} RGB color
|
|
363
348
|
*/
|
|
364
|
-
export function hclToRgb(
|
|
365
|
-
var h = _a[0], c = _a[1], l = _a[2];
|
|
349
|
+
export function hclToRgb([h, c, l]) {
|
|
366
350
|
return labToRgb(hclToLab([h, c, l]));
|
|
367
351
|
}
|
package/lib/constants.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
// *********************
|
|
2
2
|
// Maths
|
|
3
3
|
// *********************
|
|
4
|
-
export
|
|
5
|
-
export
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
4
|
+
export const EPSILON = 1e-10;
|
|
5
|
+
export const PI = Math.PI;
|
|
6
|
+
export const TWO_PI = Math.PI * 2;
|
|
7
|
+
export const HALF_PI = Math.PI / 2;
|
|
8
|
+
export const QUARTER_PI = Math.PI / 4;
|
|
9
9
|
// *********************
|
|
10
10
|
// Colors
|
|
11
11
|
// *********************
|
|
12
12
|
// X11 colors
|
|
13
13
|
// -> https://www.w3.org/TR/css-color-3/#svg-color
|
|
14
|
-
export
|
|
14
|
+
export const W3CX11 = {
|
|
15
15
|
aliceblue: 0xf0f8ff,
|
|
16
16
|
antiquewhite: 0xfaebd7,
|
|
17
17
|
aqua: 0x00ffff,
|