@oscarpalmer/atoms 0.190.0 → 0.191.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/constants.d.mts +7 -1
- package/dist/color/constants.mjs +7 -1
- package/dist/color/index.d.mts +3 -3
- package/dist/color/index.mjs +2 -2
- package/dist/color/instance.mjs +27 -25
- package/dist/color/misc/alpha.d.mts +1 -1
- package/dist/color/misc/alpha.mjs +10 -7
- package/dist/color/misc/format.d.mts +7 -0
- package/dist/color/misc/format.mjs +20 -0
- package/dist/color/misc/get.mjs +16 -16
- package/dist/color/misc/is.d.mts +0 -36
- package/dist/color/misc/is.mjs +1 -37
- package/dist/color/misc/state.d.mts +3 -2
- package/dist/color/misc/state.mjs +69 -83
- package/dist/color/models.d.mts +28 -5
- package/dist/color/space/hex.d.mts +3 -2
- package/dist/color/space/hex.mjs +13 -1
- package/dist/color/space/hsl.d.mts +3 -2
- package/dist/color/space/hsl.mjs +13 -1
- package/dist/color/space/hwb.d.mts +3 -2
- package/dist/color/space/hwb.mjs +13 -1
- package/dist/color/space/rgb.d.mts +3 -2
- package/dist/color/space/rgb.mjs +14 -2
- package/dist/index.d.mts +23 -29
- package/dist/index.mjs +198 -184
- package/package.json +4 -4
- package/src/color/constants.ts +12 -0
- package/src/color/index.ts +26 -8
- package/src/color/instance.ts +60 -45
- package/src/color/misc/alpha.ts +12 -6
- package/src/color/misc/format.ts +29 -0
- package/src/color/misc/get.ts +20 -13
- package/src/color/misc/is.ts +7 -37
- package/src/color/misc/state.ts +115 -127
- package/src/color/models.ts +32 -4
- package/src/color/space/hex.ts +34 -1
- package/src/color/space/hsl.ts +40 -2
- package/src/color/space/hwb.ts +32 -2
- package/src/color/space/rgb.ts +33 -3
- package/dist/color/misc/index.d.mts +0 -7
- package/dist/color/misc/index.mjs +0 -15
- package/src/color/misc/index.ts +0 -29
|
@@ -2,120 +2,106 @@ import { DEFAULT_HSL, DEFAULT_HWB, DEFAULT_RGB, HEX_BLACK, KEYS_HSL, KEYS_HWB, K
|
|
|
2
2
|
import { getAlpha } from "./alpha.mjs";
|
|
3
3
|
import { getDegrees, getHexValue, getPercentage } from "./get.mjs";
|
|
4
4
|
import { isColor, isHexColor, isHslLike, isHwbLike, isRgbLike } from "./is.mjs";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
5
|
+
import { getColorFromRgb, getRgbValue } from "../space/rgb.mjs";
|
|
6
|
+
import { getColorFromHex, getNormalizedHex } from "../space/hex.mjs";
|
|
7
|
+
import { getColorFromHsl, getHslValue } from "../space/hsl.mjs";
|
|
8
|
+
import { getColorFromHwb, getHwbValue } from "../space/hwb.mjs";
|
|
9
9
|
//#region src/color/misc/state.ts
|
|
10
|
+
function getColorFromState(state, type) {
|
|
11
|
+
const { origin } = state;
|
|
12
|
+
if (type === origin) return state[origin];
|
|
13
|
+
return state[type] ?? getters[origin](state, type);
|
|
14
|
+
}
|
|
10
15
|
function getColorState(value) {
|
|
11
16
|
if (typeof value === "string") return getColorStateForHex(value);
|
|
12
|
-
if (isColor(value))
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
if (isColor(value)) {
|
|
18
|
+
const { origin } = value;
|
|
19
|
+
return {
|
|
20
|
+
origin,
|
|
21
|
+
[origin]: value[origin],
|
|
22
|
+
alpha: getAlpha(value.alpha, false)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
20
25
|
if (typeof value === "object" && value !== null) {
|
|
21
|
-
state
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
const state = {
|
|
27
|
+
alpha: getAlpha(value.alpha, false),
|
|
28
|
+
origin: void 0
|
|
29
|
+
};
|
|
30
|
+
if (KEYS_HSL.every((key) => key in value)) {
|
|
31
|
+
state.hsl = getHslValue(value);
|
|
32
|
+
state.origin = "hsl";
|
|
33
|
+
} else if (KEYS_HWB.every((key) => key in value)) {
|
|
34
|
+
state.hwb = getHwbValue(value);
|
|
35
|
+
state.origin = "hwb";
|
|
36
|
+
} else if (KEYS_RGB.every((key) => key in value)) {
|
|
37
|
+
state.rgb = getRgbValue(value);
|
|
38
|
+
state.origin = "rgb";
|
|
39
|
+
}
|
|
40
|
+
if (state.origin != null) return state;
|
|
25
41
|
}
|
|
26
|
-
return getDefaultColorState(
|
|
42
|
+
return getDefaultColorState();
|
|
27
43
|
}
|
|
28
44
|
function getColorStateForHex(value) {
|
|
29
45
|
const normalized = getNormalizedHex(value, true);
|
|
30
|
-
const hex = normalized.slice(0, 6);
|
|
31
|
-
const hsl = hexToHsl(hex);
|
|
32
46
|
return {
|
|
33
|
-
hex,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
hwb: hslToHwb(hsl),
|
|
37
|
-
rgb: hslToRgb(hsl)
|
|
47
|
+
hex: normalized.slice(0, 6),
|
|
48
|
+
alpha: getAlpha(normalized.slice(6), true),
|
|
49
|
+
origin: "hex"
|
|
38
50
|
};
|
|
39
51
|
}
|
|
40
|
-
function
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
state.hsl = hwbToHsl(state.hwb);
|
|
50
|
-
state.rgb = hwbToRgb(state.hwb);
|
|
51
|
-
state.hex = hwbToHex(state.hwb);
|
|
52
|
-
return state;
|
|
53
|
-
}
|
|
54
|
-
function getColorStateForRgb(state, value) {
|
|
55
|
-
state.rgb = getRgbValue(value);
|
|
56
|
-
state.hex = rgbToHex(state.rgb);
|
|
57
|
-
state.hsl = rgbToHsl(state.rgb);
|
|
58
|
-
state.hwb = rgbToHwb(state.rgb);
|
|
59
|
-
return state;
|
|
52
|
+
function getDefaultColorState() {
|
|
53
|
+
return {
|
|
54
|
+
alpha: getAlpha(100, false),
|
|
55
|
+
hex: HEX_BLACK,
|
|
56
|
+
hsl: { ...DEFAULT_HSL },
|
|
57
|
+
hwb: { ...DEFAULT_HWB },
|
|
58
|
+
rgb: { ...DEFAULT_RGB },
|
|
59
|
+
origin: "hex"
|
|
60
|
+
};
|
|
60
61
|
}
|
|
61
|
-
function
|
|
62
|
-
state.
|
|
63
|
-
state.
|
|
64
|
-
state.
|
|
65
|
-
state.
|
|
66
|
-
state.
|
|
67
|
-
|
|
62
|
+
function setColorValue(state, type, value, alpha) {
|
|
63
|
+
state.hex = void 0;
|
|
64
|
+
state.hsl = void 0;
|
|
65
|
+
state.hwb = void 0;
|
|
66
|
+
state.rgb = void 0;
|
|
67
|
+
state.origin = type;
|
|
68
|
+
state[type] = value;
|
|
69
|
+
if (alpha != null) state.alpha = getAlpha(alpha, type === "hex");
|
|
68
70
|
}
|
|
69
71
|
function setHexColor(state, value, alpha) {
|
|
70
72
|
if (!isHexColor(value) || !alpha && value === state.hex) return;
|
|
71
73
|
const normalized = getNormalizedHex(value, true);
|
|
72
|
-
|
|
73
|
-
const hsl = hexToHsl(hex);
|
|
74
|
-
state.hex = hex;
|
|
75
|
-
state.hsl = hsl;
|
|
76
|
-
state.rgb = hslToRgb(hsl);
|
|
77
|
-
state.hwb = hslToHwb(hsl);
|
|
78
|
-
if (alpha) state.alpha = getAlpha(normalized.slice(6));
|
|
74
|
+
setColorValue(state, "hex", normalized.slice(0, 6), alpha ? normalized.slice(6) : void 0);
|
|
79
75
|
}
|
|
80
76
|
function setHSLColor(state, value, alpha) {
|
|
81
77
|
if (!isHslLike(value)) return;
|
|
82
|
-
|
|
78
|
+
setColorValue(state, "hsl", {
|
|
83
79
|
hue: getDegrees(value.hue),
|
|
84
80
|
saturation: getPercentage(value.saturation),
|
|
85
81
|
lightness: getPercentage(value.lightness)
|
|
86
|
-
};
|
|
87
|
-
state.hsl = hsl;
|
|
88
|
-
state.hex = hslToHex(hsl);
|
|
89
|
-
state.hwb = hslToHwb(hsl);
|
|
90
|
-
state.rgb = hslToRgb(hsl);
|
|
91
|
-
if (alpha) state.alpha = getAlpha(value.alpha);
|
|
82
|
+
}, alpha ? value.alpha : void 0);
|
|
92
83
|
}
|
|
93
84
|
function setHWBColor(state, value, alpha) {
|
|
94
85
|
if (!isHwbLike(value)) return;
|
|
95
|
-
|
|
86
|
+
setColorValue(state, "hwb", {
|
|
96
87
|
hue: getDegrees(value.hue),
|
|
97
88
|
whiteness: getPercentage(value.whiteness),
|
|
98
89
|
blackness: getPercentage(value.blackness)
|
|
99
|
-
};
|
|
100
|
-
state.hwb = hwb;
|
|
101
|
-
state.hex = hwbToHex(hwb);
|
|
102
|
-
state.hsl = hwbToHsl(hwb);
|
|
103
|
-
state.rgb = hwbToRgb(hwb);
|
|
104
|
-
if (alpha) state.alpha = getAlpha(value.alpha);
|
|
90
|
+
}, alpha ? value.alpha : void 0);
|
|
105
91
|
}
|
|
106
92
|
function setRGBColor(state, value, alpha) {
|
|
107
93
|
if (!isRgbLike(value)) return;
|
|
108
|
-
|
|
109
|
-
|
|
94
|
+
setColorValue(state, "rgb", {
|
|
95
|
+
red: getHexValue(value.red),
|
|
110
96
|
green: getHexValue(value.green),
|
|
111
|
-
|
|
112
|
-
};
|
|
113
|
-
const hsl = rgbToHsl(rgb);
|
|
114
|
-
state.hsl = hsl;
|
|
115
|
-
state.rgb = rgb;
|
|
116
|
-
state.hex = hslToHex(hsl);
|
|
117
|
-
state.hwb = hslToHwb(hsl);
|
|
118
|
-
if (alpha) state.alpha = getAlpha(value.alpha);
|
|
97
|
+
blue: getHexValue(value.blue)
|
|
98
|
+
}, alpha ? value.alpha : void 0);
|
|
119
99
|
}
|
|
100
|
+
const getters = {
|
|
101
|
+
hex: getColorFromHex,
|
|
102
|
+
hsl: getColorFromHsl,
|
|
103
|
+
hwb: getColorFromHwb,
|
|
104
|
+
rgb: getColorFromRgb
|
|
105
|
+
};
|
|
120
106
|
//#endregion
|
|
121
|
-
export { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
|
|
107
|
+
export { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
|
package/dist/color/models.d.mts
CHANGED
|
@@ -101,6 +101,12 @@ type Color = {
|
|
|
101
101
|
* @param value New _HWBA_ color
|
|
102
102
|
*/
|
|
103
103
|
set hwba(value: HWBAColor);
|
|
104
|
+
/**
|
|
105
|
+
* Last color format that was set on the color
|
|
106
|
+
*
|
|
107
|
+
* @returns Color type
|
|
108
|
+
*/
|
|
109
|
+
get origin(): ColorType;
|
|
104
110
|
/**
|
|
105
111
|
* Get the color as an _RGB_ color
|
|
106
112
|
*
|
|
@@ -226,10 +232,27 @@ type RGBColor = {
|
|
|
226
232
|
type ColorProperty = 'alpha' | keyof HSLColor | keyof HWBColor | keyof RGBColor;
|
|
227
233
|
type ColorState = {
|
|
228
234
|
alpha: Alpha;
|
|
229
|
-
hex
|
|
230
|
-
hsl
|
|
231
|
-
hwb
|
|
232
|
-
|
|
235
|
+
hex?: string;
|
|
236
|
+
hsl?: HSLColor;
|
|
237
|
+
hwb?: HWBColor;
|
|
238
|
+
origin: ColorType;
|
|
239
|
+
rgb?: RGBColor;
|
|
233
240
|
};
|
|
241
|
+
/**
|
|
242
|
+
* Base color types
|
|
243
|
+
*/
|
|
244
|
+
type ColorType = 'hex' | 'hsl' | 'hwb' | 'rgb';
|
|
245
|
+
/**
|
|
246
|
+
* Extended color types
|
|
247
|
+
*/
|
|
248
|
+
type ColorTypeExtended = ColorType | 'hsla' | 'hwba' | 'rgba';
|
|
249
|
+
/**
|
|
250
|
+
* Base color values
|
|
251
|
+
*/
|
|
252
|
+
type ColorValue = string | HSLColor | HWBColor | RGBColor;
|
|
253
|
+
/**
|
|
254
|
+
* Extended color values
|
|
255
|
+
*/
|
|
256
|
+
type ColorValueExtended = ColorValue | HSLAColor | HWBAColor | RGBAColor;
|
|
234
257
|
//#endregion
|
|
235
|
-
export { Alpha, Color, ColorProperty, ColorState, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor };
|
|
258
|
+
export { Alpha, Color, ColorProperty, ColorState, ColorType, ColorTypeExtended, ColorValue, ColorValueExtended, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
1
|
+
import { ColorState, ColorType, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
2
2
|
//#region src/color/space/hex.d.ts
|
|
3
|
+
declare function getColorFromHex<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
|
|
3
4
|
/**
|
|
4
5
|
* Get the normalized hex color from a value
|
|
5
6
|
*
|
|
@@ -65,4 +66,4 @@ declare function hexToRgb(value: string): RGBColor;
|
|
|
65
66
|
*/
|
|
66
67
|
declare function hexToRgba(value: string): RGBAColor;
|
|
67
68
|
//#endregion
|
|
68
|
-
export { getNormalizedHex, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba };
|
|
69
|
+
export { getColorFromHex, getNormalizedHex, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba };
|
package/dist/color/space/hex.mjs
CHANGED
|
@@ -17,6 +17,18 @@ function convertHexToRgba(value) {
|
|
|
17
17
|
red: getHexValue(values[0])
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
|
+
function getColorFromHex(state, type) {
|
|
21
|
+
switch (type) {
|
|
22
|
+
case "hsl":
|
|
23
|
+
state.hsl ??= hexToHsl(state.hex);
|
|
24
|
+
break;
|
|
25
|
+
case "hwb":
|
|
26
|
+
state.hwb ??= hexToHwb(state.hex);
|
|
27
|
+
break;
|
|
28
|
+
case "rgb": state.rgb ??= hexToRgb(state.hex);
|
|
29
|
+
}
|
|
30
|
+
return state[type];
|
|
31
|
+
}
|
|
20
32
|
/**
|
|
21
33
|
* Get the normalized hex color from a value
|
|
22
34
|
*
|
|
@@ -119,4 +131,4 @@ function hexToRgba(value) {
|
|
|
119
131
|
return convertHexToRgba(value);
|
|
120
132
|
}
|
|
121
133
|
//#endregion
|
|
122
|
-
export { getNormalizedHex, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba };
|
|
134
|
+
export { getColorFromHex, getNormalizedHex, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
1
|
+
import { ColorState, ColorType, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
2
2
|
//#region src/color/space/hsl.d.ts
|
|
3
|
+
declare function getColorFromHsl<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
|
|
3
4
|
declare function getHslValue(value: Record<keyof HSLColor, unknown>): HSLColor;
|
|
4
5
|
/**
|
|
5
6
|
* Convert an _HSL(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
|
|
@@ -52,4 +53,4 @@ declare function hslToRgb(hsl: HSLAColor | HSLColor): RGBColor;
|
|
|
52
53
|
*/
|
|
53
54
|
declare function hslToRgba(hsl: HSLAColor | HSLColor): RGBAColor;
|
|
54
55
|
//#endregion
|
|
55
|
-
export { getHslValue, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba };
|
|
56
|
+
export { getColorFromHsl, getHslValue, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba };
|
package/dist/color/space/hsl.mjs
CHANGED
|
@@ -31,6 +31,18 @@ function convertHslToRgba(input) {
|
|
|
31
31
|
red: getHexValue(getHexyValue(hue, lightness, saturation, 0))
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
+
function getColorFromHsl(state, type) {
|
|
35
|
+
switch (type) {
|
|
36
|
+
case "hex":
|
|
37
|
+
state.hex = hslToHex(state.hsl);
|
|
38
|
+
break;
|
|
39
|
+
case "hwb":
|
|
40
|
+
state.hwb = hslToHwb(state.hsl);
|
|
41
|
+
break;
|
|
42
|
+
case "rgb": state.rgb = hslToRgb(state.hsl);
|
|
43
|
+
}
|
|
44
|
+
return state[type];
|
|
45
|
+
}
|
|
34
46
|
function getHexyValue(hue, lightness, saturation, value) {
|
|
35
47
|
const part = (value + hue / 30) % 12;
|
|
36
48
|
return (lightness - saturation * Math.min(lightness, 1 - lightness) * Math.max(-1, Math.min(part - 3, 9 - part, 1))) * 255;
|
|
@@ -113,4 +125,4 @@ function hslToRgba(hsl) {
|
|
|
113
125
|
return convertHslToRgba(hsl);
|
|
114
126
|
}
|
|
115
127
|
//#endregion
|
|
116
|
-
export { getHslValue, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba };
|
|
128
|
+
export { getColorFromHsl, getHslValue, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
1
|
+
import { ColorState, ColorType, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
2
2
|
//#region src/color/space/hwb.d.ts
|
|
3
3
|
declare function convertHwbToHsl(input: HWBAColor | HWBColor): HSLAColor;
|
|
4
|
+
declare function getColorFromHwb<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
|
|
4
5
|
declare function getHwbValue(value: Record<keyof HWBColor, unknown>): HWBColor;
|
|
5
6
|
/**
|
|
6
7
|
* Convert an _HWB(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
|
|
@@ -49,4 +50,4 @@ declare function hwbToRgb(hwb: HWBAColor | HWBColor): RGBColor;
|
|
|
49
50
|
*/
|
|
50
51
|
declare function hwbToRgba(hwb: HWBAColor | HWBColor): RGBAColor;
|
|
51
52
|
//#endregion
|
|
52
|
-
export { convertHwbToHsl, getHwbValue, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba };
|
|
53
|
+
export { convertHwbToHsl, getColorFromHwb, getHwbValue, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba };
|
package/dist/color/space/hwb.mjs
CHANGED
|
@@ -22,6 +22,18 @@ function convertHwbToHsl(input) {
|
|
|
22
22
|
alpha: getAlphaValue(input?.alpha ?? 100)
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
|
+
function getColorFromHwb(state, type) {
|
|
26
|
+
switch (type) {
|
|
27
|
+
case "hex":
|
|
28
|
+
state.hex = hwbToHex(state.hwb);
|
|
29
|
+
break;
|
|
30
|
+
case "hsl":
|
|
31
|
+
state.hsl = hwbToHsl(state.hwb);
|
|
32
|
+
break;
|
|
33
|
+
case "rgb": state.rgb = hwbToRgb(state.hwb);
|
|
34
|
+
}
|
|
35
|
+
return state[type];
|
|
36
|
+
}
|
|
25
37
|
function getHwbSaturation(blackness, whiteness, lightness) {
|
|
26
38
|
const hue = 1 - blackness - whiteness;
|
|
27
39
|
if (hue <= 0) return 0;
|
|
@@ -96,4 +108,4 @@ function hwbToRgba(hwb) {
|
|
|
96
108
|
return hslToRgba(hwbToHsla(hwb));
|
|
97
109
|
}
|
|
98
110
|
//#endregion
|
|
99
|
-
export { convertHwbToHsl, getHwbValue, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba };
|
|
111
|
+
export { convertHwbToHsl, getColorFromHwb, getHwbValue, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
1
|
+
import { ColorState, ColorType, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
2
2
|
//#region src/color/space/rgb.d.ts
|
|
3
3
|
declare function convertRgbToHex(rgb: RGBAColor | RGBColor, alpha: boolean): string;
|
|
4
4
|
declare function convertRgbToHsla(value: unknown): HSLAColor;
|
|
5
5
|
declare function convertRgbToHwba(value: unknown): HWBAColor;
|
|
6
|
+
declare function getColorFromRgb<Type extends ColorType>(state: ColorState, type: Type): NonNullable<ColorState[Type]>;
|
|
6
7
|
declare function getRgbValue(value: Record<keyof RGBColor, unknown>): RGBColor;
|
|
7
8
|
/**
|
|
8
9
|
* Convert an _RGB(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
|
|
@@ -58,4 +59,4 @@ declare function rgbToHwb(rgb: RGBAColor | RGBColor): HWBColor;
|
|
|
58
59
|
*/
|
|
59
60
|
declare function rgbToHwba(rgb: RGBAColor | RGBColor): HWBAColor;
|
|
60
61
|
//#endregion
|
|
61
|
-
export { convertRgbToHex, convertRgbToHsla, convertRgbToHwba, getRgbValue, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba };
|
|
62
|
+
export { convertRgbToHex, convertRgbToHsla, convertRgbToHwba, getColorFromRgb, getRgbValue, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba };
|
package/dist/color/space/rgb.mjs
CHANGED
|
@@ -14,7 +14,7 @@ function convertRgbToHex(rgb, alpha) {
|
|
|
14
14
|
return hexString.length === 1 ? `0${hexString}` : hexString;
|
|
15
15
|
}))}`;
|
|
16
16
|
let a = "";
|
|
17
|
-
if (typeof alpha === "boolean" && alpha) a = getAlpha(rgb.alpha).hex;
|
|
17
|
+
if (typeof alpha === "boolean" && alpha) a = getAlpha(rgb.alpha, false).hex;
|
|
18
18
|
return `${hex}${a}`;
|
|
19
19
|
}
|
|
20
20
|
function convertRgbToHsla(value) {
|
|
@@ -50,6 +50,18 @@ function convertRgbToHwba(value) {
|
|
|
50
50
|
alpha: getAlphaValue(value?.alpha ?? 100)
|
|
51
51
|
};
|
|
52
52
|
}
|
|
53
|
+
function getColorFromRgb(state, type) {
|
|
54
|
+
switch (type) {
|
|
55
|
+
case "hex":
|
|
56
|
+
state.hex = rgbToHex(state.rgb);
|
|
57
|
+
break;
|
|
58
|
+
case "hsl":
|
|
59
|
+
state.hsl = rgbToHsl(state.rgb);
|
|
60
|
+
break;
|
|
61
|
+
case "hwb": state.hwb = rgbToHwb(state.rgb);
|
|
62
|
+
}
|
|
63
|
+
return state[type];
|
|
64
|
+
}
|
|
53
65
|
function getRgbHue(values) {
|
|
54
66
|
const { blue, delta, green, max, red } = values;
|
|
55
67
|
switch (max) {
|
|
@@ -155,4 +167,4 @@ function rgbToHwba(rgb) {
|
|
|
155
167
|
return convertRgbToHwba(rgb);
|
|
156
168
|
}
|
|
157
169
|
//#endregion
|
|
158
|
-
export { convertRgbToHex, convertRgbToHsla, convertRgbToHwba, getRgbValue, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba };
|
|
170
|
+
export { convertRgbToHex, convertRgbToHsla, convertRgbToHwba, getColorFromRgb, getRgbValue, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba };
|
package/dist/index.d.mts
CHANGED
|
@@ -6081,6 +6081,12 @@ type Color = {
|
|
|
6081
6081
|
* @param value New _HWBA_ color
|
|
6082
6082
|
*/
|
|
6083
6083
|
set hwba(value: HWBAColor);
|
|
6084
|
+
/**
|
|
6085
|
+
* Last color format that was set on the color
|
|
6086
|
+
*
|
|
6087
|
+
* @returns Color type
|
|
6088
|
+
*/
|
|
6089
|
+
get origin(): ColorType;
|
|
6084
6090
|
/**
|
|
6085
6091
|
* Get the color as an _RGB_ color
|
|
6086
6092
|
*
|
|
@@ -6203,6 +6209,22 @@ type RGBColor = {
|
|
|
6203
6209
|
*/
|
|
6204
6210
|
red: number;
|
|
6205
6211
|
};
|
|
6212
|
+
/**
|
|
6213
|
+
* Base color types
|
|
6214
|
+
*/
|
|
6215
|
+
type ColorType = 'hex' | 'hsl' | 'hwb' | 'rgb';
|
|
6216
|
+
/**
|
|
6217
|
+
* Extended color types
|
|
6218
|
+
*/
|
|
6219
|
+
type ColorTypeExtended = ColorType | 'hsla' | 'hwba' | 'rgba';
|
|
6220
|
+
/**
|
|
6221
|
+
* Base color values
|
|
6222
|
+
*/
|
|
6223
|
+
type ColorValue = string | HSLColor | HWBColor | RGBColor;
|
|
6224
|
+
/**
|
|
6225
|
+
* Extended color values
|
|
6226
|
+
*/
|
|
6227
|
+
type ColorValueExtended = ColorValue | HSLAColor | HWBAColor | RGBAColor;
|
|
6206
6228
|
//#endregion
|
|
6207
6229
|
//#region src/color/misc/get.d.ts
|
|
6208
6230
|
/**
|
|
@@ -6345,13 +6367,6 @@ declare function isHslaColor(value: unknown): value is HSLAColor;
|
|
|
6345
6367
|
* @returns `true` if the value is an _HSL_ color, otherwise `false`
|
|
6346
6368
|
*/
|
|
6347
6369
|
declare function isHslColor(value: unknown): value is HSLColor;
|
|
6348
|
-
/**
|
|
6349
|
-
* Is the value like an _HSL_ color?
|
|
6350
|
-
*
|
|
6351
|
-
* @param value Value to check
|
|
6352
|
-
* @returns `true` if the value is like an _HSL_ color, otherwise `false`
|
|
6353
|
-
*/
|
|
6354
|
-
declare function isHslLike(value: unknown): value is Record<keyof HSLColor, unknown>;
|
|
6355
6370
|
/**
|
|
6356
6371
|
* Is the value an _HWBA_ color?
|
|
6357
6372
|
*
|
|
@@ -6359,13 +6374,6 @@ declare function isHslLike(value: unknown): value is Record<keyof HSLColor, unkn
|
|
|
6359
6374
|
* @returns `true` if the value is an _HWBA_ color, otherwise `false`
|
|
6360
6375
|
*/
|
|
6361
6376
|
declare function isHwbaColor(value: unknown): value is HWBAColor;
|
|
6362
|
-
/**
|
|
6363
|
-
* Is the value like an _HWBA_ color?
|
|
6364
|
-
*
|
|
6365
|
-
* @param value Value to check
|
|
6366
|
-
* @returns `true` if the value is like an _HWBA_ color, otherwise `false`
|
|
6367
|
-
*/
|
|
6368
|
-
declare function isHwbaLike(value: unknown): value is Record<keyof HWBAColor, unknown>;
|
|
6369
6377
|
/**
|
|
6370
6378
|
* Is the value an _HWB_ color?
|
|
6371
6379
|
*
|
|
@@ -6373,13 +6381,6 @@ declare function isHwbaLike(value: unknown): value is Record<keyof HWBAColor, un
|
|
|
6373
6381
|
* @returns `true` if the value is an _HWB_ color, otherwise `false`
|
|
6374
6382
|
*/
|
|
6375
6383
|
declare function isHwbColor(value: unknown): value is HWBColor;
|
|
6376
|
-
/**
|
|
6377
|
-
* Is the value like an _HWB_ color?
|
|
6378
|
-
*
|
|
6379
|
-
* @param value Value to check
|
|
6380
|
-
* @returns `true` if the value is like an _HWB_ color, otherwise `false`
|
|
6381
|
-
*/
|
|
6382
|
-
declare function isHwbLike(value: unknown): value is Record<keyof HWBColor, unknown>;
|
|
6383
6384
|
/**
|
|
6384
6385
|
* Is the value an _RGBA_ color?
|
|
6385
6386
|
*
|
|
@@ -6394,13 +6395,6 @@ declare function isRgbaColor(value: unknown): value is RGBAColor;
|
|
|
6394
6395
|
* @returns `true` if the value is an _RGB_ color, otherwise `false`
|
|
6395
6396
|
*/
|
|
6396
6397
|
declare function isRgbColor(value: unknown): value is RGBColor;
|
|
6397
|
-
/**
|
|
6398
|
-
* Is the value like an _RGB_ color?
|
|
6399
|
-
*
|
|
6400
|
-
* @param value Value to check
|
|
6401
|
-
* @returns `true` if the value is like an _RGB_ color, otherwise `false`
|
|
6402
|
-
*/
|
|
6403
|
-
declare function isRgbLike(value: unknown): value is Record<keyof RGBColor, unknown>;
|
|
6404
6398
|
//#endregion
|
|
6405
6399
|
//#region src/color/space/hex.d.ts
|
|
6406
6400
|
/**
|
|
@@ -8442,4 +8436,4 @@ declare class SizedSet<Value = unknown> extends Set<Value> {
|
|
|
8442
8436
|
get(value: Value, update?: boolean): Value | undefined;
|
|
8443
8437
|
}
|
|
8444
8438
|
//#endregion
|
|
8445
|
-
export { AnyResult, ArrayComparison, ArrayComparisonSorter, ArrayKeySorter, ArrayOrPlainObject, ArrayValueSorter, AssertProperty, Asserter, AssignOptions, Assigner, AsyncCancelableCallback, AttemptFlow, AttemptFlowPromise, Beacon, BeaconOptions, BuiltIns, CancelableCallback, CancelablePromise, type Color, Constructor, DiffOptions, DiffResult, DiffValue, EqualOptions, Err, EventPosition, Events, ExtendedErr, ExtendedResult, Flow, FlowPromise, Frozen, FulfilledPromise, Fuzzy, FuzzyConfiguration, FuzzyOptions, FuzzyResult, FuzzySearchOptions, GenericAsyncCallback, GenericCallback, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Herald, Key$1 as Key, KeyedQueue, KeyedValue, Logger, type Memoized, type MemoizedOptions, MergeOptions, Merger, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NormalizeOptions, Normalizer, NumericalKeys, NumericalValues, Observable, Ok, OnceAsyncCallback, OnceCallback, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PlainObject, Primitive, PromiseData, PromiseHandlers, PromiseOptions, PromiseParameters, PromiseStrategy, PromiseTimeoutError, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValue, PromisesValues, Queue, QueueError, QueueOptions, Queued, QueuedResult, type RGBAColor, type RGBColor, RejectedPromise, RequiredKeys, Result, ResultMatch, RetryError, RetryOptions, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, type Shaken, Simplify, SizedMap, SizedSet, type Smushed, SortDirection, Sorter, Subscription, TemplateOptions, type TimedLogger, ToString, Transformer, TypedArray, UnionToIntersection, type Unsmushed, Unsubscriber, UnwrapValue, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isBeacon, isColor, isConstructor, isEmpty, isError, isEvents, isFrozen, isFulfilled, isHerald, isHexColor, isHslColor,
|
|
8439
|
+
export { AnyResult, ArrayComparison, ArrayComparisonSorter, ArrayKeySorter, ArrayOrPlainObject, ArrayValueSorter, AssertProperty, Asserter, AssignOptions, Assigner, AsyncCancelableCallback, AttemptFlow, AttemptFlowPromise, Beacon, BeaconOptions, BuiltIns, CancelableCallback, CancelablePromise, type Color, type ColorType, type ColorTypeExtended, type ColorValue, type ColorValueExtended, Constructor, DiffOptions, DiffResult, DiffValue, EqualOptions, Err, EventPosition, Events, ExtendedErr, ExtendedResult, Flow, FlowPromise, Frozen, FulfilledPromise, Fuzzy, FuzzyConfiguration, FuzzyOptions, FuzzyResult, FuzzySearchOptions, GenericAsyncCallback, GenericCallback, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Herald, Key$1 as Key, KeyedQueue, KeyedValue, Logger, type Memoized, type MemoizedOptions, MergeOptions, Merger, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NormalizeOptions, Normalizer, NumericalKeys, NumericalValues, Observable, Ok, OnceAsyncCallback, OnceCallback, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PlainObject, Primitive, PromiseData, PromiseHandlers, PromiseOptions, PromiseParameters, PromiseStrategy, PromiseTimeoutError, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValue, PromisesValues, Queue, QueueError, QueueOptions, Queued, QueuedResult, type RGBAColor, type RGBColor, RejectedPromise, RequiredKeys, Result, ResultMatch, RetryError, RetryOptions, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, type Shaken, Simplify, SizedMap, SizedSet, type Smushed, SortDirection, Sorter, Subscription, TemplateOptions, type TimedLogger, ToString, Transformer, TypedArray, UnionToIntersection, type Unsmushed, Unsubscriber, UnwrapValue, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isBeacon, isColor, isConstructor, isEmpty, isError, isEvents, isFrozen, isFulfilled, isHerald, isHexColor, isHslColor, isHslaColor, isHwbColor, isHwbaColor, isInstanceOf, isKey, isKeyedQueue, isLogger, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isObservable, isOk, isPlainObject, isPrimitive, isQueue, isQueueInstance, isRejected, isResult, isRgbColor, isRgbaColor, isSorted, isSubscription, isTimedLogger, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, lowerCase, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba, round, select, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|