@oscarpalmer/atoms 0.190.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.
Files changed (57) hide show
  1. package/dist/beacon.d.mts +6 -44
  2. package/dist/beacon.mjs +20 -67
  3. package/dist/color/constants.d.mts +82 -35
  4. package/dist/color/constants.mjs +106 -61
  5. package/dist/color/index.d.mts +3 -3
  6. package/dist/color/index.mjs +2 -2
  7. package/dist/color/instance.mjs +60 -35
  8. package/dist/color/misc/alpha.d.mts +1 -1
  9. package/dist/color/misc/alpha.mjs +12 -10
  10. package/dist/color/misc/format.d.mts +7 -0
  11. package/dist/color/misc/format.mjs +20 -0
  12. package/dist/color/misc/get.mjs +25 -25
  13. package/dist/color/misc/is.d.mts +0 -36
  14. package/dist/color/misc/is.mjs +22 -57
  15. package/dist/color/misc/state.d.mts +8 -6
  16. package/dist/color/misc/state.mjs +79 -88
  17. package/dist/color/models.d.mts +106 -6
  18. package/dist/color/space/hex.d.mts +3 -2
  19. package/dist/color/space/hex.mjs +22 -10
  20. package/dist/color/space/hsl.d.mts +3 -2
  21. package/dist/color/space/hsl.mjs +26 -14
  22. package/dist/color/space/hwb.d.mts +3 -2
  23. package/dist/color/space/hwb.mjs +20 -8
  24. package/dist/color/space/rgb.d.mts +3 -2
  25. package/dist/color/space/rgb.mjs +27 -15
  26. package/dist/herald.d.mts +2 -3
  27. package/dist/index.d.mts +139 -73
  28. package/dist/index.mjs +653 -477
  29. package/dist/internal/subscription.d.mts +52 -0
  30. package/dist/internal/subscription.mjs +127 -0
  31. package/dist/models.d.mts +5 -1
  32. package/package.json +4 -4
  33. package/src/beacon.ts +32 -117
  34. package/src/color/constants.ts +115 -66
  35. package/src/color/index.ts +26 -8
  36. package/src/color/instance.ts +113 -45
  37. package/src/color/misc/alpha.ts +18 -20
  38. package/src/color/misc/format.ts +29 -0
  39. package/src/color/misc/get.ts +31 -41
  40. package/src/color/misc/is.ts +31 -71
  41. package/src/color/misc/state.ts +165 -141
  42. package/src/color/models.ts +126 -5
  43. package/src/color/space/hex.ts +46 -18
  44. package/src/color/space/hsl.ts +44 -14
  45. package/src/color/space/hwb.ts +38 -8
  46. package/src/color/space/rgb.ts +48 -15
  47. package/src/herald.ts +1 -3
  48. package/src/index.ts +1 -0
  49. package/src/internal/subscription.ts +264 -0
  50. package/src/models.ts +5 -0
  51. package/src/string/fuzzy.ts +1 -1
  52. package/src/value/freeze.ts +2 -2
  53. package/src/value/shake.ts +1 -1
  54. package/src/value/transform.ts +1 -1
  55. package/dist/color/misc/index.d.mts +0 -7
  56. package/dist/color/misc/index.mjs +0 -15
  57. package/src/color/misc/index.ts +0 -29
@@ -1,121 +1,112 @@
1
- import { DEFAULT_HSL, DEFAULT_HWB, DEFAULT_RGB, HEX_BLACK, KEYS_HSL, KEYS_HWB, KEYS_RGB } from "../constants.mjs";
1
+ import { COLOR_DEFAULTS, COLOR_KEYS, COLOR_LENGTHS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
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 { getRgbValue, rgbToHex, rgbToHsl, rgbToHwb } from "../space/rgb.mjs";
6
- import { getNormalizedHex, hexToHsl } from "../space/hex.mjs";
7
- import { getHslValue, hslToHex, hslToHwb, hslToRgb } from "../space/hsl.mjs";
8
- import { getHwbValue, hwbToHex, hwbToHsl, hwbToRgb } from "../space/hwb.mjs";
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)) return {
13
- hex: value.hex,
14
- hsl: value.hsl,
15
- hwb: value.hwb,
16
- rgb: value.rgb,
17
- alpha: getAlpha(value.alpha)
18
- };
19
- const state = {};
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.alpha = getAlpha(value.alpha);
22
- if (KEYS_HSL.every((key) => key in value)) return getColorStateForHsl(state, value);
23
- if (KEYS_HWB.every((key) => key in value)) return getColorStateForHwb(state, value);
24
- if (KEYS_RGB.every((key) => key in value)) return getColorStateForRgb(state, value);
26
+ const state = {
27
+ alpha: getAlpha(value.alpha, false),
28
+ origin: void 0
29
+ };
30
+ if (COLOR_KEYS.hsl.every((key) => key in value)) {
31
+ state.hsl = getHslValue(value);
32
+ state.origin = COLOR_TYPE.hsl;
33
+ } else if (COLOR_KEYS.hwb.every((key) => key in value)) {
34
+ state.hwb = getHwbValue(value);
35
+ state.origin = COLOR_TYPE.hwb;
36
+ } else if (COLOR_KEYS.rgb.every((key) => key in value)) {
37
+ state.rgb = getRgbValue(value);
38
+ state.origin = COLOR_TYPE.rgb;
39
+ }
40
+ if (state.origin != null) return state;
25
41
  }
26
- return getDefaultColorState(state);
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
- hsl,
35
- alpha: getAlpha(normalized.slice(6)),
36
- hwb: hslToHwb(hsl),
37
- rgb: hslToRgb(hsl)
47
+ hex: normalized.slice(0, COLOR_LENGTHS.hexLong),
48
+ alpha: getAlpha(normalized.slice(COLOR_LENGTHS.hexLong), true),
49
+ origin: COLOR_TYPE.hex
38
50
  };
39
51
  }
40
- function getColorStateForHsl(state, value) {
41
- state.hsl = getHslValue(value);
42
- state.rgb = hslToRgb(state.hsl);
43
- state.hex = hslToHex(state.hsl);
44
- state.hwb = hslToHwb(state.hsl);
45
- return state;
46
- }
47
- function getColorStateForHwb(state, value) {
48
- state.hwb = getHwbValue(value);
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(COLOR_MAX.percent, false),
55
+ hex: COLOR_DEFAULTS.hexBlack,
56
+ hsl: { ...COLOR_DEFAULTS.hsl },
57
+ hwb: { ...COLOR_DEFAULTS.hwb },
58
+ rgb: { ...COLOR_DEFAULTS.rgb },
59
+ origin: COLOR_TYPE.hex
60
+ };
60
61
  }
61
- function getDefaultColorState(state) {
62
- state.alpha = getAlpha(100);
63
- state.hex = HEX_BLACK;
64
- state.hsl = { ...DEFAULT_HSL };
65
- state.hwb = { ...DEFAULT_HWB };
66
- state.rgb = { ...DEFAULT_RGB };
67
- return state;
62
+ function setColorValue(color, state, subscriptions, 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 === COLOR_TYPE.hex);
70
+ for (const type of COLOR_TYPE.all) {
71
+ const callbacks = subscriptions.values.to.keyed?.get(type);
72
+ if (callbacks != null) for (const callback of callbacks.values()) callback(color[type]);
73
+ }
74
+ for (const callback of subscriptions.values.to.any.values()) callback(color);
68
75
  }
69
- function setHexColor(state, value, alpha) {
76
+ function setHexColor(color, state, subscriptions, value, alpha) {
70
77
  if (!isHexColor(value) || !alpha && value === state.hex) return;
71
78
  const normalized = getNormalizedHex(value, true);
72
- const hex = normalized.slice(0, 6);
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));
79
+ setColorValue(color, state, subscriptions, COLOR_TYPE.hex, normalized.slice(0, COLOR_LENGTHS.hexLong), alpha ? normalized.slice(COLOR_LENGTHS.hexLong) : void 0);
79
80
  }
80
- function setHSLColor(state, value, alpha) {
81
+ function setHSLColor(color, state, subscriptions, value, alpha) {
81
82
  if (!isHslLike(value)) return;
82
- const hsl = {
83
+ setColorValue(color, state, subscriptions, COLOR_TYPE.hsl, {
83
84
  hue: getDegrees(value.hue),
84
85
  saturation: getPercentage(value.saturation),
85
86
  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);
87
+ }, alpha ? value.alpha : void 0);
92
88
  }
93
- function setHWBColor(state, value, alpha) {
89
+ function setHWBColor(color, state, subscriptions, value, alpha) {
94
90
  if (!isHwbLike(value)) return;
95
- const hwb = {
91
+ setColorValue(color, state, subscriptions, COLOR_TYPE.hwb, {
96
92
  hue: getDegrees(value.hue),
97
93
  whiteness: getPercentage(value.whiteness),
98
94
  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);
95
+ }, alpha ? value.alpha : void 0);
105
96
  }
106
- function setRGBColor(state, value, alpha) {
97
+ function setRGBColor(color, state, subscriptions, value, alpha) {
107
98
  if (!isRgbLike(value)) return;
108
- const rgb = {
109
- blue: getHexValue(value.blue),
99
+ setColorValue(color, state, subscriptions, COLOR_TYPE.rgb, {
100
+ red: getHexValue(value.red),
110
101
  green: getHexValue(value.green),
111
- red: getHexValue(value.red)
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);
102
+ blue: getHexValue(value.blue)
103
+ }, alpha ? value.alpha : void 0);
119
104
  }
105
+ const getters = {
106
+ hex: getColorFromHex,
107
+ hsl: getColorFromHsl,
108
+ hwb: getColorFromHwb,
109
+ rgb: getColorFromRgb
110
+ };
120
111
  //#endregion
121
- export { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
112
+ export { getColorFromState, getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
@@ -1,3 +1,5 @@
1
+ import { Subscription } from "../internal/subscription.mjs";
2
+ import { COLOR_ALPHA, COLOR_TYPE } from "./constants.mjs";
1
3
  //#region src/color/models.d.ts
2
4
  type Alpha = {
3
5
  hex: string;
@@ -101,6 +103,12 @@ type Color = {
101
103
  * @param value New _HWBA_ color
102
104
  */
103
105
  set hwba(value: HWBAColor);
106
+ /**
107
+ * Last color format that was set on the color
108
+ *
109
+ * @returns Color type
110
+ */
111
+ get origin(): ColorType;
104
112
  /**
105
113
  * Get the color as an _RGB_ color
106
114
  *
@@ -125,6 +133,75 @@ type Color = {
125
133
  * @param value New _RGBA_ color
126
134
  */
127
135
  set rgba(value: RGBAColor);
136
+ /**
137
+ * Subscribe to alpha channel _(opacity)_ changes of the color
138
+ *
139
+ * @param key Color property to subscribe to
140
+ * @param callback Callback function for changes
141
+ */
142
+ subscribe(key: 'alpha', callback: (value: number) => void): Subscription;
143
+ /**
144
+ * Subscribe to hex color changes of the color
145
+ *
146
+ * @param key Color type to subscribe to
147
+ * @param callback Callback function for changes
148
+ */
149
+ subscribe(key: typeof COLOR_TYPE.hex, callback: (value: string) => void): Subscription;
150
+ /**
151
+ * Subscribe to hex color changes of the color with alpha channel _(opacity)_
152
+ *
153
+ * @param key Color type to subscribe to
154
+ * @param callback Callback function for changes
155
+ */
156
+ subscribe(key: typeof COLOR_TYPE.hexa, callback: (value: string) => void): Subscription;
157
+ /**
158
+ * Subscribe to _HSL_ color changes of the color
159
+ *
160
+ * @param key Color type to subscribe to
161
+ * @param callback Callback function for changes
162
+ */
163
+ subscribe(key: typeof COLOR_TYPE.hsl, callback: (value: HSLColor) => void): Subscription;
164
+ /**
165
+ * Subscribe to _HSL(A)_ color changes of the color
166
+ *
167
+ * @param key Color type to subscribe to
168
+ * @param callback Callback function for changes
169
+ */
170
+ subscribe(key: typeof COLOR_TYPE.hsla, callback: (value: HSLAColor) => void): Subscription;
171
+ /**
172
+ * Subscribe to _HWB_ color changes of the color
173
+ *
174
+ * @param key Color type to subscribe to
175
+ * @param callback Callback function for changes
176
+ */
177
+ subscribe(key: typeof COLOR_TYPE.hwb, callback: (value: HWBColor) => void): Subscription;
178
+ /**
179
+ * Subscribe to _HWB(A)_ color changes of the color
180
+ *
181
+ * @param key Color type to subscribe to
182
+ * @param callback Callback function for changes
183
+ */
184
+ subscribe(key: typeof COLOR_TYPE.hwba, callback: (value: HWBAColor) => void): Subscription;
185
+ /**
186
+ * Subscribe to _RGB_ color changes of the color
187
+ *
188
+ * @param key Color type to subscribe to
189
+ * @param callback Callback function for changes
190
+ */
191
+ subscribe(key: typeof COLOR_TYPE.rgb, callback: (value: RGBColor) => void): Subscription;
192
+ /**
193
+ * Subscribe to _RGBA_ color changes of the color
194
+ *
195
+ * @param key Color type to subscribe to
196
+ * @param callback Callback function for changes
197
+ */
198
+ subscribe(key: typeof COLOR_TYPE.rgba, callback: (value: RGBAColor) => void): Subscription;
199
+ /**
200
+ * Subscribe to any color changes of the color
201
+ *
202
+ * @param callback Callback function for changes
203
+ */
204
+ subscribe(callback: (value: Color) => void): Subscription;
128
205
  /**
129
206
  * Get the color as a hex string
130
207
  *
@@ -159,6 +236,12 @@ type Color = {
159
236
  * @returns Hex color string
160
237
  */
161
238
  toString(): string;
239
+ /**
240
+ * Unsubscribe from all color changes of the color
241
+ *
242
+ * _(To unsubscribe from a specific color change, use the `unsubscribe()` method on the `Subscription` returned by the individual `subscribe()` method)_
243
+ */
244
+ unsubscribe(): void;
162
245
  };
163
246
  type ColorWithAlpha = {
164
247
  /**
@@ -223,13 +306,30 @@ type RGBColor = {
223
306
  */
224
307
  red: number;
225
308
  };
226
- type ColorProperty = 'alpha' | keyof HSLColor | keyof HWBColor | keyof RGBColor;
309
+ type ColorProperty = typeof COLOR_ALPHA.name | keyof HSLColor | keyof HWBColor | keyof RGBColor;
227
310
  type ColorState = {
228
311
  alpha: Alpha;
229
- hex: string;
230
- hsl: HSLColor;
231
- hwb: HWBColor;
232
- rgb: RGBColor;
312
+ hex?: string;
313
+ hsl?: HSLColor;
314
+ hwb?: HWBColor;
315
+ origin: ColorType;
316
+ rgb?: RGBColor;
233
317
  };
318
+ /**
319
+ * Base color types
320
+ */
321
+ type ColorType = 'hex' | 'hsl' | 'hwb' | 'rgb';
322
+ /**
323
+ * Extended color types
324
+ */
325
+ type ColorTypeExtended = ColorType | 'hsla' | 'hwba' | 'rgba';
326
+ /**
327
+ * Base color values
328
+ */
329
+ type ColorValue = string | HSLColor | HWBColor | RGBColor;
330
+ /**
331
+ * Extended color values
332
+ */
333
+ type ColorValueExtended = ColorValue | HSLAColor | HWBAColor | RGBAColor;
234
334
  //#endregion
235
- export { Alpha, Color, ColorProperty, ColorState, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor };
335
+ 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 };
@@ -1,22 +1,34 @@
1
1
  import { join } from "../../internal/string.mjs";
2
- import { ALPHA_FULL_HEX_LONG, EXPRESSION_HEX_LONG, EXPRESSION_PREFIX, HEX_BLACK } from "../constants.mjs";
2
+ import { COLOR_ALPHA, COLOR_DEFAULTS, COLOR_EXPRESSION, COLOR_LENGTHS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
3
3
  import { getHexValue, getPercentage } from "../misc/get.mjs";
4
4
  import { isHexColor } from "../misc/is.mjs";
5
5
  import { convertRgbToHsla, convertRgbToHwba } from "./rgb.mjs";
6
6
  //#region src/color/space/hex.ts
7
7
  function convertHexToRgba(value) {
8
8
  const normalized = getNormalizedHex(value, true);
9
- const pairs = EXPRESSION_HEX_LONG.exec(normalized);
9
+ const pairs = COLOR_EXPRESSION.hexLong.exec(normalized);
10
10
  const values = [];
11
11
  const { length } = pairs;
12
12
  for (let index = 1; index < length; index += 1) values.push(Number.parseInt(pairs[index], 16));
13
13
  return {
14
- alpha: getPercentage(values[3] / 255 * 100),
14
+ alpha: getPercentage(values[3] / COLOR_MAX.hex * 100),
15
15
  blue: getHexValue(values[2]),
16
16
  green: getHexValue(values[1]),
17
17
  red: getHexValue(values[0])
18
18
  };
19
19
  }
20
+ function getColorFromHex(state, type) {
21
+ switch (type) {
22
+ case COLOR_TYPE.hsl:
23
+ state.hsl ??= hexToHsl(state.hex);
24
+ break;
25
+ case COLOR_TYPE.hwb:
26
+ state.hwb ??= hexToHwb(state.hex);
27
+ break;
28
+ case COLOR_TYPE.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
  *
@@ -28,14 +40,14 @@ function convertHexToRgba(value) {
28
40
  */
29
41
  function getNormalizedHex(value, alpha) {
30
42
  const includeAlpha = alpha ?? false;
31
- if (!isHexColor(value)) return `${HEX_BLACK}${includeAlpha ? ALPHA_FULL_HEX_LONG : ""}`;
32
- const normalized = value.replace(EXPRESSION_PREFIX, "");
33
- if (normalized.length < 6) {
34
- const hex = normalized.slice(0, 3);
35
- const a = includeAlpha ? normalized[3] ?? "f" : "";
43
+ if (!isHexColor(value)) return `${COLOR_DEFAULTS.hexBlack}${includeAlpha ? COLOR_ALPHA.fullHexLong : ""}`;
44
+ const normalized = value.replace(COLOR_EXPRESSION.prefix, "");
45
+ if (normalized.length < COLOR_LENGTHS.hexLong) {
46
+ const hex = normalized.slice(0, COLOR_LENGTHS.hexShort);
47
+ const a = includeAlpha ? normalized[COLOR_LENGTHS.hexShort] ?? COLOR_ALPHA.fullHexShort : "";
36
48
  return join(`${hex}${a}`.split("").map((character) => character.repeat(2)));
37
49
  }
38
- return `${normalized.slice(0, 6)}${includeAlpha ? normalized.slice(6) || ALPHA_FULL_HEX_LONG : ""}`;
50
+ return `${normalized.slice(0, COLOR_LENGTHS.hexLong)}${includeAlpha ? normalized.slice(COLOR_LENGTHS.hexLong) || COLOR_ALPHA.fullHexLong : ""}`;
39
51
  }
40
52
  /**
41
53
  * Convert a hex color string to a _HSL_ color
@@ -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 };
@@ -1,39 +1,51 @@
1
- import { DEFAULT_HSL } from "../constants.mjs";
1
+ import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
2
2
  import { getAlphaValue } from "../misc/alpha.mjs";
3
3
  import { getDegrees, getHexValue, getPercentage } from "../misc/get.mjs";
4
4
  import { isHslLike } from "../misc/is.mjs";
5
5
  import { convertRgbToHex } from "./rgb.mjs";
6
6
  //#region src/color/space/hsl.ts
7
7
  function convertHslToHwba(input) {
8
- let { hue, lightness, saturation } = isHslLike(input) ? getHslValue(input) : { ...DEFAULT_HSL };
9
- lightness /= 100;
10
- saturation /= 100;
8
+ let { hue, lightness, saturation } = isHslLike(input) ? getHslValue(input) : { ...COLOR_DEFAULTS.hsl };
9
+ lightness /= COLOR_MAX.percent;
10
+ saturation /= COLOR_MAX.percent;
11
11
  const value = lightness + saturation * Math.min(lightness, 1 - lightness);
12
12
  if (value === 0) saturation = 0;
13
13
  else saturation = 2 * (1 - lightness / value);
14
14
  const blackness = 1 - value;
15
15
  return {
16
16
  hue,
17
- whiteness: (1 - saturation) * value * 100,
18
- blackness: blackness * 100,
19
- alpha: getAlphaValue(input?.alpha ?? 100)
17
+ whiteness: (1 - saturation) * value * COLOR_MAX.percent,
18
+ blackness: blackness * COLOR_MAX.percent,
19
+ alpha: getAlphaValue(input?.alpha ?? COLOR_MAX.percent)
20
20
  };
21
21
  }
22
22
  function convertHslToRgba(input) {
23
- const hsl = isHslLike(input) ? getHslValue(input) : { ...DEFAULT_HSL };
24
- const hue = hsl.hue % 360;
25
- const saturation = hsl.saturation / 100;
26
- const lightness = hsl.lightness / 100;
23
+ const hsl = isHslLike(input) ? getHslValue(input) : { ...COLOR_DEFAULTS.hsl };
24
+ const hue = hsl.hue % COLOR_MAX.degree;
25
+ const saturation = hsl.saturation / COLOR_MAX.percent;
26
+ const lightness = hsl.lightness / COLOR_MAX.percent;
27
27
  return {
28
- alpha: getAlphaValue(input?.alpha ?? 100),
28
+ alpha: getAlphaValue(input?.alpha ?? COLOR_MAX.percent),
29
29
  blue: getHexValue(getHexyValue(hue, lightness, saturation, 4)),
30
30
  green: getHexValue(getHexyValue(hue, lightness, saturation, 8)),
31
31
  red: getHexValue(getHexyValue(hue, lightness, saturation, 0))
32
32
  };
33
33
  }
34
+ function getColorFromHsl(state, type) {
35
+ switch (type) {
36
+ case COLOR_TYPE.hex:
37
+ state.hex = hslToHex(state.hsl);
38
+ break;
39
+ case COLOR_TYPE.hwb:
40
+ state.hwb = hslToHwb(state.hsl);
41
+ break;
42
+ case COLOR_TYPE.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
- return (lightness - saturation * Math.min(lightness, 1 - lightness) * Math.max(-1, Math.min(part - 3, 9 - part, 1))) * 255;
48
+ return (lightness - saturation * Math.min(lightness, 1 - lightness) * Math.max(-1, Math.min(part - 3, 9 - part, 1))) * COLOR_MAX.hex;
37
49
  }
38
50
  function getHslValue(value) {
39
51
  return {
@@ -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 };
@@ -1,4 +1,4 @@
1
- import { DEFAULT_HWB } from "../constants.mjs";
1
+ import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
2
2
  import { getAlphaValue } from "../misc/alpha.mjs";
3
3
  import { getDegrees, getPercentage } from "../misc/get.mjs";
4
4
  import { isHwbLike } from "../misc/is.mjs";
@@ -6,9 +6,9 @@ import { rgbToHex } from "./rgb.mjs";
6
6
  import { hslToRgb, hslToRgba } from "./hsl.mjs";
7
7
  //#region src/color/space/hwb.ts
8
8
  function convertHwbToHsl(input) {
9
- let { blackness, hue, whiteness } = isHwbLike(input) ? getHwbValue(input) : DEFAULT_HWB;
10
- blackness /= 100;
11
- whiteness /= 100;
9
+ let { blackness, hue, whiteness } = isHwbLike(input) ? getHwbValue(input) : COLOR_DEFAULTS.hwb;
10
+ blackness /= COLOR_MAX.percent;
11
+ whiteness /= COLOR_MAX.percent;
12
12
  if (blackness + whiteness > 1) {
13
13
  const total = blackness + whiteness;
14
14
  blackness /= total;
@@ -17,11 +17,23 @@ function convertHwbToHsl(input) {
17
17
  const lightness = (1 - blackness + whiteness) / 2;
18
18
  return {
19
19
  hue,
20
- saturation: getHwbSaturation(blackness, whiteness, lightness) * 100,
21
- lightness: lightness * 100,
22
- alpha: getAlphaValue(input?.alpha ?? 100)
20
+ saturation: getHwbSaturation(blackness, whiteness, lightness) * COLOR_MAX.percent,
21
+ lightness: lightness * COLOR_MAX.percent,
22
+ alpha: getAlphaValue(input?.alpha ?? COLOR_MAX.percent)
23
23
  };
24
24
  }
25
+ function getColorFromHwb(state, type) {
26
+ switch (type) {
27
+ case COLOR_TYPE.hex:
28
+ state.hex = hwbToHex(state.hwb);
29
+ break;
30
+ case COLOR_TYPE.hsl:
31
+ state.hsl = hwbToHsl(state.hwb);
32
+ break;
33
+ case COLOR_TYPE.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 };