@oscarpalmer/atoms 0.91.0 → 0.92.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/color.cjs +56 -5
- package/dist/color.js +56 -5
- package/package.json +1 -1
- package/src/color.ts +78 -11
- package/types/color.d.cts +15 -0
- package/types/color.d.ts +15 -0
- package/types/index.d.cts +15 -0
package/dist/color.cjs
CHANGED
|
@@ -7,15 +7,51 @@ const number = require('./number.cjs');
|
|
|
7
7
|
class Color {
|
|
8
8
|
$color = true;
|
|
9
9
|
state;
|
|
10
|
+
/**
|
|
11
|
+
* Get the color as a hex-color
|
|
12
|
+
*/
|
|
10
13
|
get hex() {
|
|
11
14
|
return this.state.hex;
|
|
12
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Set colors from a hex-color
|
|
18
|
+
*/
|
|
19
|
+
set hex(value) {
|
|
20
|
+
if (isHexColor(value) && value !== this.state.hex) {
|
|
21
|
+
const hex = getNormalisedHex(value);
|
|
22
|
+
const rgb = hexToRgb(hex);
|
|
23
|
+
this.state.hex = hex;
|
|
24
|
+
this.state.hsl = rgbToHsl(rgb);
|
|
25
|
+
this.state.rgb = rgb;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get the color as an HSL-color
|
|
30
|
+
*/
|
|
13
31
|
get hsl() {
|
|
14
32
|
return this.state.hsl;
|
|
15
33
|
}
|
|
34
|
+
set hsl(value) {
|
|
35
|
+
if (isHSLColor(value)) {
|
|
36
|
+
const rgb = hslToRgb(value);
|
|
37
|
+
this.state.hex = rgbToHex(rgb);
|
|
38
|
+
this.state.hsl = value;
|
|
39
|
+
this.state.rgb = rgb;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get the color as an RGB-color
|
|
44
|
+
*/
|
|
16
45
|
get rgb() {
|
|
17
46
|
return this.state.rgb;
|
|
18
47
|
}
|
|
48
|
+
set rgb(value) {
|
|
49
|
+
if (isRGBColor(value)) {
|
|
50
|
+
this.state.hex = rgbToHex(value);
|
|
51
|
+
this.state.hsl = rgbToHsl(value);
|
|
52
|
+
this.state.rgb = value;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
19
55
|
constructor(value) {
|
|
20
56
|
this.state = getState(value);
|
|
21
57
|
}
|
|
@@ -132,22 +168,28 @@ function isHexColor(value) {
|
|
|
132
168
|
function isHSLColor(value) {
|
|
133
169
|
return isObject(value, hslKeys);
|
|
134
170
|
}
|
|
171
|
+
function isInRange(value, min, max) {
|
|
172
|
+
if (typeof value === "number" || typeof value === "string" && numberPattern.test(value)) {
|
|
173
|
+
const asNumber = Number(value);
|
|
174
|
+
return asNumber >= min && asNumber <= max;
|
|
175
|
+
}
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
135
178
|
function isRGBColor(value) {
|
|
136
179
|
return isObject(value, rgbKeys);
|
|
137
180
|
}
|
|
138
|
-
function isObject(
|
|
139
|
-
if (typeof
|
|
181
|
+
function isObject(obj, properties) {
|
|
182
|
+
if (typeof obj !== "object" || obj === null) {
|
|
140
183
|
return false;
|
|
141
184
|
}
|
|
142
|
-
const keys = Object.keys(
|
|
185
|
+
const keys = Object.keys(obj);
|
|
143
186
|
const { length } = keys;
|
|
144
187
|
if (length !== properties.size) {
|
|
145
188
|
return false;
|
|
146
189
|
}
|
|
147
190
|
for (let index = 0; index < length; index += 1) {
|
|
148
191
|
const key = keys[index];
|
|
149
|
-
|
|
150
|
-
if (!properties.has(key) || !(typeof val === "number" || typeof val === "string" && /^\d+$/.test(val))) {
|
|
192
|
+
if (!properties.has(key) || !validators[key](obj[key])) {
|
|
151
193
|
return false;
|
|
152
194
|
}
|
|
153
195
|
}
|
|
@@ -211,7 +253,16 @@ const defaultRgb = {
|
|
|
211
253
|
};
|
|
212
254
|
const groupedPattern = /^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
213
255
|
const hslKeys = /* @__PURE__ */ new Set(["hue", "lightness", "saturation"]);
|
|
256
|
+
const numberPattern = /^\d+$/;
|
|
214
257
|
const rgbKeys = /* @__PURE__ */ new Set(["blue", "green", "red"]);
|
|
258
|
+
const validators = {
|
|
259
|
+
blue: (value) => isInRange(value, 0, 255),
|
|
260
|
+
green: (value) => isInRange(value, 0, 255),
|
|
261
|
+
hue: (value) => isInRange(value, 0, 360),
|
|
262
|
+
lightness: (value) => isInRange(value, 0, 100),
|
|
263
|
+
saturation: (value) => isInRange(value, 0, 100),
|
|
264
|
+
red: (value) => isInRange(value, 0, 255)
|
|
265
|
+
};
|
|
215
266
|
|
|
216
267
|
exports.Color = Color;
|
|
217
268
|
exports.getColor = getColor;
|
package/dist/color.js
CHANGED
|
@@ -3,15 +3,51 @@ import { clamp } from './number.js';
|
|
|
3
3
|
class Color {
|
|
4
4
|
$color = true;
|
|
5
5
|
state;
|
|
6
|
+
/**
|
|
7
|
+
* Get the color as a hex-color
|
|
8
|
+
*/
|
|
6
9
|
get hex() {
|
|
7
10
|
return this.state.hex;
|
|
8
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Set colors from a hex-color
|
|
14
|
+
*/
|
|
15
|
+
set hex(value) {
|
|
16
|
+
if (isHexColor(value) && value !== this.state.hex) {
|
|
17
|
+
const hex = getNormalisedHex(value);
|
|
18
|
+
const rgb = hexToRgb(hex);
|
|
19
|
+
this.state.hex = hex;
|
|
20
|
+
this.state.hsl = rgbToHsl(rgb);
|
|
21
|
+
this.state.rgb = rgb;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Get the color as an HSL-color
|
|
26
|
+
*/
|
|
9
27
|
get hsl() {
|
|
10
28
|
return this.state.hsl;
|
|
11
29
|
}
|
|
30
|
+
set hsl(value) {
|
|
31
|
+
if (isHSLColor(value)) {
|
|
32
|
+
const rgb = hslToRgb(value);
|
|
33
|
+
this.state.hex = rgbToHex(rgb);
|
|
34
|
+
this.state.hsl = value;
|
|
35
|
+
this.state.rgb = rgb;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get the color as an RGB-color
|
|
40
|
+
*/
|
|
12
41
|
get rgb() {
|
|
13
42
|
return this.state.rgb;
|
|
14
43
|
}
|
|
44
|
+
set rgb(value) {
|
|
45
|
+
if (isRGBColor(value)) {
|
|
46
|
+
this.state.hex = rgbToHex(value);
|
|
47
|
+
this.state.hsl = rgbToHsl(value);
|
|
48
|
+
this.state.rgb = value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
15
51
|
constructor(value) {
|
|
16
52
|
this.state = getState(value);
|
|
17
53
|
}
|
|
@@ -128,22 +164,28 @@ function isHexColor(value) {
|
|
|
128
164
|
function isHSLColor(value) {
|
|
129
165
|
return isObject(value, hslKeys);
|
|
130
166
|
}
|
|
167
|
+
function isInRange(value, min, max) {
|
|
168
|
+
if (typeof value === "number" || typeof value === "string" && numberPattern.test(value)) {
|
|
169
|
+
const asNumber = Number(value);
|
|
170
|
+
return asNumber >= min && asNumber <= max;
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
131
174
|
function isRGBColor(value) {
|
|
132
175
|
return isObject(value, rgbKeys);
|
|
133
176
|
}
|
|
134
|
-
function isObject(
|
|
135
|
-
if (typeof
|
|
177
|
+
function isObject(obj, properties) {
|
|
178
|
+
if (typeof obj !== "object" || obj === null) {
|
|
136
179
|
return false;
|
|
137
180
|
}
|
|
138
|
-
const keys = Object.keys(
|
|
181
|
+
const keys = Object.keys(obj);
|
|
139
182
|
const { length } = keys;
|
|
140
183
|
if (length !== properties.size) {
|
|
141
184
|
return false;
|
|
142
185
|
}
|
|
143
186
|
for (let index = 0; index < length; index += 1) {
|
|
144
187
|
const key = keys[index];
|
|
145
|
-
|
|
146
|
-
if (!properties.has(key) || !(typeof val === "number" || typeof val === "string" && /^\d+$/.test(val))) {
|
|
188
|
+
if (!properties.has(key) || !validators[key](obj[key])) {
|
|
147
189
|
return false;
|
|
148
190
|
}
|
|
149
191
|
}
|
|
@@ -207,6 +249,15 @@ const defaultRgb = {
|
|
|
207
249
|
};
|
|
208
250
|
const groupedPattern = /^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
209
251
|
const hslKeys = /* @__PURE__ */ new Set(["hue", "lightness", "saturation"]);
|
|
252
|
+
const numberPattern = /^\d+$/;
|
|
210
253
|
const rgbKeys = /* @__PURE__ */ new Set(["blue", "green", "red"]);
|
|
254
|
+
const validators = {
|
|
255
|
+
blue: (value) => isInRange(value, 0, 255),
|
|
256
|
+
green: (value) => isInRange(value, 0, 255),
|
|
257
|
+
hue: (value) => isInRange(value, 0, 360),
|
|
258
|
+
lightness: (value) => isInRange(value, 0, 100),
|
|
259
|
+
saturation: (value) => isInRange(value, 0, 100),
|
|
260
|
+
red: (value) => isInRange(value, 0, 255)
|
|
261
|
+
};
|
|
211
262
|
|
|
212
263
|
export { Color, getColor, getForegroundColor, getHSLColor, getHexColor, getRGBColor, isColor, isHSLColor, isHexColor, isRGBColor };
|
package/package.json
CHANGED
package/src/color.ts
CHANGED
|
@@ -13,6 +13,8 @@ export type RGBColor = {
|
|
|
13
13
|
red: number;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
type Property = 'blue' | 'green' | 'hue' | 'lightness' | 'red' | 'saturation';
|
|
17
|
+
|
|
16
18
|
type State = {
|
|
17
19
|
hex: string;
|
|
18
20
|
hsl: HSLColor;
|
|
@@ -24,18 +26,59 @@ export class Color {
|
|
|
24
26
|
|
|
25
27
|
private readonly state: State;
|
|
26
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Get the color as a hex-color
|
|
31
|
+
*/
|
|
27
32
|
get hex(): string {
|
|
28
33
|
return this.state.hex;
|
|
29
34
|
}
|
|
30
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Set colors from a hex-color
|
|
38
|
+
*/
|
|
39
|
+
set hex(value: string) {
|
|
40
|
+
if (isHexColor(value) && value !== this.state.hex) {
|
|
41
|
+
const hex = getNormalisedHex(value);
|
|
42
|
+
const rgb = hexToRgb(hex);
|
|
43
|
+
|
|
44
|
+
this.state.hex = hex;
|
|
45
|
+
this.state.hsl = rgbToHsl(rgb);
|
|
46
|
+
this.state.rgb = rgb;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Get the color as an HSL-color
|
|
52
|
+
*/
|
|
31
53
|
get hsl(): HSLColor {
|
|
32
54
|
return this.state.hsl;
|
|
33
55
|
}
|
|
34
56
|
|
|
57
|
+
set hsl(value: HSLColor) {
|
|
58
|
+
if (isHSLColor(value)) {
|
|
59
|
+
const rgb = hslToRgb(value);
|
|
60
|
+
|
|
61
|
+
this.state.hex = rgbToHex(rgb);
|
|
62
|
+
this.state.hsl = value;
|
|
63
|
+
this.state.rgb = rgb;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Get the color as an RGB-color
|
|
69
|
+
*/
|
|
35
70
|
get rgb(): RGBColor {
|
|
36
71
|
return this.state.rgb;
|
|
37
72
|
}
|
|
38
73
|
|
|
74
|
+
set rgb(value: RGBColor) {
|
|
75
|
+
if (isRGBColor(value)) {
|
|
76
|
+
this.state.hex = rgbToHex(value);
|
|
77
|
+
this.state.hsl = rgbToHsl(value);
|
|
78
|
+
this.state.rgb = value;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
39
82
|
constructor(value: unknown) {
|
|
40
83
|
this.state = getState(value);
|
|
41
84
|
}
|
|
@@ -238,6 +281,19 @@ export function isHSLColor(value: unknown): value is HSLColor {
|
|
|
238
281
|
return isObject(value, hslKeys);
|
|
239
282
|
}
|
|
240
283
|
|
|
284
|
+
function isInRange(value: unknown, min: number, max: number): boolean {
|
|
285
|
+
if (
|
|
286
|
+
typeof value === 'number' ||
|
|
287
|
+
(typeof value === 'string' && numberPattern.test(value))
|
|
288
|
+
) {
|
|
289
|
+
const asNumber = Number(value);
|
|
290
|
+
|
|
291
|
+
return asNumber >= min && asNumber <= max;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
241
297
|
/**
|
|
242
298
|
* Is the value an RGB-color?
|
|
243
299
|
*/
|
|
@@ -245,12 +301,15 @@ export function isRGBColor(value: unknown): value is RGBColor {
|
|
|
245
301
|
return isObject(value, rgbKeys);
|
|
246
302
|
}
|
|
247
303
|
|
|
248
|
-
|
|
249
|
-
|
|
304
|
+
/**
|
|
305
|
+
* Is the value an object with the given properties and value requirements?
|
|
306
|
+
*/
|
|
307
|
+
function isObject(obj: unknown, properties: Set<Property>): boolean {
|
|
308
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
250
309
|
return false;
|
|
251
310
|
}
|
|
252
311
|
|
|
253
|
-
const keys = Object.keys(
|
|
312
|
+
const keys = Object.keys(obj);
|
|
254
313
|
const {length} = keys;
|
|
255
314
|
|
|
256
315
|
if (length !== properties.size) {
|
|
@@ -259,14 +318,10 @@ function isObject(value: unknown, properties: Set<string>): boolean {
|
|
|
259
318
|
|
|
260
319
|
for (let index = 0; index < length; index += 1) {
|
|
261
320
|
const key = keys[index];
|
|
262
|
-
const val = (value as PlainObject)[key];
|
|
263
321
|
|
|
264
322
|
if (
|
|
265
|
-
!properties.has(key) ||
|
|
266
|
-
!(
|
|
267
|
-
typeof val === 'number' ||
|
|
268
|
-
(typeof val === 'string' && /^\d+$/.test(val))
|
|
269
|
-
)
|
|
323
|
+
!properties.has(key as never) ||
|
|
324
|
+
!validators[key as Property]((obj as PlainObject)[key])
|
|
270
325
|
) {
|
|
271
326
|
return false;
|
|
272
327
|
}
|
|
@@ -361,5 +416,17 @@ const defaultRgb: RGBColor = {
|
|
|
361
416
|
|
|
362
417
|
const groupedPattern = /^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
|
|
363
418
|
|
|
364
|
-
const hslKeys = new Set(['hue', 'lightness', 'saturation']);
|
|
365
|
-
|
|
419
|
+
const hslKeys = new Set<Property>(['hue', 'lightness', 'saturation']);
|
|
420
|
+
|
|
421
|
+
const numberPattern = /^\d+$/;
|
|
422
|
+
|
|
423
|
+
const rgbKeys = new Set<Property>(['blue', 'green', 'red']);
|
|
424
|
+
|
|
425
|
+
const validators: Record<Property, (value: unknown) => boolean> = {
|
|
426
|
+
blue: value => isInRange(value, 0, 255),
|
|
427
|
+
green: value => isInRange(value, 0, 255),
|
|
428
|
+
hue: value => isInRange(value, 0, 360),
|
|
429
|
+
lightness: value => isInRange(value, 0, 100),
|
|
430
|
+
saturation: value => isInRange(value, 0, 100),
|
|
431
|
+
red: value => isInRange(value, 0, 255),
|
|
432
|
+
};
|
package/types/color.d.cts
CHANGED
|
@@ -13,9 +13,24 @@ export type RGBColor = {
|
|
|
13
13
|
export declare class Color {
|
|
14
14
|
private readonly $color;
|
|
15
15
|
private readonly state;
|
|
16
|
+
/**
|
|
17
|
+
* Get the color as a hex-color
|
|
18
|
+
*/
|
|
16
19
|
get hex(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Set colors from a hex-color
|
|
22
|
+
*/
|
|
23
|
+
set hex(value: string);
|
|
24
|
+
/**
|
|
25
|
+
* Get the color as an HSL-color
|
|
26
|
+
*/
|
|
17
27
|
get hsl(): HSLColor;
|
|
28
|
+
set hsl(value: HSLColor);
|
|
29
|
+
/**
|
|
30
|
+
* Get the color as an RGB-color
|
|
31
|
+
*/
|
|
18
32
|
get rgb(): RGBColor;
|
|
33
|
+
set rgb(value: RGBColor);
|
|
19
34
|
constructor(value: unknown);
|
|
20
35
|
}
|
|
21
36
|
/**
|
package/types/color.d.ts
CHANGED
|
@@ -11,9 +11,24 @@ export type RGBColor = {
|
|
|
11
11
|
export declare class Color {
|
|
12
12
|
private readonly $color;
|
|
13
13
|
private readonly state;
|
|
14
|
+
/**
|
|
15
|
+
* Get the color as a hex-color
|
|
16
|
+
*/
|
|
14
17
|
get hex(): string;
|
|
18
|
+
/**
|
|
19
|
+
* Set colors from a hex-color
|
|
20
|
+
*/
|
|
21
|
+
set hex(value: string);
|
|
22
|
+
/**
|
|
23
|
+
* Get the color as an HSL-color
|
|
24
|
+
*/
|
|
15
25
|
get hsl(): HSLColor;
|
|
26
|
+
set hsl(value: HSLColor);
|
|
27
|
+
/**
|
|
28
|
+
* Get the color as an RGB-color
|
|
29
|
+
*/
|
|
16
30
|
get rgb(): RGBColor;
|
|
31
|
+
set rgb(value: RGBColor);
|
|
17
32
|
constructor(value: unknown);
|
|
18
33
|
}
|
|
19
34
|
/**
|
package/types/index.d.cts
CHANGED
|
@@ -2471,9 +2471,24 @@ export type RGBColor = {
|
|
|
2471
2471
|
export declare class Color {
|
|
2472
2472
|
private readonly $color;
|
|
2473
2473
|
private readonly state;
|
|
2474
|
+
/**
|
|
2475
|
+
* Get the color as a hex-color
|
|
2476
|
+
*/
|
|
2474
2477
|
get hex(): string;
|
|
2478
|
+
/**
|
|
2479
|
+
* Set colors from a hex-color
|
|
2480
|
+
*/
|
|
2481
|
+
set hex(value: string);
|
|
2482
|
+
/**
|
|
2483
|
+
* Get the color as an HSL-color
|
|
2484
|
+
*/
|
|
2475
2485
|
get hsl(): HSLColor;
|
|
2486
|
+
set hsl(value: HSLColor);
|
|
2487
|
+
/**
|
|
2488
|
+
* Get the color as an RGB-color
|
|
2489
|
+
*/
|
|
2476
2490
|
get rgb(): RGBColor;
|
|
2491
|
+
set rgb(value: RGBColor);
|
|
2477
2492
|
constructor(value: unknown);
|
|
2478
2493
|
}
|
|
2479
2494
|
/**
|