@oscarpalmer/atoms 0.38.1 → 0.40.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.js +185 -0
- package/dist/js/colour.mjs +178 -0
- package/dist/js/index.js +263 -52
- package/dist/js/index.mjs +2 -0
- package/dist/js/is.js +8 -0
- package/dist/js/is.mjs +8 -0
- package/dist/js/random.js +30 -0
- package/dist/js/random.mjs +30 -0
- package/dist/js/string.js +13 -1
- package/dist/js/string.mjs +13 -1
- package/dist/js/timer.js +2 -15
- package/dist/js/timer.mjs +2 -15
- package/package.json +9 -5
- package/src/js/colour.ts +338 -0
- package/src/js/index.ts +2 -0
- package/src/js/is.ts +14 -0
- package/src/js/random.ts +46 -0
- package/src/js/string.ts +28 -3
- package/src/js/timer.ts +3 -24
- package/types/colour.d.ts +101 -0
- package/types/index.d.ts +2 -0
- package/types/is.d.ts +1 -0
- package/types/random.d.ts +24 -0
- package/types/string.d.ts +9 -0
- package/types/timer.d.ts +1 -1
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// src/js/number.ts
|
|
2
|
+
function clamp(value, min, max, loop) {
|
|
3
|
+
if (value < min) {
|
|
4
|
+
return loop === true ? max : min;
|
|
5
|
+
}
|
|
6
|
+
return value > max ? loop === true ? min : max : value;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/js/colour.ts
|
|
10
|
+
var createHex = function(original) {
|
|
11
|
+
let value = original.slice();
|
|
12
|
+
const instance = Object.create({
|
|
13
|
+
toHsl() {
|
|
14
|
+
return hexToRgb(value).toHsl();
|
|
15
|
+
},
|
|
16
|
+
toRgb() {
|
|
17
|
+
return hexToRgb(value);
|
|
18
|
+
},
|
|
19
|
+
toString() {
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(instance, "value", {
|
|
24
|
+
get() {
|
|
25
|
+
return value.slice();
|
|
26
|
+
},
|
|
27
|
+
set(hex) {
|
|
28
|
+
if (anyPattern.test(hex)) {
|
|
29
|
+
value = `#${getHex(hex)}`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return instance;
|
|
34
|
+
};
|
|
35
|
+
var createHsl = function(original) {
|
|
36
|
+
const value = { ...original };
|
|
37
|
+
const instance = Object.create({
|
|
38
|
+
toHex() {
|
|
39
|
+
return hslToRgb(value).toHex();
|
|
40
|
+
},
|
|
41
|
+
toRgb() {
|
|
42
|
+
return hslToRgb(value);
|
|
43
|
+
},
|
|
44
|
+
toString() {
|
|
45
|
+
return `hsl(${value.hue}, ${value.saturation}%, ${value.lightness}%)`;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperties(instance, {
|
|
49
|
+
hue: createProperty(value, "hue", 0, 360),
|
|
50
|
+
lightness: createProperty(value, "lightness", 0, 100),
|
|
51
|
+
saturation: createProperty(value, "saturation", 0, 100),
|
|
52
|
+
value: { value }
|
|
53
|
+
});
|
|
54
|
+
return instance;
|
|
55
|
+
};
|
|
56
|
+
var createProperty = function(store, key, min, max) {
|
|
57
|
+
return {
|
|
58
|
+
get() {
|
|
59
|
+
return store[key];
|
|
60
|
+
},
|
|
61
|
+
set(value) {
|
|
62
|
+
store[key] = clamp(value, min, max);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
var createRgb = function(original) {
|
|
67
|
+
const value = { ...original };
|
|
68
|
+
const instance = Object.create({
|
|
69
|
+
toHex() {
|
|
70
|
+
return rgbToHex(value);
|
|
71
|
+
},
|
|
72
|
+
toHsl() {
|
|
73
|
+
return rgbToHsl(value);
|
|
74
|
+
},
|
|
75
|
+
toString() {
|
|
76
|
+
return `rgb(${value.red}, ${value.green}, ${value.blue})`;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperties(instance, {
|
|
80
|
+
blue: createProperty(value, "blue", 0, 255),
|
|
81
|
+
green: createProperty(value, "green", 0, 255),
|
|
82
|
+
red: createProperty(value, "red", 0, 255),
|
|
83
|
+
value: { value }
|
|
84
|
+
});
|
|
85
|
+
return instance;
|
|
86
|
+
};
|
|
87
|
+
function getForegroundColour(value) {
|
|
88
|
+
const values = [value.blue / 255, value.green / 255, value.red / 255];
|
|
89
|
+
for (let colour of values) {
|
|
90
|
+
if (colour <= 0.03928) {
|
|
91
|
+
colour /= 12.92;
|
|
92
|
+
} else {
|
|
93
|
+
colour = ((colour + 0.055) / 1.055) ** 2.4;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
|
|
97
|
+
return luminance > 0.625 ? "black" : "white";
|
|
98
|
+
}
|
|
99
|
+
var getHex = function(value) {
|
|
100
|
+
const normalised = value.replace(/^#/, "");
|
|
101
|
+
return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
|
|
102
|
+
};
|
|
103
|
+
function hexToRgb(value) {
|
|
104
|
+
const hex = anyPattern.test(value) ? getHex(value) : "";
|
|
105
|
+
const pairs = groupedPattern.exec(hex) ?? [];
|
|
106
|
+
const rgb = [];
|
|
107
|
+
const { length } = pairs;
|
|
108
|
+
for (let index = 1;index < length; index += 1) {
|
|
109
|
+
rgb.push(Number.parseInt(pairs[index], 16));
|
|
110
|
+
}
|
|
111
|
+
return createRgb({ blue: rgb[2] ?? 0, green: rgb[1] ?? 0, red: rgb[0] ?? 0 });
|
|
112
|
+
}
|
|
113
|
+
function hslToRgb(value) {
|
|
114
|
+
let hue = value.hue % 360;
|
|
115
|
+
if (hue < 0) {
|
|
116
|
+
hue += 360;
|
|
117
|
+
}
|
|
118
|
+
const saturation = value.saturation / 100;
|
|
119
|
+
const lightness = value.lightness / 100;
|
|
120
|
+
function get(value2) {
|
|
121
|
+
const part = (value2 + hue / 30) % 12;
|
|
122
|
+
const mod = saturation * Math.min(lightness, 1 - lightness);
|
|
123
|
+
return lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1));
|
|
124
|
+
}
|
|
125
|
+
return createRgb({
|
|
126
|
+
blue: clamp(Math.round(get(4) * 255), 0, 255),
|
|
127
|
+
green: clamp(Math.round(get(8) * 255), 0, 255),
|
|
128
|
+
red: clamp(Math.round(get(0) * 255), 0, 255)
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
function rgbToHex(value) {
|
|
132
|
+
return createHex(`#${[value.red, value.green, value.blue].map((colour) => {
|
|
133
|
+
const hex = colour.toString(16);
|
|
134
|
+
return hex.length === 1 ? `0${hex}` : hex;
|
|
135
|
+
}).join("")}`);
|
|
136
|
+
}
|
|
137
|
+
function rgbToHsl(rgb) {
|
|
138
|
+
const blue = rgb.blue / 255;
|
|
139
|
+
const green = rgb.green / 255;
|
|
140
|
+
const red = rgb.red / 255;
|
|
141
|
+
const max = Math.max(blue, green, red);
|
|
142
|
+
const min = Math.min(blue, green, red);
|
|
143
|
+
const delta = max - min;
|
|
144
|
+
const lightness = (min + max) / 2;
|
|
145
|
+
let hue = 0;
|
|
146
|
+
let saturation = 0;
|
|
147
|
+
if (delta !== 0) {
|
|
148
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (max - lightness) / Math.min(lightness, 1 - lightness);
|
|
149
|
+
switch (max) {
|
|
150
|
+
case blue:
|
|
151
|
+
hue = (red - green) / delta + 4;
|
|
152
|
+
break;
|
|
153
|
+
case green:
|
|
154
|
+
hue = (blue - red) / delta + 2;
|
|
155
|
+
break;
|
|
156
|
+
case red:
|
|
157
|
+
hue = (green - blue) / delta + (green < blue ? 6 : 0);
|
|
158
|
+
break;
|
|
159
|
+
default:
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
hue *= 60;
|
|
163
|
+
}
|
|
164
|
+
if (saturation < 0) {
|
|
165
|
+
hue += 180;
|
|
166
|
+
saturation = Math.abs(saturation);
|
|
167
|
+
}
|
|
168
|
+
if (hue >= 360) {
|
|
169
|
+
hue -= 360;
|
|
170
|
+
}
|
|
171
|
+
return createHsl({
|
|
172
|
+
hue: +hue.toFixed(2),
|
|
173
|
+
lightness: +(lightness * 100).toFixed(2),
|
|
174
|
+
saturation: +(saturation * 100).toFixed(2)
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
var anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
178
|
+
var groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
179
|
+
export {
|
|
180
|
+
rgbToHsl,
|
|
181
|
+
rgbToHex,
|
|
182
|
+
hslToRgb,
|
|
183
|
+
hexToRgb,
|
|
184
|
+
getForegroundColour
|
|
185
|
+
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// src/js/colour.ts
|
|
2
|
+
import {clamp} from "./number";
|
|
3
|
+
var createHex = function(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.slice();
|
|
19
|
+
},
|
|
20
|
+
set(hex) {
|
|
21
|
+
if (anyPattern.test(hex)) {
|
|
22
|
+
value = `#${getHex(hex)}`;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return instance;
|
|
27
|
+
};
|
|
28
|
+
var createHsl = function(original) {
|
|
29
|
+
const value = { ...original };
|
|
30
|
+
const instance = Object.create({
|
|
31
|
+
toHex() {
|
|
32
|
+
return hslToRgb(value).toHex();
|
|
33
|
+
},
|
|
34
|
+
toRgb() {
|
|
35
|
+
return hslToRgb(value);
|
|
36
|
+
},
|
|
37
|
+
toString() {
|
|
38
|
+
return `hsl(${value.hue}, ${value.saturation}%, ${value.lightness}%)`;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperties(instance, {
|
|
42
|
+
hue: createProperty(value, "hue", 0, 360),
|
|
43
|
+
lightness: createProperty(value, "lightness", 0, 100),
|
|
44
|
+
saturation: createProperty(value, "saturation", 0, 100),
|
|
45
|
+
value: { value }
|
|
46
|
+
});
|
|
47
|
+
return instance;
|
|
48
|
+
};
|
|
49
|
+
var createProperty = function(store, key, min, max) {
|
|
50
|
+
return {
|
|
51
|
+
get() {
|
|
52
|
+
return store[key];
|
|
53
|
+
},
|
|
54
|
+
set(value) {
|
|
55
|
+
store[key] = clamp(value, min, max);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
var createRgb = function(original) {
|
|
60
|
+
const value = { ...original };
|
|
61
|
+
const instance = Object.create({
|
|
62
|
+
toHex() {
|
|
63
|
+
return rgbToHex(value);
|
|
64
|
+
},
|
|
65
|
+
toHsl() {
|
|
66
|
+
return rgbToHsl(value);
|
|
67
|
+
},
|
|
68
|
+
toString() {
|
|
69
|
+
return `rgb(${value.red}, ${value.green}, ${value.blue})`;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperties(instance, {
|
|
73
|
+
blue: createProperty(value, "blue", 0, 255),
|
|
74
|
+
green: createProperty(value, "green", 0, 255),
|
|
75
|
+
red: createProperty(value, "red", 0, 255),
|
|
76
|
+
value: { value }
|
|
77
|
+
});
|
|
78
|
+
return instance;
|
|
79
|
+
};
|
|
80
|
+
function getForegroundColour(value) {
|
|
81
|
+
const values = [value.blue / 255, value.green / 255, value.red / 255];
|
|
82
|
+
for (let colour of values) {
|
|
83
|
+
if (colour <= 0.03928) {
|
|
84
|
+
colour /= 12.92;
|
|
85
|
+
} else {
|
|
86
|
+
colour = ((colour + 0.055) / 1.055) ** 2.4;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
|
|
90
|
+
return luminance > 0.625 ? "black" : "white";
|
|
91
|
+
}
|
|
92
|
+
var getHex = function(value) {
|
|
93
|
+
const normalised = value.replace(/^#/, "");
|
|
94
|
+
return normalised.length === 3 ? normalised.split("").map((character) => character.repeat(2)).join("") : normalised;
|
|
95
|
+
};
|
|
96
|
+
function hexToRgb(value) {
|
|
97
|
+
const hex = anyPattern.test(value) ? getHex(value) : "";
|
|
98
|
+
const pairs = groupedPattern.exec(hex) ?? [];
|
|
99
|
+
const rgb = [];
|
|
100
|
+
const { length } = pairs;
|
|
101
|
+
for (let index = 1;index < length; index += 1) {
|
|
102
|
+
rgb.push(Number.parseInt(pairs[index], 16));
|
|
103
|
+
}
|
|
104
|
+
return createRgb({ blue: rgb[2] ?? 0, green: rgb[1] ?? 0, red: rgb[0] ?? 0 });
|
|
105
|
+
}
|
|
106
|
+
function hslToRgb(value) {
|
|
107
|
+
let hue = value.hue % 360;
|
|
108
|
+
if (hue < 0) {
|
|
109
|
+
hue += 360;
|
|
110
|
+
}
|
|
111
|
+
const saturation = value.saturation / 100;
|
|
112
|
+
const lightness = value.lightness / 100;
|
|
113
|
+
function get(value2) {
|
|
114
|
+
const part = (value2 + hue / 30) % 12;
|
|
115
|
+
const mod = saturation * Math.min(lightness, 1 - lightness);
|
|
116
|
+
return lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1));
|
|
117
|
+
}
|
|
118
|
+
return createRgb({
|
|
119
|
+
blue: clamp(Math.round(get(4) * 255), 0, 255),
|
|
120
|
+
green: clamp(Math.round(get(8) * 255), 0, 255),
|
|
121
|
+
red: clamp(Math.round(get(0) * 255), 0, 255)
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function rgbToHex(value) {
|
|
125
|
+
return createHex(`#${[value.red, value.green, value.blue].map((colour) => {
|
|
126
|
+
const hex = colour.toString(16);
|
|
127
|
+
return hex.length === 1 ? `0${hex}` : hex;
|
|
128
|
+
}).join("")}`);
|
|
129
|
+
}
|
|
130
|
+
function rgbToHsl(rgb) {
|
|
131
|
+
const blue = rgb.blue / 255;
|
|
132
|
+
const green = rgb.green / 255;
|
|
133
|
+
const red = rgb.red / 255;
|
|
134
|
+
const max = Math.max(blue, green, red);
|
|
135
|
+
const min = Math.min(blue, green, red);
|
|
136
|
+
const delta = max - min;
|
|
137
|
+
const lightness = (min + max) / 2;
|
|
138
|
+
let hue = 0;
|
|
139
|
+
let saturation = 0;
|
|
140
|
+
if (delta !== 0) {
|
|
141
|
+
saturation = lightness === 0 || lightness === 1 ? 0 : (max - lightness) / Math.min(lightness, 1 - lightness);
|
|
142
|
+
switch (max) {
|
|
143
|
+
case blue:
|
|
144
|
+
hue = (red - green) / delta + 4;
|
|
145
|
+
break;
|
|
146
|
+
case green:
|
|
147
|
+
hue = (blue - red) / delta + 2;
|
|
148
|
+
break;
|
|
149
|
+
case red:
|
|
150
|
+
hue = (green - blue) / delta + (green < blue ? 6 : 0);
|
|
151
|
+
break;
|
|
152
|
+
default:
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
hue *= 60;
|
|
156
|
+
}
|
|
157
|
+
if (saturation < 0) {
|
|
158
|
+
hue += 180;
|
|
159
|
+
saturation = Math.abs(saturation);
|
|
160
|
+
}
|
|
161
|
+
if (hue >= 360) {
|
|
162
|
+
hue -= 360;
|
|
163
|
+
}
|
|
164
|
+
return createHsl({
|
|
165
|
+
hue: +hue.toFixed(2),
|
|
166
|
+
lightness: +(lightness * 100).toFixed(2),
|
|
167
|
+
saturation: +(saturation * 100).toFixed(2)
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
var anyPattern = /^#*([a-f0-9]{3}){1,2}$/i;
|
|
171
|
+
var groupedPattern = /^#*([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
172
|
+
export {
|
|
173
|
+
rgbToHsl,
|
|
174
|
+
rgbToHex,
|
|
175
|
+
hslToRgb,
|
|
176
|
+
hexToRgb,
|
|
177
|
+
getForegroundColour
|
|
178
|
+
};
|