@oscarpalmer/atoms 0.67.0 → 0.68.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.
- package/dist/js/colour/base.mjs +17 -0
- package/dist/js/colour/functions.mjs +97 -0
- package/dist/js/colour/hex.mjs +29 -43
- package/dist/js/colour/hsl.mjs +51 -41
- package/dist/js/colour/index.js +190 -100
- package/dist/js/colour/index.mjs +14 -10
- package/dist/js/colour/is.mjs +26 -0
- package/dist/js/colour/rgb.mjs +48 -68
- package/dist/js/emitter.js +100 -85
- package/dist/js/emitter.mjs +97 -85
- package/dist/js/function.js +32 -28
- package/dist/js/function.mjs +32 -28
- package/dist/js/index.js +601 -473
- package/dist/js/index.mjs +1 -1
- package/dist/js/logger.js +59 -52
- package/dist/js/logger.mjs +60 -53
- package/dist/js/timer/constants.mjs +9 -0
- package/dist/js/timer/functions.mjs +93 -0
- package/dist/js/{timer.js → timer/index.js} +191 -176
- package/dist/js/timer/index.mjs +46 -0
- package/dist/js/timer/is.mjs +22 -0
- package/dist/js/timer/repeat.mjs +5 -0
- package/dist/js/timer/timer.mjs +79 -0
- package/dist/js/timer/when.mjs +69 -0
- package/package.json +5 -5
- package/src/js/colour/base.ts +43 -0
- package/src/js/colour/functions.ts +139 -0
- package/src/js/colour/hex.ts +61 -56
- package/src/js/colour/hsl.ts +90 -47
- package/src/js/colour/index.ts +6 -5
- package/src/js/colour/is.ts +57 -0
- package/src/js/colour/rgb.ts +94 -92
- package/src/js/emitter.ts +158 -131
- package/src/js/function.ts +50 -41
- package/src/js/index.ts +1 -1
- package/src/js/logger.ts +88 -93
- package/src/js/timer/constants.ts +16 -0
- package/src/js/timer/functions.ts +151 -0
- package/src/js/timer/index.ts +49 -0
- package/src/js/timer/is.ts +34 -0
- package/src/js/timer/models.ts +76 -0
- package/src/js/timer/repeat.ts +0 -0
- package/src/js/timer/timer.ts +186 -0
- package/src/js/timer/when.ts +111 -0
- package/types/colour/base.d.ts +22 -0
- package/types/colour/functions.d.ts +23 -0
- package/types/colour/hex.d.ts +31 -6
- package/types/colour/hsl.d.ts +47 -5
- package/types/colour/index.d.ts +5 -5
- package/types/colour/is.d.ts +20 -0
- package/types/colour/rgb.d.ts +51 -9
- package/types/emitter.d.ts +23 -12
- package/types/function.d.ts +11 -6
- package/types/index.d.cts +590 -885
- package/types/index.d.ts +1 -1
- package/types/logger.d.ts +59 -21
- package/types/timer/constants.d.ts +13 -0
- package/types/timer/functions.d.ts +5 -0
- package/types/timer/index.d.ts +11 -0
- package/types/timer/is.d.ts +17 -0
- package/types/timer/models.d.ts +62 -0
- package/types/timer/repeat.d.ts +1 -0
- package/types/timer/timer.d.ts +72 -0
- package/types/timer/when.d.ts +28 -0
- package/dist/js/timer.mjs +0 -272
- package/src/js/colour/models.ts +0 -83
- package/src/js/timer.ts +0 -569
- package/types/colour/models.d.ts +0 -79
- package/types/timer.d.ts +0 -147
- /package/dist/js/{colour → timer}/models.mjs +0 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/js/colour/base.ts
|
|
2
|
+
import {isColourValue} from "./is";
|
|
3
|
+
|
|
4
|
+
class Colour {
|
|
5
|
+
get value() {
|
|
6
|
+
return { ...this.state.value };
|
|
7
|
+
}
|
|
8
|
+
constructor(type, value, defaults, properties) {
|
|
9
|
+
this.$colour = type;
|
|
10
|
+
this.state = {
|
|
11
|
+
value: isColourValue(value, properties) ? { ...value } : { ...defaults }
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
Colour
|
|
17
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/js/colour/functions.ts
|
|
2
|
+
import {clamp} from "../number";
|
|
3
|
+
import {HexColour} from "./hex";
|
|
4
|
+
import {HSLColour} from "./hsl";
|
|
5
|
+
import {RGBColour} from "./rgb";
|
|
6
|
+
function getNormalisedHex(value) {
|
|
7
|
+
const normalised = value.replace(/^#/, "");
|
|
8
|
+
return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
|
|
9
|
+
}
|
|
10
|
+
function hexToRgb(value) {
|
|
11
|
+
const hex2 = anyPattern.test(value) ? getNormalisedHex(value) : "";
|
|
12
|
+
const pairs = groupedPattern.exec(hex2) ?? [];
|
|
13
|
+
const rgb2 = [];
|
|
14
|
+
const { length } = pairs;
|
|
15
|
+
for (let index = 1;index < length; index += 1) {
|
|
16
|
+
rgb2.push(Number.parseInt(pairs[index], 16));
|
|
17
|
+
}
|
|
18
|
+
return new RGBColour({
|
|
19
|
+
blue: rgb2[2] ?? 0,
|
|
20
|
+
green: rgb2[1] ?? 0,
|
|
21
|
+
red: rgb2[0] ?? 0
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function hslToRgb(value) {
|
|
25
|
+
let hue = value.hue % 360;
|
|
26
|
+
if (hue < 0) {
|
|
27
|
+
hue += 360;
|
|
28
|
+
}
|
|
29
|
+
const saturation = value.saturation / 100;
|
|
30
|
+
const lightness = value.lightness / 100;
|
|
31
|
+
function get(value2) {
|
|
32
|
+
const part = (value2 + hue / 30) % 12;
|
|
33
|
+
const mod = saturation * Math.min(lightness, 1 - lightness);
|
|
34
|
+
return lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1));
|
|
35
|
+
}
|
|
36
|
+
return new RGBColour({
|
|
37
|
+
blue: clamp(Math.round(get(4) * 255), 0, 255),
|
|
38
|
+
green: clamp(Math.round(get(8) * 255), 0, 255),
|
|
39
|
+
red: clamp(Math.round(get(0) * 255), 0, 255)
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function rgbToHex(value) {
|
|
43
|
+
return new HexColour(`${[value.red, value.green, value.blue].map((colour) => {
|
|
44
|
+
const hex2 = colour.toString(16);
|
|
45
|
+
return hex2.length === 1 ? `0${hex2}` : hex2;
|
|
46
|
+
}).join("")}`);
|
|
47
|
+
}
|
|
48
|
+
function rgbToHsl(rgb2) {
|
|
49
|
+
const blue = rgb2.blue / 255;
|
|
50
|
+
const green = rgb2.green / 255;
|
|
51
|
+
const red = rgb2.red / 255;
|
|
52
|
+
const max = Math.max(blue, green, red);
|
|
53
|
+
const min = Math.min(blue, green, red);
|
|
54
|
+
const delta = max - min;
|
|
55
|
+
const lightness = (min + max) / 2;
|
|
56
|
+
let hue = 0;
|
|
57
|
+
let saturation = 0;
|
|
58
|
+
if (delta !== 0) {
|
|
59
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (max - lightness) / Math.min(lightness, 1 - lightness);
|
|
60
|
+
switch (max) {
|
|
61
|
+
case blue:
|
|
62
|
+
hue = (red - green) / delta + 4;
|
|
63
|
+
break;
|
|
64
|
+
case green:
|
|
65
|
+
hue = (blue - red) / delta + 2;
|
|
66
|
+
break;
|
|
67
|
+
case red:
|
|
68
|
+
hue = (green - blue) / delta + (green < blue ? 6 : 0);
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
hue *= 60;
|
|
74
|
+
}
|
|
75
|
+
if (saturation < 0) {
|
|
76
|
+
hue += 180;
|
|
77
|
+
saturation = Math.abs(saturation);
|
|
78
|
+
}
|
|
79
|
+
if (hue >= 360) {
|
|
80
|
+
hue -= 360;
|
|
81
|
+
}
|
|
82
|
+
return new HSLColour({
|
|
83
|
+
hue: +hue.toFixed(2),
|
|
84
|
+
lightness: +(lightness * 100).toFixed(2),
|
|
85
|
+
saturation: +(saturation * 100).toFixed(2)
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
var anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
89
|
+
var groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
90
|
+
export {
|
|
91
|
+
rgbToHsl,
|
|
92
|
+
rgbToHex,
|
|
93
|
+
hslToRgb,
|
|
94
|
+
hexToRgb,
|
|
95
|
+
getNormalisedHex,
|
|
96
|
+
anyPattern
|
|
97
|
+
};
|
package/dist/js/colour/hex.mjs
CHANGED
|
@@ -1,51 +1,37 @@
|
|
|
1
1
|
// src/js/colour/hex.ts
|
|
2
|
-
import {
|
|
3
|
-
function createHex(original) {
|
|
4
|
-
let value = original.slice();
|
|
5
|
-
const instance = Object.create({
|
|
6
|
-
toHsl() {
|
|
7
|
-
return hexToRgb(value).toHsl();
|
|
8
|
-
},
|
|
9
|
-
toRgb() {
|
|
10
|
-
return hexToRgb(value);
|
|
11
|
-
},
|
|
12
|
-
toString() {
|
|
13
|
-
return `#${value}`;
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
Object.defineProperty(instance, "value", {
|
|
17
|
-
get() {
|
|
18
|
-
return `#${value}`;
|
|
19
|
-
},
|
|
20
|
-
set(hex) {
|
|
21
|
-
if (anyPattern.test(hex)) {
|
|
22
|
-
value = getNormalisedHex(hex);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
return instance;
|
|
27
|
-
}
|
|
2
|
+
import {anyPattern, getNormalisedHex, hexToRgb} from "./functions";
|
|
28
3
|
function getHexColour(value) {
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
function getNormalisedHex(value) {
|
|
32
|
-
const normalised = value.replace(/^#/, "");
|
|
33
|
-
return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
|
|
4
|
+
return new HexColour(value);
|
|
34
5
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
6
|
+
|
|
7
|
+
class HexColour {
|
|
8
|
+
state;
|
|
9
|
+
get value() {
|
|
10
|
+
return `#${this.state.value}`;
|
|
11
|
+
}
|
|
12
|
+
set value(value) {
|
|
13
|
+
this.state.value = anyPattern.test(value) ? getNormalisedHex(value) : "000000";
|
|
14
|
+
}
|
|
15
|
+
constructor(value) {
|
|
16
|
+
this.$colour = "hex";
|
|
17
|
+
this.state = {
|
|
18
|
+
value: typeof value === "string" && anyPattern.test(value) ? getNormalisedHex(value) : "000000"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
toHsl() {
|
|
22
|
+
return HexColour.toRgb(this.value).toHsl();
|
|
23
|
+
}
|
|
24
|
+
toRgb() {
|
|
25
|
+
return HexColour.toRgb(this.value);
|
|
26
|
+
}
|
|
27
|
+
toString() {
|
|
28
|
+
return this.value;
|
|
29
|
+
}
|
|
30
|
+
static toRgb(value) {
|
|
31
|
+
return hexToRgb(value);
|
|
42
32
|
}
|
|
43
|
-
return createRgb({ blue: rgb2[2] ?? 0, green: rgb2[1] ?? 0, red: rgb2[0] ?? 0 });
|
|
44
33
|
}
|
|
45
|
-
var anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
46
|
-
var groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
47
34
|
export {
|
|
48
|
-
hexToRgb,
|
|
49
35
|
getHexColour,
|
|
50
|
-
|
|
36
|
+
HexColour
|
|
51
37
|
};
|
package/dist/js/colour/hsl.mjs
CHANGED
|
@@ -1,47 +1,57 @@
|
|
|
1
1
|
// src/js/colour/hsl.ts
|
|
2
|
-
import {createProperty} from "../internal/colour-property";
|
|
3
2
|
import {clamp} from "../number";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
toHex() {
|
|
9
|
-
return hslToRgb(value).toHex();
|
|
10
|
-
},
|
|
11
|
-
toRgb() {
|
|
12
|
-
return hslToRgb(value);
|
|
13
|
-
},
|
|
14
|
-
toString() {
|
|
15
|
-
return `hsl(${value.hue}, ${value.saturation}%, ${value.lightness}%)`;
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
Object.defineProperties(instance, {
|
|
19
|
-
hue: createProperty(value, "hue", 0, 360),
|
|
20
|
-
lightness: createProperty(value, "lightness", 0, 100),
|
|
21
|
-
saturation: createProperty(value, "saturation", 0, 100),
|
|
22
|
-
value: { value }
|
|
23
|
-
});
|
|
24
|
-
return instance;
|
|
3
|
+
import {Colour} from "./base";
|
|
4
|
+
import {hslToRgb} from "./functions";
|
|
5
|
+
function getHSLColour(value) {
|
|
6
|
+
return new HSLColour(value);
|
|
25
7
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
hue
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
8
|
+
|
|
9
|
+
class HSLColour extends Colour {
|
|
10
|
+
get hue() {
|
|
11
|
+
return +this.state.value.hue;
|
|
12
|
+
}
|
|
13
|
+
set hue(value) {
|
|
14
|
+
this.state.value.hue = clamp(value, 0, 360);
|
|
15
|
+
}
|
|
16
|
+
get lightness() {
|
|
17
|
+
return +this.state.value.lightness;
|
|
18
|
+
}
|
|
19
|
+
set lightness(value) {
|
|
20
|
+
this.state.value.lightness = clamp(value, 0, 100);
|
|
21
|
+
}
|
|
22
|
+
get saturation() {
|
|
23
|
+
return +this.state.value.saturation;
|
|
24
|
+
}
|
|
25
|
+
set saturation(value) {
|
|
26
|
+
this.state.value.saturation = clamp(value, 0, 100);
|
|
27
|
+
}
|
|
28
|
+
constructor(value) {
|
|
29
|
+
super("hsl", value, defaults, properties);
|
|
30
|
+
}
|
|
31
|
+
toHex() {
|
|
32
|
+
return HSLColour.toRgb(this.state.value).toHex();
|
|
33
|
+
}
|
|
34
|
+
toRgb() {
|
|
35
|
+
return HSLColour.toRgb(this.state.value);
|
|
36
|
+
}
|
|
37
|
+
toString() {
|
|
38
|
+
return `hsl(${this.state.value.hue}, ${this.state.value.saturation}%, ${this.state.value.lightness}%)`;
|
|
39
|
+
}
|
|
40
|
+
static toRgb(value) {
|
|
41
|
+
return hslToRgb(value);
|
|
42
|
+
}
|
|
43
43
|
}
|
|
44
|
+
var defaults = {
|
|
45
|
+
hue: 0,
|
|
46
|
+
lightness: 0,
|
|
47
|
+
saturation: 0
|
|
48
|
+
};
|
|
49
|
+
var properties = [
|
|
50
|
+
"hue",
|
|
51
|
+
"lightness",
|
|
52
|
+
"saturation"
|
|
53
|
+
];
|
|
44
54
|
export {
|
|
45
|
-
|
|
46
|
-
|
|
55
|
+
getHSLColour,
|
|
56
|
+
HSLColour
|
|
47
57
|
};
|
package/dist/js/colour/index.js
CHANGED
|
@@ -20,39 +20,158 @@ function clamp(value, min, max, loop) {
|
|
|
20
20
|
return value > max ? loop === true ? min : max : value;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
// src/js/
|
|
24
|
-
function
|
|
25
|
-
return
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
// src/js/colour/is.ts
|
|
24
|
+
function isColour(value) {
|
|
25
|
+
return isInstance(/^(hex|hsl|rgb)$/, value);
|
|
26
|
+
}
|
|
27
|
+
function isColourValue(value, properties) {
|
|
28
|
+
return typeof value === "object" && value !== null && properties.every((property) => (property in value) && typeof value[property] === "number");
|
|
29
|
+
}
|
|
30
|
+
function isHexColour(value) {
|
|
31
|
+
return isInstance(/^hex$/, value);
|
|
32
|
+
}
|
|
33
|
+
function isHSLColour(value) {
|
|
34
|
+
return isInstance(/^hsl$/, value);
|
|
35
|
+
}
|
|
36
|
+
function isInstance(pattern, value) {
|
|
37
|
+
return typeof value === "object" && value !== null && "$colour" in value && typeof value.$colour === "string" && pattern.test(value.$colour);
|
|
38
|
+
}
|
|
39
|
+
function isRGBColour(value) {
|
|
40
|
+
return isInstance(/^rgb$/, value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/js/colour/base.ts
|
|
44
|
+
class Colour {
|
|
45
|
+
get value() {
|
|
46
|
+
return { ...this.state.value };
|
|
47
|
+
}
|
|
48
|
+
constructor(type, value, defaults, properties) {
|
|
49
|
+
this.$colour = type;
|
|
50
|
+
this.state = {
|
|
51
|
+
value: isColourValue(value, properties) ? { ...value } : { ...defaults }
|
|
52
|
+
};
|
|
53
|
+
}
|
|
33
54
|
}
|
|
34
55
|
|
|
35
56
|
// src/js/colour/hsl.ts
|
|
36
|
-
function
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
lightness
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
function getHSLColour(value) {
|
|
58
|
+
return new HSLColour(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
class HSLColour extends Colour {
|
|
62
|
+
get hue() {
|
|
63
|
+
return +this.state.value.hue;
|
|
64
|
+
}
|
|
65
|
+
set hue(value) {
|
|
66
|
+
this.state.value.hue = clamp(value, 0, 360);
|
|
67
|
+
}
|
|
68
|
+
get lightness() {
|
|
69
|
+
return +this.state.value.lightness;
|
|
70
|
+
}
|
|
71
|
+
set lightness(value) {
|
|
72
|
+
this.state.value.lightness = clamp(value, 0, 100);
|
|
73
|
+
}
|
|
74
|
+
get saturation() {
|
|
75
|
+
return +this.state.value.saturation;
|
|
76
|
+
}
|
|
77
|
+
set saturation(value) {
|
|
78
|
+
this.state.value.saturation = clamp(value, 0, 100);
|
|
79
|
+
}
|
|
80
|
+
constructor(value) {
|
|
81
|
+
super("hsl", value, defaults, properties);
|
|
82
|
+
}
|
|
83
|
+
toHex() {
|
|
84
|
+
return HSLColour.toRgb(this.state.value).toHex();
|
|
85
|
+
}
|
|
86
|
+
toRgb() {
|
|
87
|
+
return HSLColour.toRgb(this.state.value);
|
|
88
|
+
}
|
|
89
|
+
toString() {
|
|
90
|
+
return `hsl(${this.state.value.hue}, ${this.state.value.saturation}%, ${this.state.value.lightness}%)`;
|
|
91
|
+
}
|
|
92
|
+
static toRgb(value) {
|
|
93
|
+
return hslToRgb(value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
var defaults = {
|
|
97
|
+
hue: 0,
|
|
98
|
+
lightness: 0,
|
|
99
|
+
saturation: 0
|
|
100
|
+
};
|
|
101
|
+
var properties = [
|
|
102
|
+
"hue",
|
|
103
|
+
"lightness",
|
|
104
|
+
"saturation"
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// src/js/colour/rgb.ts
|
|
108
|
+
function getRGBColour(value) {
|
|
109
|
+
return new RGBColour(value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
class RGBColour extends Colour {
|
|
113
|
+
get blue() {
|
|
114
|
+
return +this.state.value.blue;
|
|
115
|
+
}
|
|
116
|
+
set blue(value) {
|
|
117
|
+
this.state.value.blue = clamp(value, 0, 255);
|
|
118
|
+
}
|
|
119
|
+
get green() {
|
|
120
|
+
return +this.state.value.green;
|
|
121
|
+
}
|
|
122
|
+
set green(value) {
|
|
123
|
+
this.state.value.green = clamp(value, 0, 255);
|
|
124
|
+
}
|
|
125
|
+
get red() {
|
|
126
|
+
return +this.state.value.red;
|
|
127
|
+
}
|
|
128
|
+
set red(value) {
|
|
129
|
+
this.state.value.red = clamp(value, 0, 255);
|
|
130
|
+
}
|
|
131
|
+
constructor(value) {
|
|
132
|
+
super("rgb", value, defaults2, properties2);
|
|
133
|
+
}
|
|
134
|
+
toHex() {
|
|
135
|
+
return RGBColour.toHex(this.value);
|
|
136
|
+
}
|
|
137
|
+
toHsl() {
|
|
138
|
+
return RGBColour.toHsl(this.value);
|
|
139
|
+
}
|
|
140
|
+
toString() {
|
|
141
|
+
return `rgb(${this.value.red}, ${this.value.green}, ${this.value.blue})`;
|
|
142
|
+
}
|
|
143
|
+
static toHex(value) {
|
|
144
|
+
return rgbToHex(value);
|
|
145
|
+
}
|
|
146
|
+
static toHsl(rgb) {
|
|
147
|
+
return rgbToHsl(rgb);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
var defaults2 = {
|
|
151
|
+
blue: 0,
|
|
152
|
+
green: 0,
|
|
153
|
+
red: 0
|
|
154
|
+
};
|
|
155
|
+
var properties2 = ["blue", "green", "red"];
|
|
156
|
+
|
|
157
|
+
// src/js/colour/functions.ts
|
|
158
|
+
function getNormalisedHex(value) {
|
|
159
|
+
const normalised = value.replace(/^#/, "");
|
|
160
|
+
return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
|
|
161
|
+
}
|
|
162
|
+
function hexToRgb(value) {
|
|
163
|
+
const hex2 = anyPattern.test(value) ? getNormalisedHex(value) : "";
|
|
164
|
+
const pairs = groupedPattern.exec(hex2) ?? [];
|
|
165
|
+
const rgb2 = [];
|
|
166
|
+
const { length } = pairs;
|
|
167
|
+
for (let index = 1;index < length; index += 1) {
|
|
168
|
+
rgb2.push(Number.parseInt(pairs[index], 16));
|
|
169
|
+
}
|
|
170
|
+
return new RGBColour({
|
|
171
|
+
blue: rgb2[2] ?? 0,
|
|
172
|
+
green: rgb2[1] ?? 0,
|
|
173
|
+
red: rgb2[0] ?? 0
|
|
54
174
|
});
|
|
55
|
-
return instance;
|
|
56
175
|
}
|
|
57
176
|
function hslToRgb(value) {
|
|
58
177
|
let hue = value.hue % 360;
|
|
@@ -66,37 +185,14 @@ function hslToRgb(value) {
|
|
|
66
185
|
const mod = saturation * Math.min(lightness, 1 - lightness);
|
|
67
186
|
return lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1));
|
|
68
187
|
}
|
|
69
|
-
return
|
|
188
|
+
return new RGBColour({
|
|
70
189
|
blue: clamp(Math.round(get(4) * 255), 0, 255),
|
|
71
190
|
green: clamp(Math.round(get(8) * 255), 0, 255),
|
|
72
191
|
red: clamp(Math.round(get(0) * 255), 0, 255)
|
|
73
192
|
});
|
|
74
193
|
}
|
|
75
|
-
|
|
76
|
-
// src/js/colour/rgb.ts
|
|
77
|
-
function createRgb(original) {
|
|
78
|
-
const value = { ...original };
|
|
79
|
-
const instance = Object.create({
|
|
80
|
-
toHex() {
|
|
81
|
-
return rgbToHex(value);
|
|
82
|
-
},
|
|
83
|
-
toHsl() {
|
|
84
|
-
return rgbToHsl(value);
|
|
85
|
-
},
|
|
86
|
-
toString() {
|
|
87
|
-
return `rgb(${value.red}, ${value.green}, ${value.blue})`;
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
Object.defineProperties(instance, {
|
|
91
|
-
blue: createProperty(value, "blue", 0, 255),
|
|
92
|
-
green: createProperty(value, "green", 0, 255),
|
|
93
|
-
red: createProperty(value, "red", 0, 255),
|
|
94
|
-
value: { value }
|
|
95
|
-
});
|
|
96
|
-
return instance;
|
|
97
|
-
}
|
|
98
194
|
function rgbToHex(value) {
|
|
99
|
-
return
|
|
195
|
+
return new HexColour(`${[value.red, value.green, value.blue].map((colour) => {
|
|
100
196
|
const hex2 = colour.toString(16);
|
|
101
197
|
return hex2.length === 1 ? `0${hex2}` : hex2;
|
|
102
198
|
}).join("")}`);
|
|
@@ -135,63 +231,57 @@ function rgbToHsl(rgb2) {
|
|
|
135
231
|
if (hue >= 360) {
|
|
136
232
|
hue -= 360;
|
|
137
233
|
}
|
|
138
|
-
return
|
|
234
|
+
return new HSLColour({
|
|
139
235
|
hue: +hue.toFixed(2),
|
|
140
236
|
lightness: +(lightness * 100).toFixed(2),
|
|
141
237
|
saturation: +(saturation * 100).toFixed(2)
|
|
142
238
|
});
|
|
143
239
|
}
|
|
240
|
+
var anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
241
|
+
var groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
144
242
|
|
|
145
243
|
// src/js/colour/hex.ts
|
|
146
|
-
function createHex(original) {
|
|
147
|
-
let value = original.slice();
|
|
148
|
-
const instance = Object.create({
|
|
149
|
-
toHsl() {
|
|
150
|
-
return hexToRgb(value).toHsl();
|
|
151
|
-
},
|
|
152
|
-
toRgb() {
|
|
153
|
-
return hexToRgb(value);
|
|
154
|
-
},
|
|
155
|
-
toString() {
|
|
156
|
-
return `#${value}`;
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
Object.defineProperty(instance, "value", {
|
|
160
|
-
get() {
|
|
161
|
-
return `#${value}`;
|
|
162
|
-
},
|
|
163
|
-
set(hex2) {
|
|
164
|
-
if (anyPattern.test(hex2)) {
|
|
165
|
-
value = getNormalisedHex(hex2);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
});
|
|
169
|
-
return instance;
|
|
170
|
-
}
|
|
171
244
|
function getHexColour(value) {
|
|
172
|
-
return
|
|
245
|
+
return new HexColour(value);
|
|
173
246
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
247
|
+
|
|
248
|
+
class HexColour {
|
|
249
|
+
state;
|
|
250
|
+
get value() {
|
|
251
|
+
return `#${this.state.value}`;
|
|
252
|
+
}
|
|
253
|
+
set value(value) {
|
|
254
|
+
this.state.value = anyPattern.test(value) ? getNormalisedHex(value) : "000000";
|
|
255
|
+
}
|
|
256
|
+
constructor(value) {
|
|
257
|
+
this.$colour = "hex";
|
|
258
|
+
this.state = {
|
|
259
|
+
value: typeof value === "string" && anyPattern.test(value) ? getNormalisedHex(value) : "000000"
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
toHsl() {
|
|
263
|
+
return HexColour.toRgb(this.value).toHsl();
|
|
264
|
+
}
|
|
265
|
+
toRgb() {
|
|
266
|
+
return HexColour.toRgb(this.value);
|
|
267
|
+
}
|
|
268
|
+
toString() {
|
|
269
|
+
return this.value;
|
|
270
|
+
}
|
|
271
|
+
static toRgb(value) {
|
|
272
|
+
return hexToRgb(value);
|
|
185
273
|
}
|
|
186
|
-
return createRgb({ blue: rgb3[2] ?? 0, green: rgb3[1] ?? 0, red: rgb3[0] ?? 0 });
|
|
187
274
|
}
|
|
188
|
-
var anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
189
|
-
var groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
190
275
|
export {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
276
|
+
isRGBColour,
|
|
277
|
+
isHexColour,
|
|
278
|
+
isHSLColour,
|
|
279
|
+
isColour,
|
|
280
|
+
getRGBColour,
|
|
195
281
|
getHexColour,
|
|
196
|
-
|
|
282
|
+
getHSLColour,
|
|
283
|
+
getForegroundColour,
|
|
284
|
+
RGBColour,
|
|
285
|
+
HexColour,
|
|
286
|
+
HSLColour
|
|
197
287
|
};
|
package/dist/js/colour/index.mjs
CHANGED
|
@@ -11,16 +11,20 @@ function getForegroundColour(value) {
|
|
|
11
11
|
const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
|
|
12
12
|
return luminance > 0.625 ? "black" : "white";
|
|
13
13
|
}
|
|
14
|
-
import {getHexColour,
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import {rgbToHex, rgbToHsl} from "./rgb";
|
|
14
|
+
import {getHexColour, HexColour} from "./hex";
|
|
15
|
+
import {getHSLColour, HSLColour} from "./hsl";
|
|
16
|
+
import {isColour, isHexColour, isHSLColour, isRGBColour} from "./is";
|
|
17
|
+
import {getRGBColour, RGBColour} from "./rgb";
|
|
19
18
|
export {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
isRGBColour,
|
|
20
|
+
isHexColour,
|
|
21
|
+
isHSLColour,
|
|
22
|
+
isColour,
|
|
23
|
+
getRGBColour,
|
|
24
24
|
getHexColour,
|
|
25
|
-
|
|
25
|
+
getHSLColour,
|
|
26
|
+
getForegroundColour,
|
|
27
|
+
RGBColour,
|
|
28
|
+
HexColour,
|
|
29
|
+
HSLColour
|
|
26
30
|
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// src/js/colour/is.ts
|
|
2
|
+
function isColour(value) {
|
|
3
|
+
return isInstance(/^(hex|hsl|rgb)$/, value);
|
|
4
|
+
}
|
|
5
|
+
function isColourValue(value, properties) {
|
|
6
|
+
return typeof value === "object" && value !== null && properties.every((property) => (property in value) && typeof value[property] === "number");
|
|
7
|
+
}
|
|
8
|
+
function isHexColour(value) {
|
|
9
|
+
return isInstance(/^hex$/, value);
|
|
10
|
+
}
|
|
11
|
+
function isHSLColour(value) {
|
|
12
|
+
return isInstance(/^hsl$/, value);
|
|
13
|
+
}
|
|
14
|
+
function isInstance(pattern, value) {
|
|
15
|
+
return typeof value === "object" && value !== null && "$colour" in value && typeof value.$colour === "string" && pattern.test(value.$colour);
|
|
16
|
+
}
|
|
17
|
+
function isRGBColour(value) {
|
|
18
|
+
return isInstance(/^rgb$/, value);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
isRGBColour,
|
|
22
|
+
isHexColour,
|
|
23
|
+
isHSLColour,
|
|
24
|
+
isColourValue,
|
|
25
|
+
isColour
|
|
26
|
+
};
|