@oscarpalmer/atoms 0.191.0 → 0.192.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 +6 -44
- package/dist/beacon.mjs +20 -67
- package/dist/color/constants.d.mts +82 -41
- package/dist/color/constants.mjs +102 -63
- package/dist/color/instance.mjs +36 -13
- package/dist/color/misc/alpha.mjs +9 -10
- package/dist/color/misc/format.mjs +4 -4
- package/dist/color/misc/get.mjs +18 -18
- package/dist/color/misc/is.mjs +22 -21
- package/dist/color/misc/state.d.mts +6 -5
- package/dist/color/misc/state.mjs +31 -26
- package/dist/color/models.d.mts +78 -1
- package/dist/color/space/hex.mjs +12 -12
- package/dist/color/space/hsl.mjs +16 -16
- package/dist/color/space/hwb.mjs +10 -10
- package/dist/color/space/rgb.mjs +16 -16
- package/dist/herald.d.mts +2 -3
- package/dist/index.d.mts +117 -45
- package/dist/index.mjs +522 -360
- package/dist/internal/subscription.d.mts +52 -0
- package/dist/internal/subscription.mjs +127 -0
- package/dist/models.d.mts +5 -1
- package/package.json +1 -1
- package/src/beacon.ts +32 -117
- package/src/color/constants.ts +115 -78
- package/src/color/instance.ts +65 -12
- package/src/color/misc/alpha.ts +13 -21
- package/src/color/misc/format.ts +4 -4
- package/src/color/misc/get.ts +23 -40
- package/src/color/misc/is.ts +29 -39
- package/src/color/misc/state.ts +77 -41
- package/src/color/models.ts +94 -1
- package/src/color/space/hex.ts +18 -23
- package/src/color/space/hsl.ts +16 -24
- package/src/color/space/hwb.ts +10 -10
- package/src/color/space/rgb.ts +19 -16
- package/src/herald.ts +1 -3
- package/src/index.ts +1 -0
- package/src/internal/subscription.ts +264 -0
- package/src/models.ts +5 -0
- package/src/string/fuzzy.ts +1 -1
- package/src/value/freeze.ts +2 -2
- package/src/value/shake.ts +1 -1
- package/src/value/transform.ts +1 -1
package/src/color/misc/state.ts
CHANGED
|
@@ -1,19 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
DEFAULT_HWB,
|
|
4
|
-
DEFAULT_RGB,
|
|
5
|
-
HEX_BLACK,
|
|
6
|
-
KEYS_HSL,
|
|
7
|
-
KEYS_HWB,
|
|
8
|
-
KEYS_RGB,
|
|
9
|
-
LENGTH_LONG,
|
|
10
|
-
MAX_PERCENT,
|
|
11
|
-
TYPE_HEX,
|
|
12
|
-
TYPE_HSL,
|
|
13
|
-
TYPE_HWB,
|
|
14
|
-
TYPE_RGB,
|
|
15
|
-
} from '../constants';
|
|
1
|
+
import type {Subscriptions} from '../../internal/subscription';
|
|
2
|
+
import {COLOR_DEFAULTS, COLOR_KEYS, COLOR_LENGTHS, COLOR_MAX, COLOR_TYPE} from '../constants';
|
|
16
3
|
import type {
|
|
4
|
+
Color,
|
|
17
5
|
ColorState,
|
|
18
6
|
ColorType,
|
|
19
7
|
HSLAColor,
|
|
@@ -67,15 +55,15 @@ export function getColorState(value: unknown): ColorState {
|
|
|
67
55
|
origin: undefined as never,
|
|
68
56
|
};
|
|
69
57
|
|
|
70
|
-
if (
|
|
58
|
+
if (COLOR_KEYS.hsl.every(key => key in value)) {
|
|
71
59
|
state.hsl = getHslValue(value as Record<keyof HSLColor, unknown>);
|
|
72
|
-
state.origin =
|
|
73
|
-
} else if (
|
|
60
|
+
state.origin = COLOR_TYPE.hsl;
|
|
61
|
+
} else if (COLOR_KEYS.hwb.every(key => key in value)) {
|
|
74
62
|
state.hwb = getHwbValue(value as Record<keyof HWBColor, unknown>);
|
|
75
|
-
state.origin =
|
|
76
|
-
} else if (
|
|
63
|
+
state.origin = COLOR_TYPE.hwb;
|
|
64
|
+
} else if (COLOR_KEYS.rgb.every(key => key in value)) {
|
|
77
65
|
state.rgb = getRgbValue(value as Record<keyof RGBColor, unknown>);
|
|
78
|
-
state.origin =
|
|
66
|
+
state.origin = COLOR_TYPE.rgb;
|
|
79
67
|
}
|
|
80
68
|
|
|
81
69
|
if (state.origin != null) {
|
|
@@ -88,28 +76,30 @@ export function getColorState(value: unknown): ColorState {
|
|
|
88
76
|
|
|
89
77
|
function getColorStateForHex(value: string): ColorState {
|
|
90
78
|
const normalized = getNormalizedHex(value, true);
|
|
91
|
-
const hex = normalized.slice(0,
|
|
79
|
+
const hex = normalized.slice(0, COLOR_LENGTHS.hexLong);
|
|
92
80
|
|
|
93
81
|
return {
|
|
94
82
|
hex,
|
|
95
|
-
alpha: getAlpha(normalized.slice(
|
|
96
|
-
origin:
|
|
83
|
+
alpha: getAlpha(normalized.slice(COLOR_LENGTHS.hexLong), true),
|
|
84
|
+
origin: COLOR_TYPE.hex,
|
|
97
85
|
};
|
|
98
86
|
}
|
|
99
87
|
|
|
100
88
|
function getDefaultColorState(): ColorState {
|
|
101
89
|
return {
|
|
102
|
-
alpha: getAlpha(
|
|
103
|
-
hex:
|
|
104
|
-
hsl: {...
|
|
105
|
-
hwb: {...
|
|
106
|
-
rgb: {...
|
|
107
|
-
origin:
|
|
90
|
+
alpha: getAlpha(COLOR_MAX.percent, false),
|
|
91
|
+
hex: COLOR_DEFAULTS.hexBlack,
|
|
92
|
+
hsl: {...COLOR_DEFAULTS.hsl},
|
|
93
|
+
hwb: {...COLOR_DEFAULTS.hwb},
|
|
94
|
+
rgb: {...COLOR_DEFAULTS.rgb},
|
|
95
|
+
origin: COLOR_TYPE.hex,
|
|
108
96
|
};
|
|
109
97
|
}
|
|
110
98
|
|
|
111
99
|
function setColorValue<Type extends ColorType>(
|
|
100
|
+
color: Color,
|
|
112
101
|
state: ColorState,
|
|
102
|
+
subscriptions: Subscriptions<Function>,
|
|
113
103
|
type: Type,
|
|
114
104
|
value: ColorState[Type],
|
|
115
105
|
alpha?: number | string,
|
|
@@ -123,11 +113,31 @@ function setColorValue<Type extends ColorType>(
|
|
|
123
113
|
state[type] = value;
|
|
124
114
|
|
|
125
115
|
if (alpha != null) {
|
|
126
|
-
state.alpha = getAlpha(alpha, type ===
|
|
116
|
+
state.alpha = getAlpha(alpha, type === COLOR_TYPE.hex);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const type of COLOR_TYPE.all) {
|
|
120
|
+
const callbacks = subscriptions.values.to.keyed?.get(type);
|
|
121
|
+
|
|
122
|
+
if (callbacks != null) {
|
|
123
|
+
for (const callback of callbacks.values()) {
|
|
124
|
+
callback(color[type]);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
for (const callback of subscriptions.values.to.any.values()) {
|
|
130
|
+
callback(color);
|
|
127
131
|
}
|
|
128
132
|
}
|
|
129
133
|
|
|
130
|
-
export function setHexColor(
|
|
134
|
+
export function setHexColor(
|
|
135
|
+
color: Color,
|
|
136
|
+
state: ColorState,
|
|
137
|
+
subscriptions: Subscriptions<Function>,
|
|
138
|
+
value: string,
|
|
139
|
+
alpha: boolean,
|
|
140
|
+
): void {
|
|
131
141
|
if (!isHexColor(value) || (!alpha && value === state.hex)) {
|
|
132
142
|
return;
|
|
133
143
|
}
|
|
@@ -135,21 +145,31 @@ export function setHexColor(state: ColorState, value: string, alpha: boolean): v
|
|
|
135
145
|
const normalized = getNormalizedHex(value, true);
|
|
136
146
|
|
|
137
147
|
setColorValue(
|
|
148
|
+
color,
|
|
138
149
|
state,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
150
|
+
subscriptions,
|
|
151
|
+
COLOR_TYPE.hex,
|
|
152
|
+
normalized.slice(0, COLOR_LENGTHS.hexLong),
|
|
153
|
+
alpha ? normalized.slice(COLOR_LENGTHS.hexLong) : undefined,
|
|
142
154
|
);
|
|
143
155
|
}
|
|
144
156
|
|
|
145
|
-
export function setHSLColor(
|
|
157
|
+
export function setHSLColor(
|
|
158
|
+
color: Color,
|
|
159
|
+
state: ColorState,
|
|
160
|
+
subscriptions: Subscriptions<Function>,
|
|
161
|
+
value: unknown,
|
|
162
|
+
alpha: boolean,
|
|
163
|
+
): void {
|
|
146
164
|
if (!isHslLike(value)) {
|
|
147
165
|
return;
|
|
148
166
|
}
|
|
149
167
|
|
|
150
168
|
setColorValue(
|
|
169
|
+
color,
|
|
151
170
|
state,
|
|
152
|
-
|
|
171
|
+
subscriptions,
|
|
172
|
+
COLOR_TYPE.hsl,
|
|
153
173
|
{
|
|
154
174
|
hue: getDegrees((value as HSLColor).hue),
|
|
155
175
|
saturation: getPercentage((value as HSLColor).saturation),
|
|
@@ -159,14 +179,22 @@ export function setHSLColor(state: ColorState, value: unknown, alpha: boolean):
|
|
|
159
179
|
);
|
|
160
180
|
}
|
|
161
181
|
|
|
162
|
-
export function setHWBColor(
|
|
182
|
+
export function setHWBColor(
|
|
183
|
+
color: Color,
|
|
184
|
+
state: ColorState,
|
|
185
|
+
subscriptions: Subscriptions<Function>,
|
|
186
|
+
value: unknown,
|
|
187
|
+
alpha: boolean,
|
|
188
|
+
): void {
|
|
163
189
|
if (!isHwbLike(value)) {
|
|
164
190
|
return;
|
|
165
191
|
}
|
|
166
192
|
|
|
167
193
|
setColorValue(
|
|
194
|
+
color,
|
|
168
195
|
state,
|
|
169
|
-
|
|
196
|
+
subscriptions,
|
|
197
|
+
COLOR_TYPE.hwb,
|
|
170
198
|
{
|
|
171
199
|
hue: getDegrees((value as HWBColor).hue),
|
|
172
200
|
whiteness: getPercentage((value as HWBColor).whiteness),
|
|
@@ -176,14 +204,22 @@ export function setHWBColor(state: ColorState, value: unknown, alpha: boolean):
|
|
|
176
204
|
);
|
|
177
205
|
}
|
|
178
206
|
|
|
179
|
-
export function setRGBColor(
|
|
207
|
+
export function setRGBColor(
|
|
208
|
+
color: Color,
|
|
209
|
+
state: ColorState,
|
|
210
|
+
subscriptions: Subscriptions<Function>,
|
|
211
|
+
value: unknown,
|
|
212
|
+
alpha: boolean,
|
|
213
|
+
): void {
|
|
180
214
|
if (!isRgbLike(value)) {
|
|
181
215
|
return;
|
|
182
216
|
}
|
|
183
217
|
|
|
184
218
|
setColorValue(
|
|
219
|
+
color,
|
|
185
220
|
state,
|
|
186
|
-
|
|
221
|
+
subscriptions,
|
|
222
|
+
COLOR_TYPE.rgb,
|
|
187
223
|
{
|
|
188
224
|
red: getHexValue((value as RGBColor).red),
|
|
189
225
|
green: getHexValue((value as RGBColor).green),
|
package/src/color/models.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
// #region Types
|
|
2
2
|
|
|
3
|
+
import type {Subscription} from '../internal/subscription';
|
|
4
|
+
import type {COLOR_ALPHA, COLOR_TYPE} from './constants';
|
|
5
|
+
|
|
3
6
|
export type Alpha = {
|
|
4
7
|
hex: string;
|
|
5
8
|
value: number;
|
|
@@ -152,6 +155,85 @@ export type Color = {
|
|
|
152
155
|
*/
|
|
153
156
|
set rgba(value: RGBAColor);
|
|
154
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Subscribe to alpha channel _(opacity)_ changes of the color
|
|
160
|
+
*
|
|
161
|
+
* @param key Color property to subscribe to
|
|
162
|
+
* @param callback Callback function for changes
|
|
163
|
+
*/
|
|
164
|
+
subscribe(key: 'alpha', callback: (value: number) => void): Subscription;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Subscribe to hex color changes of the color
|
|
168
|
+
*
|
|
169
|
+
* @param key Color type to subscribe to
|
|
170
|
+
* @param callback Callback function for changes
|
|
171
|
+
*/
|
|
172
|
+
subscribe(key: typeof COLOR_TYPE.hex, callback: (value: string) => void): Subscription;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Subscribe to hex color changes of the color with alpha channel _(opacity)_
|
|
176
|
+
*
|
|
177
|
+
* @param key Color type to subscribe to
|
|
178
|
+
* @param callback Callback function for changes
|
|
179
|
+
*/
|
|
180
|
+
subscribe(key: typeof COLOR_TYPE.hexa, callback: (value: string) => void): Subscription;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Subscribe to _HSL_ color changes of the color
|
|
184
|
+
*
|
|
185
|
+
* @param key Color type to subscribe to
|
|
186
|
+
* @param callback Callback function for changes
|
|
187
|
+
*/
|
|
188
|
+
subscribe(key: typeof COLOR_TYPE.hsl, callback: (value: HSLColor) => void): Subscription;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Subscribe to _HSL(A)_ color changes of the color
|
|
192
|
+
*
|
|
193
|
+
* @param key Color type to subscribe to
|
|
194
|
+
* @param callback Callback function for changes
|
|
195
|
+
*/
|
|
196
|
+
subscribe(key: typeof COLOR_TYPE.hsla, callback: (value: HSLAColor) => void): Subscription;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Subscribe to _HWB_ color changes of the color
|
|
200
|
+
*
|
|
201
|
+
* @param key Color type to subscribe to
|
|
202
|
+
* @param callback Callback function for changes
|
|
203
|
+
*/
|
|
204
|
+
subscribe(key: typeof COLOR_TYPE.hwb, callback: (value: HWBColor) => void): Subscription;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Subscribe to _HWB(A)_ color changes of the color
|
|
208
|
+
*
|
|
209
|
+
* @param key Color type to subscribe to
|
|
210
|
+
* @param callback Callback function for changes
|
|
211
|
+
*/
|
|
212
|
+
subscribe(key: typeof COLOR_TYPE.hwba, callback: (value: HWBAColor) => void): Subscription;
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Subscribe to _RGB_ color changes of the color
|
|
216
|
+
*
|
|
217
|
+
* @param key Color type to subscribe to
|
|
218
|
+
* @param callback Callback function for changes
|
|
219
|
+
*/
|
|
220
|
+
subscribe(key: typeof COLOR_TYPE.rgb, callback: (value: RGBColor) => void): Subscription;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Subscribe to _RGBA_ color changes of the color
|
|
224
|
+
*
|
|
225
|
+
* @param key Color type to subscribe to
|
|
226
|
+
* @param callback Callback function for changes
|
|
227
|
+
*/
|
|
228
|
+
subscribe(key: typeof COLOR_TYPE.rgba, callback: (value: RGBAColor) => void): Subscription;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Subscribe to any color changes of the color
|
|
232
|
+
*
|
|
233
|
+
* @param callback Callback function for changes
|
|
234
|
+
*/
|
|
235
|
+
subscribe(callback: (value: Color) => void): Subscription;
|
|
236
|
+
|
|
155
237
|
/**
|
|
156
238
|
* Get the color as a hex string
|
|
157
239
|
*
|
|
@@ -190,6 +272,13 @@ export type Color = {
|
|
|
190
272
|
* @returns Hex color string
|
|
191
273
|
*/
|
|
192
274
|
toString(): string;
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Unsubscribe from all color changes of the color
|
|
278
|
+
*
|
|
279
|
+
* _(To unsubscribe from a specific color change, use the `unsubscribe()` method on the `Subscription` returned by the individual `subscribe()` method)_
|
|
280
|
+
*/
|
|
281
|
+
unsubscribe(): void;
|
|
193
282
|
};
|
|
194
283
|
|
|
195
284
|
type ColorWithAlpha = {
|
|
@@ -262,7 +351,11 @@ export type RGBColor = {
|
|
|
262
351
|
red: number;
|
|
263
352
|
};
|
|
264
353
|
|
|
265
|
-
export type ColorProperty =
|
|
354
|
+
export type ColorProperty =
|
|
355
|
+
| typeof COLOR_ALPHA.name
|
|
356
|
+
| keyof HSLColor
|
|
357
|
+
| keyof HWBColor
|
|
358
|
+
| keyof RGBColor;
|
|
266
359
|
|
|
267
360
|
export type ColorState = {
|
|
268
361
|
alpha: Alpha;
|
package/src/color/space/hex.ts
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import {join} from '../../internal/string';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
LENGTH_SHORT,
|
|
10
|
-
MAX_HEX,
|
|
11
|
-
TYPE_HSL,
|
|
12
|
-
TYPE_HWB,
|
|
13
|
-
TYPE_RGB,
|
|
3
|
+
COLOR_ALPHA,
|
|
4
|
+
COLOR_DEFAULTS,
|
|
5
|
+
COLOR_EXPRESSION,
|
|
6
|
+
COLOR_LENGTHS,
|
|
7
|
+
COLOR_MAX,
|
|
8
|
+
COLOR_TYPE,
|
|
14
9
|
} from '../constants';
|
|
15
10
|
import {getHexValue, getPercentage} from '../misc/get';
|
|
16
11
|
import {isHexColor} from '../misc/is';
|
|
@@ -30,7 +25,7 @@ import {convertRgbToHsla, convertRgbToHwba} from './rgb';
|
|
|
30
25
|
|
|
31
26
|
function convertHexToRgba(value: string): RGBAColor {
|
|
32
27
|
const normalized = getNormalizedHex(value, true);
|
|
33
|
-
const pairs =
|
|
28
|
+
const pairs = COLOR_EXPRESSION.hexLong.exec(normalized) as RegExpExecArray;
|
|
34
29
|
const values: number[] = [];
|
|
35
30
|
|
|
36
31
|
const {length} = pairs;
|
|
@@ -40,7 +35,7 @@ function convertHexToRgba(value: string): RGBAColor {
|
|
|
40
35
|
}
|
|
41
36
|
|
|
42
37
|
return {
|
|
43
|
-
alpha: getPercentage((values[3] /
|
|
38
|
+
alpha: getPercentage((values[3] / COLOR_MAX.hex) * 100),
|
|
44
39
|
blue: getHexValue(values[2]),
|
|
45
40
|
green: getHexValue(values[1]),
|
|
46
41
|
red: getHexValue(values[0]),
|
|
@@ -52,15 +47,15 @@ export function getColorFromHex<Type extends ColorType>(
|
|
|
52
47
|
type: Type,
|
|
53
48
|
): NonNullable<ColorState[Type]> {
|
|
54
49
|
switch (type) {
|
|
55
|
-
case
|
|
50
|
+
case COLOR_TYPE.hsl:
|
|
56
51
|
state.hsl ??= hexToHsl(state.hex!);
|
|
57
52
|
break;
|
|
58
53
|
|
|
59
|
-
case
|
|
54
|
+
case COLOR_TYPE.hwb:
|
|
60
55
|
state.hwb ??= hexToHwb(state.hex!);
|
|
61
56
|
break;
|
|
62
57
|
|
|
63
|
-
case
|
|
58
|
+
case COLOR_TYPE.rgb:
|
|
64
59
|
state.rgb ??= hexToRgb(state.hex!);
|
|
65
60
|
break;
|
|
66
61
|
}
|
|
@@ -81,21 +76,21 @@ export function getNormalizedHex(value: unknown, alpha?: boolean): string {
|
|
|
81
76
|
const includeAlpha = alpha ?? false;
|
|
82
77
|
|
|
83
78
|
if (!isHexColor(value)) {
|
|
84
|
-
return `${
|
|
79
|
+
return `${COLOR_DEFAULTS.hexBlack}${includeAlpha ? COLOR_ALPHA.fullHexLong : ''}`;
|
|
85
80
|
}
|
|
86
81
|
|
|
87
|
-
const normalized = value.replace(
|
|
82
|
+
const normalized = value.replace(COLOR_EXPRESSION.prefix, '');
|
|
88
83
|
|
|
89
|
-
if (normalized.length <
|
|
90
|
-
const hex = normalized.slice(0,
|
|
91
|
-
const a = includeAlpha ? (normalized[
|
|
84
|
+
if (normalized.length < COLOR_LENGTHS.hexLong) {
|
|
85
|
+
const hex = normalized.slice(0, COLOR_LENGTHS.hexShort);
|
|
86
|
+
const a = includeAlpha ? (normalized[COLOR_LENGTHS.hexShort] ?? COLOR_ALPHA.fullHexShort) : '';
|
|
92
87
|
|
|
93
88
|
return join(`${hex}${a}`.split('').map(character => character.repeat(2)));
|
|
94
89
|
}
|
|
95
90
|
|
|
96
|
-
const hex = normalized.slice(0,
|
|
91
|
+
const hex = normalized.slice(0, COLOR_LENGTHS.hexLong);
|
|
97
92
|
|
|
98
|
-
const a = includeAlpha ? normalized.slice(
|
|
93
|
+
const a = includeAlpha ? normalized.slice(COLOR_LENGTHS.hexLong) || COLOR_ALPHA.fullHexLong : '';
|
|
99
94
|
|
|
100
95
|
return `${hex}${a}`;
|
|
101
96
|
}
|
package/src/color/space/hsl.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DEFAULT_HSL,
|
|
3
|
-
MAX_DEGREE,
|
|
4
|
-
MAX_HEX,
|
|
5
|
-
MAX_PERCENT,
|
|
6
|
-
TYPE_HEX,
|
|
7
|
-
TYPE_HWB,
|
|
8
|
-
TYPE_RGB,
|
|
9
|
-
} from '../constants';
|
|
1
|
+
import {COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE} from '../constants';
|
|
10
2
|
import {getAlphaValue} from '../misc/alpha';
|
|
11
3
|
import {getDegrees, getHexValue, getPercentage} from '../misc/get';
|
|
12
4
|
import {isHslLike} from '../misc/is';
|
|
@@ -25,12 +17,12 @@ import {convertRgbToHex} from './rgb';
|
|
|
25
17
|
// #region Functions
|
|
26
18
|
|
|
27
19
|
function convertHslToHwba(input: unknown): HWBAColor {
|
|
28
|
-
const hsl = isHslLike(input) ? getHslValue(input) : {...
|
|
20
|
+
const hsl = isHslLike(input) ? getHslValue(input) : {...COLOR_DEFAULTS.hsl};
|
|
29
21
|
|
|
30
22
|
let {hue, lightness, saturation} = hsl;
|
|
31
23
|
|
|
32
|
-
lightness /=
|
|
33
|
-
saturation /=
|
|
24
|
+
lightness /= COLOR_MAX.percent;
|
|
25
|
+
saturation /= COLOR_MAX.percent;
|
|
34
26
|
|
|
35
27
|
const value = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
36
28
|
|
|
@@ -45,21 +37,21 @@ function convertHslToHwba(input: unknown): HWBAColor {
|
|
|
45
37
|
|
|
46
38
|
return {
|
|
47
39
|
hue,
|
|
48
|
-
whiteness: whiteness *
|
|
49
|
-
blackness: blackness *
|
|
50
|
-
alpha: getAlphaValue((input as HSLAColor)?.alpha ??
|
|
40
|
+
whiteness: whiteness * COLOR_MAX.percent,
|
|
41
|
+
blackness: blackness * COLOR_MAX.percent,
|
|
42
|
+
alpha: getAlphaValue((input as HSLAColor)?.alpha ?? COLOR_MAX.percent),
|
|
51
43
|
};
|
|
52
44
|
}
|
|
53
45
|
|
|
54
46
|
function convertHslToRgba(input: unknown): RGBAColor {
|
|
55
|
-
const hsl = isHslLike(input) ? getHslValue(input) : {...
|
|
47
|
+
const hsl = isHslLike(input) ? getHslValue(input) : {...COLOR_DEFAULTS.hsl};
|
|
56
48
|
|
|
57
|
-
const hue = hsl.hue %
|
|
58
|
-
const saturation = hsl.saturation /
|
|
59
|
-
const lightness = hsl.lightness /
|
|
49
|
+
const hue = hsl.hue % COLOR_MAX.degree;
|
|
50
|
+
const saturation = hsl.saturation / COLOR_MAX.percent;
|
|
51
|
+
const lightness = hsl.lightness / COLOR_MAX.percent;
|
|
60
52
|
|
|
61
53
|
return {
|
|
62
|
-
alpha: getAlphaValue((input as HSLAColor)?.alpha ??
|
|
54
|
+
alpha: getAlphaValue((input as HSLAColor)?.alpha ?? COLOR_MAX.percent),
|
|
63
55
|
blue: getHexValue(getHexyValue(hue, lightness, saturation, 4)),
|
|
64
56
|
green: getHexValue(getHexyValue(hue, lightness, saturation, 8)),
|
|
65
57
|
red: getHexValue(getHexyValue(hue, lightness, saturation, 0)),
|
|
@@ -71,15 +63,15 @@ export function getColorFromHsl<Type extends ColorType>(
|
|
|
71
63
|
type: Type,
|
|
72
64
|
): NonNullable<ColorState[Type]> {
|
|
73
65
|
switch (type) {
|
|
74
|
-
case
|
|
66
|
+
case COLOR_TYPE.hex:
|
|
75
67
|
state.hex = hslToHex(state.hsl!);
|
|
76
68
|
break;
|
|
77
69
|
|
|
78
|
-
case
|
|
70
|
+
case COLOR_TYPE.hwb:
|
|
79
71
|
state.hwb = hslToHwb(state.hsl!);
|
|
80
72
|
break;
|
|
81
73
|
|
|
82
|
-
case
|
|
74
|
+
case COLOR_TYPE.rgb:
|
|
83
75
|
state.rgb = hslToRgb(state.hsl!);
|
|
84
76
|
break;
|
|
85
77
|
}
|
|
@@ -91,7 +83,7 @@ function getHexyValue(hue: number, lightness: number, saturation: number, value:
|
|
|
91
83
|
const part = (value + hue / 30) % 12;
|
|
92
84
|
const mod = saturation * Math.min(lightness, 1 - lightness);
|
|
93
85
|
|
|
94
|
-
return (lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1))) *
|
|
86
|
+
return (lightness - mod * Math.max(-1, Math.min(part - 3, 9 - part, 1))) * COLOR_MAX.hex;
|
|
95
87
|
}
|
|
96
88
|
|
|
97
89
|
export function getHslValue(value: Record<keyof HSLColor, unknown>): HSLColor {
|
package/src/color/space/hwb.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE} from '../constants';
|
|
2
2
|
import {getAlphaValue} from '../misc/alpha';
|
|
3
3
|
import {getDegrees, getPercentage} from '../misc/get';
|
|
4
4
|
import {isHwbLike} from '../misc/is';
|
|
@@ -18,10 +18,10 @@ import {rgbToHex} from './rgb';
|
|
|
18
18
|
// #region Functions
|
|
19
19
|
|
|
20
20
|
export function convertHwbToHsl(input: HWBAColor | HWBColor): HSLAColor {
|
|
21
|
-
let {blackness, hue, whiteness} = isHwbLike(input) ? getHwbValue(input) :
|
|
21
|
+
let {blackness, hue, whiteness} = isHwbLike(input) ? getHwbValue(input) : COLOR_DEFAULTS.hwb;
|
|
22
22
|
|
|
23
|
-
blackness /=
|
|
24
|
-
whiteness /=
|
|
23
|
+
blackness /= COLOR_MAX.percent;
|
|
24
|
+
whiteness /= COLOR_MAX.percent;
|
|
25
25
|
|
|
26
26
|
if (blackness + whiteness > 1) {
|
|
27
27
|
const total = blackness + whiteness;
|
|
@@ -36,9 +36,9 @@ export function convertHwbToHsl(input: HWBAColor | HWBColor): HSLAColor {
|
|
|
36
36
|
|
|
37
37
|
return {
|
|
38
38
|
hue,
|
|
39
|
-
saturation: saturation *
|
|
40
|
-
lightness: lightness *
|
|
41
|
-
alpha: getAlphaValue((input as HWBAColor)?.alpha ??
|
|
39
|
+
saturation: saturation * COLOR_MAX.percent,
|
|
40
|
+
lightness: lightness * COLOR_MAX.percent,
|
|
41
|
+
alpha: getAlphaValue((input as HWBAColor)?.alpha ?? COLOR_MAX.percent),
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -47,15 +47,15 @@ export function getColorFromHwb<Type extends ColorType>(
|
|
|
47
47
|
type: Type,
|
|
48
48
|
): NonNullable<ColorState[Type]> {
|
|
49
49
|
switch (type) {
|
|
50
|
-
case
|
|
50
|
+
case COLOR_TYPE.hex:
|
|
51
51
|
state.hex = hwbToHex(state.hwb!);
|
|
52
52
|
break;
|
|
53
53
|
|
|
54
|
-
case
|
|
54
|
+
case COLOR_TYPE.hsl:
|
|
55
55
|
state.hsl = hwbToHsl(state.hwb!);
|
|
56
56
|
break;
|
|
57
57
|
|
|
58
|
-
case
|
|
58
|
+
case COLOR_TYPE.rgb:
|
|
59
59
|
state.rgb = hwbToRgb(state.hwb!);
|
|
60
60
|
break;
|
|
61
61
|
}
|
package/src/color/space/rgb.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {join} from '../../internal/string';
|
|
2
|
-
import {
|
|
2
|
+
import {COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE} from '../constants';
|
|
3
3
|
import {getAlpha, getAlphaValue} from '../misc/alpha';
|
|
4
4
|
import {getHexValue} from '../misc/get';
|
|
5
5
|
import {isRgbLike} from '../misc/is';
|
|
@@ -48,7 +48,7 @@ export function convertRgbToHex(rgb: RGBAColor | RGBColor, alpha: boolean): stri
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export function convertRgbToHsla(value: unknown): HSLAColor {
|
|
51
|
-
const rgb = isRgbLike(value) ? getRgbValue(value) : {...
|
|
51
|
+
const rgb = isRgbLike(value) ? getRgbValue(value) : {...COLOR_DEFAULTS.rgb};
|
|
52
52
|
const values = getRgbValues(rgb);
|
|
53
53
|
|
|
54
54
|
const {delta, max, min} = values;
|
|
@@ -68,14 +68,14 @@ export function convertRgbToHsla(value: unknown): HSLAColor {
|
|
|
68
68
|
|
|
69
69
|
return {
|
|
70
70
|
hue,
|
|
71
|
-
saturation: saturation *
|
|
72
|
-
lightness: lightness *
|
|
73
|
-
alpha: getAlphaValue((value as RGBAColor)?.alpha ??
|
|
71
|
+
saturation: saturation * COLOR_MAX.percent,
|
|
72
|
+
lightness: lightness * COLOR_MAX.percent,
|
|
73
|
+
alpha: getAlphaValue((value as RGBAColor)?.alpha ?? COLOR_MAX.percent),
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
export function convertRgbToHwba(value: unknown): HWBAColor {
|
|
78
|
-
const rgb = isRgbLike(value) ? getRgbValue(value) : {...
|
|
78
|
+
const rgb = isRgbLike(value) ? getRgbValue(value) : {...COLOR_DEFAULTS.rgb};
|
|
79
79
|
const values = getRgbValues(rgb);
|
|
80
80
|
|
|
81
81
|
const {delta, max, min} = values;
|
|
@@ -90,9 +90,9 @@ export function convertRgbToHwba(value: unknown): HWBAColor {
|
|
|
90
90
|
|
|
91
91
|
return {
|
|
92
92
|
hue,
|
|
93
|
-
whiteness: min *
|
|
94
|
-
blackness: (1 - max) *
|
|
95
|
-
alpha: getAlphaValue((value as RGBAColor)?.alpha ??
|
|
93
|
+
whiteness: min * COLOR_MAX.percent,
|
|
94
|
+
blackness: (1 - max) * COLOR_MAX.percent,
|
|
95
|
+
alpha: getAlphaValue((value as RGBAColor)?.alpha ?? COLOR_MAX.percent),
|
|
96
96
|
};
|
|
97
97
|
}
|
|
98
98
|
|
|
@@ -101,15 +101,15 @@ export function getColorFromRgb<Type extends ColorType>(
|
|
|
101
101
|
type: Type,
|
|
102
102
|
): NonNullable<ColorState[Type]> {
|
|
103
103
|
switch (type) {
|
|
104
|
-
case
|
|
104
|
+
case COLOR_TYPE.hex:
|
|
105
105
|
state.hex = rgbToHex(state.rgb!);
|
|
106
106
|
break;
|
|
107
107
|
|
|
108
|
-
case
|
|
108
|
+
case COLOR_TYPE.hsl:
|
|
109
109
|
state.hsl = rgbToHsl(state.rgb!);
|
|
110
110
|
break;
|
|
111
111
|
|
|
112
|
-
case
|
|
112
|
+
case COLOR_TYPE.hwb:
|
|
113
113
|
state.hwb = rgbToHwb(state.rgb!);
|
|
114
114
|
break;
|
|
115
115
|
}
|
|
@@ -141,9 +141,9 @@ export function getRgbValue(value: Record<keyof RGBColor, unknown>): RGBColor {
|
|
|
141
141
|
}
|
|
142
142
|
|
|
143
143
|
function getRgbValues(rgb: RGBColor): RgbValues {
|
|
144
|
-
const blue = rgb.blue /
|
|
145
|
-
const green = rgb.green /
|
|
146
|
-
const red = rgb.red /
|
|
144
|
+
const blue = rgb.blue / COLOR_MAX.hex;
|
|
145
|
+
const green = rgb.green / COLOR_MAX.hex;
|
|
146
|
+
const red = rgb.red / COLOR_MAX.hex;
|
|
147
147
|
|
|
148
148
|
const max = Math.max(blue, green, red);
|
|
149
149
|
const min = Math.min(blue, green, red);
|
|
@@ -163,7 +163,10 @@ function getRgbValues(rgb: RGBColor): RgbValues {
|
|
|
163
163
|
* @returns Hex color string
|
|
164
164
|
*/
|
|
165
165
|
export function rgbToHex(rgb: RGBAColor | RGBColor, alpha?: boolean): string {
|
|
166
|
-
return convertRgbToHex(
|
|
166
|
+
return convertRgbToHex(
|
|
167
|
+
isRgbLike(rgb) ? getRgbValue(rgb) : {...COLOR_DEFAULTS.rgb},
|
|
168
|
+
alpha ?? false,
|
|
169
|
+
);
|
|
167
170
|
}
|
|
168
171
|
|
|
169
172
|
/**
|
package/src/herald.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {noop} from './internal/function/misc';
|
|
2
|
-
import type {GenericCallback} from './models';
|
|
2
|
+
import type {GenericCallback, Unsubscriber} from './models';
|
|
3
3
|
|
|
4
4
|
// #region Types
|
|
5
5
|
|
|
@@ -57,8 +57,6 @@ export type Herald<Map extends Record<string, GenericCallback>> = {
|
|
|
57
57
|
unsubscribe<Event extends keyof Map>(event: Event, callback?: Map[Event]): void;
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
-
export type Unsubscriber = () => void;
|
|
61
|
-
|
|
62
60
|
// #endregion
|
|
63
61
|
|
|
64
62
|
// #region Functions
|
package/src/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ export * from './herald';
|
|
|
21
21
|
|
|
22
22
|
export * from './internal/function/misc';
|
|
23
23
|
export * from './internal/string';
|
|
24
|
+
export {isSubscription, type Subscription} from './internal/subscription';
|
|
24
25
|
export * from './internal/value/compare';
|
|
25
26
|
export * from './internal/value/equal';
|
|
26
27
|
export * from './internal/value/get';
|