@oscarpalmer/atoms 0.189.1 → 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/beacon.d.mts +51 -31
- package/dist/beacon.mjs +163 -141
- package/dist/color/constants.d.mts +7 -1
- package/dist/color/constants.mjs +7 -1
- package/dist/color/index.d.mts +3 -4
- package/dist/color/index.mjs +4 -4
- package/dist/color/instance.d.mts +3 -167
- package/dist/color/instance.mjs +62 -219
- 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.d.mts +1 -2
- package/dist/color/misc/get.mjs +19 -19
- package/dist/color/misc/is.d.mts +1 -38
- 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 +185 -6
- 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/function/memoize.d.mts +3 -9
- package/dist/function/memoize.mjs +43 -90
- package/dist/function/once.mjs +16 -4
- package/dist/herald.d.mts +19 -15
- package/dist/herald.mjs +79 -89
- package/dist/index.d.mts +195 -190
- package/dist/index.mjs +918 -1149
- package/dist/internal/result.d.mts +6 -6
- package/dist/internal/result.mjs +1 -1
- package/dist/internal/value/has.d.mts +2 -2
- package/dist/logger.d.mts +7 -7
- package/dist/logger.mjs +72 -51
- package/dist/promise/helpers.d.mts +2 -2
- package/dist/promise/helpers.mjs +2 -2
- package/dist/queue.d.mts +24 -12
- package/dist/queue.mjs +288 -346
- package/dist/string/fuzzy.d.mts +4 -13
- package/dist/string/fuzzy.mjs +15 -41
- package/package.json +3 -3
- package/src/beacon.ts +262 -160
- package/src/color/constants.ts +12 -0
- package/src/color/index.ts +32 -14
- package/src/color/instance.ts +87 -248
- package/src/color/misc/alpha.ts +12 -6
- package/src/color/misc/format.ts +29 -0
- package/src/color/misc/get.ts +24 -17
- package/src/color/misc/is.ts +7 -37
- package/src/color/misc/state.ts +115 -127
- package/src/color/models.ts +212 -6
- 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/src/function/memoize.ts +74 -65
- package/src/function/once.ts +4 -0
- package/src/herald.ts +136 -94
- package/src/internal/result.ts +6 -6
- package/src/internal/value/has.ts +2 -2
- package/src/logger.ts +93 -42
- package/src/promise/helpers.ts +2 -2
- package/src/queue.ts +480 -375
- package/src/string/fuzzy.ts +36 -31
- package/dist/color/misc/index.d.mts +0 -7
- package/dist/color/misc/index.mjs +0 -15
- package/src/color/misc/index.ts +0 -30
|
@@ -1,169 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Color } from "./models.mjs";
|
|
2
2
|
//#region src/color/instance.d.ts
|
|
3
|
-
|
|
4
|
-
* A color that is represented in multiple color formats
|
|
5
|
-
*/
|
|
6
|
-
declare class Color {
|
|
7
|
-
#private;
|
|
8
|
-
/**
|
|
9
|
-
* A property to identify this as a Color instance, used for type checking
|
|
10
|
-
*
|
|
11
|
-
* @internal
|
|
12
|
-
*/
|
|
13
|
-
private readonly $color;
|
|
14
|
-
/**
|
|
15
|
-
* Get the alpha channel _(opacity)_ of the color as a percentage between `0` and `100`
|
|
16
|
-
*
|
|
17
|
-
* @returns Current alpha channel value
|
|
18
|
-
*/
|
|
19
|
-
get alpha(): number;
|
|
20
|
-
/**
|
|
21
|
-
* Set the alpha channel _(opacity)_ of the color as a percentage between `0` and `100`
|
|
22
|
-
*
|
|
23
|
-
* @param value New alpha channel value
|
|
24
|
-
*/
|
|
25
|
-
set alpha(value: number);
|
|
26
|
-
/**
|
|
27
|
-
* Get the color as a hex color string
|
|
28
|
-
*
|
|
29
|
-
* _Hex color string is returned with no `#`-prefix or alpha channel (opacity)_
|
|
30
|
-
*
|
|
31
|
-
* @returns Current color as a hex color string
|
|
32
|
-
*/
|
|
33
|
-
get hex(): string;
|
|
34
|
-
/**
|
|
35
|
-
* Set the color from a hex color string
|
|
36
|
-
*
|
|
37
|
-
* - `#`-prefix is optional
|
|
38
|
-
* - Alpha channel _(opacity)_ will be ignored
|
|
39
|
-
*
|
|
40
|
-
* @param value New hex color string
|
|
41
|
-
*/
|
|
42
|
-
set hex(value: string);
|
|
43
|
-
/**
|
|
44
|
-
* Get the color as a hex color with an alpha channel _(opacity)_
|
|
45
|
-
*
|
|
46
|
-
* _Hex color string is returned with alpha channel (opacity), but without `#`-prefix_
|
|
47
|
-
*
|
|
48
|
-
* @returns Current color as a hex color string
|
|
49
|
-
*/
|
|
50
|
-
get hexa(): string;
|
|
51
|
-
/**
|
|
52
|
-
* Set the color from a hex color string with an alpha channel _(opacity)_
|
|
53
|
-
*
|
|
54
|
-
* - `#`-prefix is optional
|
|
55
|
-
* - Alpha channel _(opacity)_ will be respected
|
|
56
|
-
*
|
|
57
|
-
* @param value New hex color string
|
|
58
|
-
*/
|
|
59
|
-
set hexa(value: string);
|
|
60
|
-
/**
|
|
61
|
-
* Get the color as an _HSL_ color
|
|
62
|
-
*
|
|
63
|
-
* @returns Current color as an _HSL_ color
|
|
64
|
-
*/
|
|
65
|
-
get hsl(): HSLColor;
|
|
66
|
-
/**
|
|
67
|
-
* Set colors from an _HSL_ color
|
|
68
|
-
*
|
|
69
|
-
* @param value New _HSL_ color
|
|
70
|
-
*/
|
|
71
|
-
set hsl(value: HSLColor);
|
|
72
|
-
/**
|
|
73
|
-
* Get the color as an _HSLA_ color
|
|
74
|
-
*
|
|
75
|
-
* @returns Current color as an _HSLA_ color
|
|
76
|
-
*/
|
|
77
|
-
get hsla(): HSLAColor;
|
|
78
|
-
/**
|
|
79
|
-
* Set colors and alpha from an _HSLA_ color
|
|
80
|
-
*
|
|
81
|
-
* @param value New _HSLA_ color
|
|
82
|
-
*/
|
|
83
|
-
set hsla(value: HSLAColor);
|
|
84
|
-
/**
|
|
85
|
-
* Get the color as an _HWB_ color
|
|
86
|
-
*
|
|
87
|
-
* @returns Current color as an _HWB_ color
|
|
88
|
-
*/
|
|
89
|
-
get hwb(): HWBColor;
|
|
90
|
-
/**
|
|
91
|
-
* Set colors from an _HWB_ color
|
|
92
|
-
*
|
|
93
|
-
* @param value New _HWB_ color
|
|
94
|
-
*/
|
|
95
|
-
set hwb(value: HWBColor);
|
|
96
|
-
/**
|
|
97
|
-
* Get the color as an _HWBA_ color
|
|
98
|
-
*
|
|
99
|
-
* @returns Current color as an _HWBA_ color
|
|
100
|
-
*/
|
|
101
|
-
get hwba(): HWBAColor;
|
|
102
|
-
/**
|
|
103
|
-
* Set colors and alpha from an _HWBA_ color
|
|
104
|
-
*
|
|
105
|
-
* @param value New _HWBA_ color
|
|
106
|
-
*/
|
|
107
|
-
set hwba(value: HWBAColor);
|
|
108
|
-
/**
|
|
109
|
-
* Get the color as an _RGB_ color
|
|
110
|
-
*
|
|
111
|
-
* @returns Current color as an _RGB_ color
|
|
112
|
-
*/
|
|
113
|
-
get rgb(): RGBColor;
|
|
114
|
-
/**
|
|
115
|
-
* Set colors from an _RGB_ color
|
|
116
|
-
*
|
|
117
|
-
* @param value New _RGB_ color
|
|
118
|
-
*/
|
|
119
|
-
set rgb(value: RGBColor);
|
|
120
|
-
/**
|
|
121
|
-
* Get the color as an _RGBA_ color
|
|
122
|
-
*
|
|
123
|
-
* @returns Current color as an _RGBA_ color
|
|
124
|
-
*/
|
|
125
|
-
get rgba(): RGBAColor;
|
|
126
|
-
/**
|
|
127
|
-
* Set colors and alpha from an _RGBA_ color
|
|
128
|
-
*
|
|
129
|
-
* @param value New _RGBA_ color
|
|
130
|
-
*/
|
|
131
|
-
set rgba(value: RGBAColor);
|
|
132
|
-
constructor(value: unknown);
|
|
133
|
-
/**
|
|
134
|
-
* Get the color as a hex string
|
|
135
|
-
*
|
|
136
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
137
|
-
* @returns Hex color string
|
|
138
|
-
*/
|
|
139
|
-
toHexString(alpha?: boolean): string;
|
|
140
|
-
/**
|
|
141
|
-
* Get the color as an _HSL(A)_ string
|
|
142
|
-
*
|
|
143
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
144
|
-
* @returns _HSL(A)_ color string
|
|
145
|
-
*/
|
|
146
|
-
toHslString(alpha?: boolean): string;
|
|
147
|
-
/**
|
|
148
|
-
* Get the color as an _HWB(A)_ string
|
|
149
|
-
*
|
|
150
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
151
|
-
* @returns _HWB(A)_ color string
|
|
152
|
-
*/
|
|
153
|
-
toHwbString(alpha?: boolean): string;
|
|
154
|
-
/**
|
|
155
|
-
* Get the color as an _RGB(A)_ string
|
|
156
|
-
*
|
|
157
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
158
|
-
* @returns _RGB(A)_ color string
|
|
159
|
-
*/
|
|
160
|
-
toRgbString(alpha?: boolean): string;
|
|
161
|
-
/**
|
|
162
|
-
* Get the color as a hex color string
|
|
163
|
-
*
|
|
164
|
-
* @returns Hex color string
|
|
165
|
-
*/
|
|
166
|
-
toString(): string;
|
|
167
|
-
}
|
|
3
|
+
declare function color(value: unknown): Color;
|
|
168
4
|
//#endregion
|
|
169
|
-
export {
|
|
5
|
+
export { color };
|
package/dist/color/instance.mjs
CHANGED
|
@@ -1,223 +1,66 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PROPERTY_COLOR } from "./constants.mjs";
|
|
2
2
|
import { getAlpha } from "./misc/alpha.mjs";
|
|
3
|
-
import { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
|
|
3
|
+
import { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor } from "./misc/state.mjs";
|
|
4
|
+
import { formatHslColor, formatHwbColor, formatRgbColor } from "./misc/format.mjs";
|
|
4
5
|
//#region src/color/instance.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
* @param value New hex color string
|
|
64
|
-
*/
|
|
65
|
-
set hexa(value) {
|
|
66
|
-
setHexColor(this.#state, value, true);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Get the color as an _HSL_ color
|
|
70
|
-
*
|
|
71
|
-
* @returns Current color as an _HSL_ color
|
|
72
|
-
*/
|
|
73
|
-
get hsl() {
|
|
74
|
-
return { ...this.#state.hsl };
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Set colors from an _HSL_ color
|
|
78
|
-
*
|
|
79
|
-
* @param value New _HSL_ color
|
|
80
|
-
*/
|
|
81
|
-
set hsl(value) {
|
|
82
|
-
setHSLColor(this.#state, value, false);
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Get the color as an _HSLA_ color
|
|
86
|
-
*
|
|
87
|
-
* @returns Current color as an _HSLA_ color
|
|
88
|
-
*/
|
|
89
|
-
get hsla() {
|
|
90
|
-
return {
|
|
91
|
-
...this.#state.hsl,
|
|
92
|
-
alpha: this.#state.alpha.value
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Set colors and alpha from an _HSLA_ color
|
|
97
|
-
*
|
|
98
|
-
* @param value New _HSLA_ color
|
|
99
|
-
*/
|
|
100
|
-
set hsla(value) {
|
|
101
|
-
setHSLColor(this.#state, value, true);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Get the color as an _HWB_ color
|
|
105
|
-
*
|
|
106
|
-
* @returns Current color as an _HWB_ color
|
|
107
|
-
*/
|
|
108
|
-
get hwb() {
|
|
109
|
-
return { ...this.#state.hwb };
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Set colors from an _HWB_ color
|
|
113
|
-
*
|
|
114
|
-
* @param value New _HWB_ color
|
|
115
|
-
*/
|
|
116
|
-
set hwb(value) {
|
|
117
|
-
setHWBColor(this.#state, value, false);
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Get the color as an _HWBA_ color
|
|
121
|
-
*
|
|
122
|
-
* @returns Current color as an _HWBA_ color
|
|
123
|
-
*/
|
|
124
|
-
get hwba() {
|
|
125
|
-
return {
|
|
126
|
-
...this.#state.hwb,
|
|
127
|
-
alpha: this.#state.alpha.value
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Set colors and alpha from an _HWBA_ color
|
|
132
|
-
*
|
|
133
|
-
* @param value New _HWBA_ color
|
|
134
|
-
*/
|
|
135
|
-
set hwba(value) {
|
|
136
|
-
setHWBColor(this.#state, value, true);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Get the color as an _RGB_ color
|
|
140
|
-
*
|
|
141
|
-
* @returns Current color as an _RGB_ color
|
|
142
|
-
*/
|
|
143
|
-
get rgb() {
|
|
144
|
-
return { ...this.#state.rgb };
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Set colors from an _RGB_ color
|
|
148
|
-
*
|
|
149
|
-
* @param value New _RGB_ color
|
|
150
|
-
*/
|
|
151
|
-
set rgb(value) {
|
|
152
|
-
setRGBColor(this.#state, value, false);
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Get the color as an _RGBA_ color
|
|
156
|
-
*
|
|
157
|
-
* @returns Current color as an _RGBA_ color
|
|
158
|
-
*/
|
|
159
|
-
get rgba() {
|
|
160
|
-
return {
|
|
161
|
-
...this.#state.rgb,
|
|
162
|
-
alpha: this.#state.alpha.value
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Set colors and alpha from an _RGBA_ color
|
|
167
|
-
*
|
|
168
|
-
* @param value New _RGBA_ color
|
|
169
|
-
*/
|
|
170
|
-
set rgba(value) {
|
|
171
|
-
setRGBColor(this.#state, value, true);
|
|
172
|
-
}
|
|
173
|
-
constructor(value) {
|
|
174
|
-
this.#state = getColorState(value);
|
|
175
|
-
Object.defineProperty(this, "$color", { value: true });
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Get the color as a hex string
|
|
179
|
-
*
|
|
180
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
181
|
-
* @returns Hex color string
|
|
182
|
-
*/
|
|
183
|
-
toHexString(alpha) {
|
|
184
|
-
return `#${alpha === true ? this.hexa : this.hex}`;
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Get the color as an _HSL(A)_ string
|
|
188
|
-
*
|
|
189
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
190
|
-
* @returns _HSL(A)_ color string
|
|
191
|
-
*/
|
|
192
|
-
toHslString(alpha) {
|
|
193
|
-
return formatHslColor(this, alpha === true);
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Get the color as an _HWB(A)_ string
|
|
197
|
-
*
|
|
198
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
199
|
-
* @returns _HWB(A)_ color string
|
|
200
|
-
*/
|
|
201
|
-
toHwbString(alpha) {
|
|
202
|
-
return formatHwbColor(this, alpha === true);
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Get the color as an _RGB(A)_ string
|
|
206
|
-
*
|
|
207
|
-
* @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
|
|
208
|
-
* @returns _RGB(A)_ color string
|
|
209
|
-
*/
|
|
210
|
-
toRgbString(alpha) {
|
|
211
|
-
return formatRgbColor(this, alpha === true);
|
|
212
|
-
}
|
|
213
|
-
/**
|
|
214
|
-
* Get the color as a hex color string
|
|
215
|
-
*
|
|
216
|
-
* @returns Hex color string
|
|
217
|
-
*/
|
|
218
|
-
toString() {
|
|
219
|
-
return this.toHexString();
|
|
220
|
-
}
|
|
6
|
+
function color(value) {
|
|
7
|
+
const state = getColorState(value);
|
|
8
|
+
const instance = {
|
|
9
|
+
toHexString: (alpha) => `#${alpha === true ? instance.hexa : instance.hex}`,
|
|
10
|
+
toHslString: (alpha) => formatHslColor(state, alpha),
|
|
11
|
+
toHwbString: (alpha) => formatHwbColor(state, alpha),
|
|
12
|
+
toRgbString: (alpha) => formatRgbColor(state, alpha),
|
|
13
|
+
toString: () => instance.toHexString()
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperties(instance, {
|
|
16
|
+
[PROPERTY_COLOR]: {
|
|
17
|
+
enumerable: false,
|
|
18
|
+
value: true
|
|
19
|
+
},
|
|
20
|
+
alpha: {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: () => state.alpha.value,
|
|
23
|
+
set: (value) => setAlphaValue(state, value)
|
|
24
|
+
},
|
|
25
|
+
hex: getProperty("hex", state, false),
|
|
26
|
+
hexa: getProperty("hex", state, true),
|
|
27
|
+
hsl: getProperty("hsl", state, false),
|
|
28
|
+
hsla: getProperty("hsl", state, true),
|
|
29
|
+
hwb: getProperty("hwb", state, false),
|
|
30
|
+
hwba: getProperty("hwb", state, true),
|
|
31
|
+
origin: {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: () => state.origin
|
|
34
|
+
},
|
|
35
|
+
rgb: getProperty("rgb", state, false),
|
|
36
|
+
rgba: getProperty("rgb", state, true)
|
|
37
|
+
});
|
|
38
|
+
return Object.freeze(instance);
|
|
39
|
+
}
|
|
40
|
+
function getProperty(space, state, alpha) {
|
|
41
|
+
const setValue = setters[space];
|
|
42
|
+
return {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
get: () => getValue(space, state, alpha),
|
|
45
|
+
set: (value) => setValue(state, value, alpha)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function getValue(space, state, alpha) {
|
|
49
|
+
const value = getColorFromState(state, space);
|
|
50
|
+
if (typeof value === "string") return alpha ? `${value}${state.alpha.hex}` : value;
|
|
51
|
+
return alpha ? {
|
|
52
|
+
...value,
|
|
53
|
+
alpha: state.alpha.value
|
|
54
|
+
} : { ...value };
|
|
55
|
+
}
|
|
56
|
+
function setAlphaValue(state, value) {
|
|
57
|
+
if (typeof value === "number" && !Number.isNaN(value)) state.alpha = getAlpha(value, false);
|
|
58
|
+
}
|
|
59
|
+
const setters = {
|
|
60
|
+
hex: setHexColor,
|
|
61
|
+
hsl: setHSLColor,
|
|
62
|
+
hwb: setHWBColor,
|
|
63
|
+
rgb: setRGBColor
|
|
221
64
|
};
|
|
222
65
|
//#endregion
|
|
223
|
-
export {
|
|
66
|
+
export { color };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Alpha } from "../models.mjs";
|
|
2
2
|
//#region src/color/misc/alpha.d.ts
|
|
3
|
-
declare function getAlpha(value: unknown): Alpha;
|
|
3
|
+
declare function getAlpha(value: unknown, hex: boolean): Alpha;
|
|
4
4
|
declare function getAlphaHexadecimal(value: number): string;
|
|
5
5
|
declare function getAlphaValue(original: number): number;
|
|
6
6
|
//#endregion
|
|
@@ -1,18 +1,21 @@
|
|
|
1
|
+
import { clamp } from "../../internal/number.mjs";
|
|
2
|
+
import { ALPHA_FULL_HEX_LONG, DEFAULT_ALPHA, EXPRESSION_ALPHA_HEX } from "../constants.mjs";
|
|
1
3
|
import { round } from "../../internal/math/misc.mjs";
|
|
2
|
-
import
|
|
4
|
+
import "../../index.mjs";
|
|
3
5
|
//#region src/color/misc/alpha.ts
|
|
4
|
-
function getAlpha(value) {
|
|
6
|
+
function getAlpha(value, hex) {
|
|
5
7
|
if (typeof value === "number") return getAlphaFromValue(value);
|
|
6
|
-
if (typeof value
|
|
8
|
+
if (typeof value !== "string" || value.toLowerCase() === ALPHA_FULL_HEX_LONG) return { ...DEFAULT_ALPHA };
|
|
9
|
+
if (hex && EXPRESSION_ALPHA_HEX.test(value)) return {
|
|
7
10
|
hex: value,
|
|
8
11
|
value: Number.parseInt(value, 16) / 255
|
|
9
12
|
};
|
|
10
|
-
return
|
|
13
|
+
return getAlphaFromValue(Number.parseFloat(value));
|
|
11
14
|
}
|
|
12
15
|
function getAlphaHexadecimal(value) {
|
|
13
16
|
if (value === 0) return "00";
|
|
14
17
|
if (value === 100) return ALPHA_FULL_HEX_LONG;
|
|
15
|
-
return round(value * 255).toString(16);
|
|
18
|
+
return round(value / 100 * 255).toString(16);
|
|
16
19
|
}
|
|
17
20
|
function getAlphaFromValue(value) {
|
|
18
21
|
const alpha = getAlphaValue(value);
|
|
@@ -22,8 +25,8 @@ function getAlphaFromValue(value) {
|
|
|
22
25
|
};
|
|
23
26
|
}
|
|
24
27
|
function getAlphaValue(original) {
|
|
25
|
-
if (Number.isNaN(original)
|
|
26
|
-
return original
|
|
28
|
+
if (Number.isNaN(original)) return 100;
|
|
29
|
+
return clamp(original, 0, 100);
|
|
27
30
|
}
|
|
28
31
|
//#endregion
|
|
29
32
|
export { getAlpha, getAlphaHexadecimal, getAlphaValue };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ColorState } from "../models.mjs";
|
|
2
|
+
//#region src/color/misc/format.d.ts
|
|
3
|
+
declare function formatHslColor(state: ColorState, alpha?: boolean): string;
|
|
4
|
+
declare function formatHwbColor(state: ColorState, alpha?: boolean): string;
|
|
5
|
+
declare function formatRgbColor(state: ColorState, alpha?: boolean): string;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { formatHslColor, formatHwbColor, formatRgbColor };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import "../constants.mjs";
|
|
2
|
+
import { getColorFromState } from "./state.mjs";
|
|
3
|
+
//#region src/color/misc/format.ts
|
|
4
|
+
function formatHslColor(state, alpha) {
|
|
5
|
+
const { hue, lightness, saturation } = getColorFromState(state, "hsl");
|
|
6
|
+
return `hsl(${hue}deg ${saturation}% ${lightness}%${getSuffix(state, alpha)})`;
|
|
7
|
+
}
|
|
8
|
+
function formatHwbColor(state, alpha) {
|
|
9
|
+
const { blackness, hue, whiteness } = getColorFromState(state, "hwb");
|
|
10
|
+
return `hwb(${hue}deg ${whiteness}% ${blackness}%${getSuffix(state, alpha)})`;
|
|
11
|
+
}
|
|
12
|
+
function formatRgbColor(state, alpha) {
|
|
13
|
+
const { blue, green, red } = getColorFromState(state, "rgb");
|
|
14
|
+
return `rgb(${red} ${green} ${blue}${getSuffix(state, alpha)})`;
|
|
15
|
+
}
|
|
16
|
+
function getSuffix(state, alpha) {
|
|
17
|
+
return alpha === true ? ` / ${state.alpha.value}%` : "";
|
|
18
|
+
}
|
|
19
|
+
//#endregion
|
|
20
|
+
export { formatHslColor, formatHwbColor, formatRgbColor };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
2
|
-
import { Color } from "../instance.mjs";
|
|
1
|
+
import { Color, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
|
|
3
2
|
//#region src/color/misc/get.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Get a foreground color _(usually text)_ based on a background color's luminance as an unprefixed hex color string
|
package/dist/color/misc/get.mjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { clamp } from "../../internal/number.mjs";
|
|
2
|
-
import { round } from "../../internal/math/misc.mjs";
|
|
3
2
|
import { HEX_BLACK, HEX_WHITE, SRGB_LUMINANCE_EXPONENT, SRGB_LUMINANCE_MODIFIER, SRGB_LUMINANCE_MULTIPLIER, SRGB_LUMINANCE_OFFSET } from "../constants.mjs";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { round } from "../../internal/math/misc.mjs";
|
|
4
|
+
import { getColorFromState, getColorState } from "./state.mjs";
|
|
5
|
+
import { color } from "../instance.mjs";
|
|
6
6
|
//#region src/color/misc/get.ts
|
|
7
7
|
function getClampedValue(value, minimum, maximum, rounding) {
|
|
8
8
|
return typeof value === "number" && !Number.isNaN(value) ? clamp(rounding ?? false ? round(value) : value, minimum, maximum) : minimum;
|
|
9
9
|
}
|
|
10
10
|
function getForegroundColor(value, hex) {
|
|
11
|
-
const { blue, green, red } = getColorState(value)
|
|
11
|
+
const { blue, green, red } = getColorFromState(getColorState(value), "rgb");
|
|
12
12
|
const values = [
|
|
13
13
|
blue / 255,
|
|
14
14
|
green / 255,
|
|
@@ -20,8 +20,8 @@ function getForegroundColor(value, hex) {
|
|
|
20
20
|
if (color <= .03928) values[index] /= SRGB_LUMINANCE_MULTIPLIER;
|
|
21
21
|
else values[index] = ((color + SRGB_LUMINANCE_OFFSET) / SRGB_LUMINANCE_MODIFIER) ** SRGB_LUMINANCE_EXPONENT;
|
|
22
22
|
}
|
|
23
|
-
const
|
|
24
|
-
return hex === true ?
|
|
23
|
+
const foreground = .2126 * values[2] + .7152 * values[1] + .0722 * values[0] > .625 ? HEX_BLACK : HEX_WHITE;
|
|
24
|
+
return hex === true ? foreground : color(foreground);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* Get the hex color _(with alpha channel, i.e., opacity)_ from any kind of value
|
|
@@ -46,7 +46,7 @@ function getHexaColor(value) {
|
|
|
46
46
|
* @returns Hex color string
|
|
47
47
|
*/
|
|
48
48
|
function getHexColor(value) {
|
|
49
|
-
return getColorState(value)
|
|
49
|
+
return getColorFromState(getColorState(value), "hex");
|
|
50
50
|
}
|
|
51
51
|
function getHexValue(value) {
|
|
52
52
|
return getClampedValue(value, 0, 255, true);
|
|
@@ -64,10 +64,10 @@ function getDegrees(value) {
|
|
|
64
64
|
* @returns _HSLA_ color
|
|
65
65
|
*/
|
|
66
66
|
function getHslaColor(value) {
|
|
67
|
-
const
|
|
67
|
+
const state = getColorState(value);
|
|
68
68
|
return {
|
|
69
|
-
...hsl,
|
|
70
|
-
alpha: alpha.value
|
|
69
|
+
...getColorFromState(state, "hsl"),
|
|
70
|
+
alpha: state.alpha.value
|
|
71
71
|
};
|
|
72
72
|
}
|
|
73
73
|
/**
|
|
@@ -80,7 +80,7 @@ function getHslaColor(value) {
|
|
|
80
80
|
* @returns _HWB_ color
|
|
81
81
|
*/
|
|
82
82
|
function getHwbColor(value) {
|
|
83
|
-
return getColorState(value)
|
|
83
|
+
return getColorFromState(getColorState(value), "hwb");
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
86
|
* Get the _HWBA_ color from any kind of value
|
|
@@ -92,10 +92,10 @@ function getHwbColor(value) {
|
|
|
92
92
|
* @returns _HWBA_ color
|
|
93
93
|
*/
|
|
94
94
|
function getHwbaColor(value) {
|
|
95
|
-
const
|
|
95
|
+
const state = getColorState(value);
|
|
96
96
|
return {
|
|
97
|
-
...hwb,
|
|
98
|
-
alpha: alpha.value
|
|
97
|
+
...getColorFromState(state, "hwb"),
|
|
98
|
+
alpha: state.alpha.value
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
@@ -108,7 +108,7 @@ function getHwbaColor(value) {
|
|
|
108
108
|
* @returns _HSL_ color
|
|
109
109
|
*/
|
|
110
110
|
function getHslColor(value) {
|
|
111
|
-
return getColorState(value)
|
|
111
|
+
return getColorFromState(getColorState(value), "hsl");
|
|
112
112
|
}
|
|
113
113
|
function getPercentage(value) {
|
|
114
114
|
return getClampedValue(value, 0, 100);
|
|
@@ -123,10 +123,10 @@ function getPercentage(value) {
|
|
|
123
123
|
* @returns _RGBA_ color
|
|
124
124
|
*/
|
|
125
125
|
function getRgbaColor(value) {
|
|
126
|
-
const
|
|
126
|
+
const state = getColorState(value);
|
|
127
127
|
return {
|
|
128
|
-
...rgb,
|
|
129
|
-
alpha: alpha.value
|
|
128
|
+
...getColorFromState(state, "rgb"),
|
|
129
|
+
alpha: state.alpha.value
|
|
130
130
|
};
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
@@ -139,7 +139,7 @@ function getRgbaColor(value) {
|
|
|
139
139
|
* @returns _RGB_ color
|
|
140
140
|
*/
|
|
141
141
|
function getRgbColor(value) {
|
|
142
|
-
return getColorState(value)
|
|
142
|
+
return getColorFromState(getColorState(value), "rgb");
|
|
143
143
|
}
|
|
144
144
|
//#endregion
|
|
145
145
|
export { getDegrees, getForegroundColor, getHexColor, getHexValue, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getPercentage, getRgbColor, getRgbaColor };
|