@oscarpalmer/atoms 0.188.1 → 0.189.1

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 (129) hide show
  1. package/dist/array/difference.d.mts +0 -1
  2. package/dist/array/exists.d.mts +0 -1
  3. package/dist/array/filter.d.mts +0 -1
  4. package/dist/array/find.d.mts +0 -1
  5. package/dist/array/first.d.mts +0 -1
  6. package/dist/array/flatten.d.mts +0 -1
  7. package/dist/array/get.d.mts +0 -1
  8. package/dist/array/group-by.d.mts +0 -1
  9. package/dist/array/last.d.mts +0 -1
  10. package/dist/array/match.d.mts +0 -1
  11. package/dist/array/match.mjs +3 -3
  12. package/dist/array/move.d.mts +0 -1
  13. package/dist/array/partition.d.mts +0 -1
  14. package/dist/array/select.d.mts +0 -1
  15. package/dist/array/select.mjs +2 -1
  16. package/dist/array/single.d.mts +0 -1
  17. package/dist/array/slice.d.mts +0 -1
  18. package/dist/array/sort.d.mts +1 -2
  19. package/dist/array/sort.mjs +0 -1
  20. package/dist/array/swap.d.mts +0 -1
  21. package/dist/array/to-map.d.mts +0 -1
  22. package/dist/array/to-record.d.mts +0 -1
  23. package/dist/array/to-set.d.mts +0 -1
  24. package/dist/array/toggle.d.mts +0 -1
  25. package/dist/array/unique.d.mts +0 -1
  26. package/dist/array/update.d.mts +0 -1
  27. package/dist/color/constants.d.mts +9 -10
  28. package/dist/color/constants.mjs +15 -7
  29. package/dist/color/index.d.mts +9 -9
  30. package/dist/color/index.mjs +8 -7
  31. package/dist/color/instance.d.mts +35 -8
  32. package/dist/color/instance.mjs +53 -13
  33. package/dist/color/misc/alpha.d.mts +0 -1
  34. package/dist/color/misc/alpha.mjs +5 -5
  35. package/dist/color/misc/get.d.mts +42 -13
  36. package/dist/color/misc/get.mjs +45 -24
  37. package/dist/color/misc/index.d.mts +4 -4
  38. package/dist/color/misc/index.mjs +12 -12
  39. package/dist/color/misc/is.d.mts +56 -3
  40. package/dist/color/misc/is.mjs +76 -5
  41. package/dist/color/misc/state.d.mts +2 -2
  42. package/dist/color/misc/state.mjs +79 -43
  43. package/dist/color/models.d.mts +18 -2
  44. package/dist/color/space/hex.d.mts +38 -5
  45. package/dist/color/space/hex.mjs +57 -9
  46. package/dist/color/space/hsl.d.mts +33 -7
  47. package/dist/color/space/hsl.mjs +65 -14
  48. package/dist/color/space/hwb.d.mts +52 -0
  49. package/dist/color/space/hwb.mjs +99 -0
  50. package/dist/color/space/rgb.d.mts +26 -5
  51. package/dist/color/space/rgb.mjs +82 -28
  52. package/dist/function/assert.d.mts +1 -2
  53. package/dist/function/limit.d.mts +0 -1
  54. package/dist/function/memoize.d.mts +0 -1
  55. package/dist/function/once.d.mts +0 -1
  56. package/dist/function/retry.d.mts +0 -1
  57. package/dist/function/work.d.mts +0 -1
  58. package/dist/herald.d.mts +0 -1
  59. package/dist/index.d.mts +296 -51
  60. package/dist/index.mjs +661 -260
  61. package/dist/internal/array/callbacks.d.mts +0 -1
  62. package/dist/internal/array/callbacks.mjs +0 -1
  63. package/dist/internal/array/group.d.mts +0 -1
  64. package/dist/internal/array/index-of.d.mts +0 -1
  65. package/dist/internal/function/timer.d.mts +0 -1
  66. package/dist/internal/is.d.mts +0 -1
  67. package/dist/internal/is.mjs +1 -1
  68. package/dist/internal/math/aggregate.d.mts +0 -1
  69. package/dist/internal/math/misc.d.mts +12 -0
  70. package/dist/internal/math/misc.mjs +19 -0
  71. package/dist/internal/result.d.mts +0 -1
  72. package/dist/internal/value/compare.d.mts +0 -1
  73. package/dist/internal/value/equal.d.mts +0 -1
  74. package/dist/internal/value/get.d.mts +0 -1
  75. package/dist/internal/value/handlers.d.mts +0 -1
  76. package/dist/internal/value/has.d.mts +0 -1
  77. package/dist/internal/value/misc.d.mts +0 -1
  78. package/dist/internal/value/set.d.mts +0 -1
  79. package/dist/is.d.mts +0 -1
  80. package/dist/math.d.mts +1 -9
  81. package/dist/math.mjs +1 -16
  82. package/dist/models.d.mts +6 -6
  83. package/dist/promise/delay.d.mts +0 -1
  84. package/dist/promise/helpers.d.mts +0 -1
  85. package/dist/promise/index.d.mts +0 -1
  86. package/dist/promise/misc.d.mts +0 -1
  87. package/dist/promise/models.d.mts +4 -5
  88. package/dist/promise/models.mjs +1 -1
  89. package/dist/promise/timed.d.mts +0 -1
  90. package/dist/query.d.mts +0 -1
  91. package/dist/query.mjs +1 -1
  92. package/dist/queue.d.mts +0 -1
  93. package/dist/random.d.mts +0 -1
  94. package/dist/result/index.d.mts +0 -1
  95. package/dist/result/index.mjs +2 -1
  96. package/dist/result/match.d.mts +0 -1
  97. package/dist/result/misc.d.mts +0 -1
  98. package/dist/result/models.d.mts +0 -1
  99. package/dist/result/work/flow.d.mts +0 -1
  100. package/dist/result/work/pipe.d.mts +0 -1
  101. package/dist/string/fuzzy.d.mts +0 -1
  102. package/dist/string/index.d.mts +0 -1
  103. package/dist/string/index.mjs +1 -1
  104. package/dist/string/template.d.mts +0 -1
  105. package/dist/value/clone.d.mts +0 -1
  106. package/dist/value/freeze.d.mts +1 -2
  107. package/dist/value/merge.d.mts +0 -1
  108. package/dist/value/omit.d.mts +0 -1
  109. package/dist/value/pick.d.mts +0 -1
  110. package/dist/value/shake.d.mts +1 -2
  111. package/dist/value/smush.d.mts +1 -2
  112. package/dist/value/transform.d.mts +1 -2
  113. package/dist/value/unsmush.d.mts +1 -2
  114. package/package.json +5 -5
  115. package/src/color/constants.ts +17 -13
  116. package/src/color/index.ts +21 -6
  117. package/src/color/instance.ts +67 -14
  118. package/src/color/misc/alpha.ts +6 -10
  119. package/src/color/misc/get.ts +70 -16
  120. package/src/color/misc/index.ts +18 -17
  121. package/src/color/misc/is.ts +90 -4
  122. package/src/color/misc/state.ts +126 -40
  123. package/src/color/models.ts +19 -1
  124. package/src/color/space/hex.ts +55 -8
  125. package/src/color/space/hsl.ts +80 -14
  126. package/src/color/space/hwb.ts +123 -0
  127. package/src/color/space/rgb.ts +108 -27
  128. package/src/internal/math/misc.ts +28 -0
  129. package/src/math.ts +2 -29
@@ -1,72 +1,106 @@
1
- import { DEFAULT_HSL, DEFAULT_RGB, HEX_BLACK, KEYS_HSL, KEYS_RGB } from "../constants.mjs";
1
+ import { DEFAULT_HSL, DEFAULT_HWB, DEFAULT_RGB, HEX_BLACK, KEYS_HSL, KEYS_HWB, KEYS_RGB } from "../constants.mjs";
2
2
  import { getAlpha } from "./alpha.mjs";
3
- import { isColor, isHexColor, isHslLike, isRgbLike } from "./is.mjs";
4
3
  import { getDegrees, getHexValue, getPercentage } from "./get.mjs";
5
- import { getRgbValue, rgbToHex, rgbToHsl } from "../space/rgb.mjs";
6
- import { getNormalizedHex, hexToRgb } from "../space/hex.mjs";
7
- import { getHslValue, hslToRgb } from "../space/hsl.mjs";
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";
8
9
  //#region src/color/misc/state.ts
9
10
  function getColorState(value) {
10
- if (typeof value === "string") {
11
- const normalized = getNormalizedHex(value, true);
12
- const hex = normalized.slice(0, 6);
13
- const rgb = hexToRgb(hex);
14
- return {
15
- hex,
16
- rgb,
17
- alpha: getAlpha(normalized.slice(6)),
18
- hsl: rgbToHsl(rgb)
19
- };
20
- }
11
+ if (typeof value === "string") return getColorStateForHex(value);
21
12
  if (isColor(value)) return {
22
13
  hex: value.hex,
23
14
  hsl: value.hsl,
15
+ hwb: value.hwb,
24
16
  rgb: value.rgb,
25
17
  alpha: getAlpha(value.alpha)
26
18
  };
27
19
  const state = {};
28
20
  if (typeof value === "object" && value !== null) {
29
21
  state.alpha = getAlpha(value.alpha);
30
- if (KEYS_HSL.every((key) => key in value)) {
31
- state.hsl = getHslValue(value);
32
- state.rgb = hslToRgb(state.hsl);
33
- state.hex = rgbToHex(state.rgb);
34
- return state;
35
- }
36
- if (KEYS_RGB.every((key) => key in value)) {
37
- state.rgb = getRgbValue(value);
38
- state.hex = rgbToHex(state.rgb);
39
- state.hsl = rgbToHsl(state.rgb);
40
- return state;
41
- }
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);
42
25
  }
43
- state.alpha ??= getAlpha(1);
44
- state.hex ??= HEX_BLACK;
45
- state.hsl ??= { ...DEFAULT_HSL };
46
- state.rgb ??= { ...DEFAULT_RGB };
26
+ return getDefaultColorState(state);
27
+ }
28
+ function getColorStateForHex(value) {
29
+ const normalized = getNormalizedHex(value, true);
30
+ const hex = normalized.slice(0, 6);
31
+ const hsl = hexToHsl(hex);
32
+ return {
33
+ hex,
34
+ hsl,
35
+ alpha: getAlpha(normalized.slice(6)),
36
+ hwb: hslToHwb(hsl),
37
+ rgb: hslToRgb(hsl)
38
+ };
39
+ }
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;
60
+ }
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 };
47
67
  return state;
48
68
  }
49
69
  function setHexColor(state, value, alpha) {
50
70
  if (!isHexColor(value) || !alpha && value === state.hex) return;
51
71
  const normalized = getNormalizedHex(value, true);
52
72
  const hex = normalized.slice(0, 6);
53
- const rgb = hexToRgb(hex);
73
+ const hsl = hexToHsl(hex);
54
74
  state.hex = hex;
55
- state.hsl = rgbToHsl(rgb);
56
- state.rgb = rgb;
75
+ state.hsl = hsl;
76
+ state.rgb = hslToRgb(hsl);
77
+ state.hwb = hslToHwb(hsl);
57
78
  if (alpha) state.alpha = getAlpha(normalized.slice(6));
58
79
  }
59
80
  function setHSLColor(state, value, alpha) {
60
81
  if (!isHslLike(value)) return;
61
82
  const hsl = {
62
83
  hue: getDegrees(value.hue),
63
- lightness: getPercentage(value.lightness),
64
- saturation: getPercentage(value.saturation)
84
+ saturation: getPercentage(value.saturation),
85
+ lightness: getPercentage(value.lightness)
65
86
  };
66
- const rgb = hslToRgb(hsl);
67
- state.hex = rgbToHex(rgb);
68
87
  state.hsl = hsl;
69
- state.rgb = rgb;
88
+ state.hex = hslToHex(hsl);
89
+ state.hwb = hslToHwb(hsl);
90
+ state.rgb = hslToRgb(hsl);
91
+ if (alpha) state.alpha = getAlpha(value.alpha);
92
+ }
93
+ function setHWBColor(state, value, alpha) {
94
+ if (!isHwbLike(value)) return;
95
+ const hwb = {
96
+ hue: getDegrees(value.hue),
97
+ whiteness: getPercentage(value.whiteness),
98
+ blackness: getPercentage(value.blackness)
99
+ };
100
+ state.hwb = hwb;
101
+ state.hex = hwbToHex(hwb);
102
+ state.hsl = hwbToHsl(hwb);
103
+ state.rgb = hwbToRgb(hwb);
70
104
  if (alpha) state.alpha = getAlpha(value.alpha);
71
105
  }
72
106
  function setRGBColor(state, value, alpha) {
@@ -76,10 +110,12 @@ function setRGBColor(state, value, alpha) {
76
110
  green: getHexValue(value.green),
77
111
  red: getHexValue(value.red)
78
112
  };
79
- state.hex = rgbToHex(rgb);
80
- state.hsl = rgbToHsl(rgb);
113
+ const hsl = rgbToHsl(rgb);
114
+ state.hsl = hsl;
81
115
  state.rgb = rgb;
116
+ state.hex = hslToHex(hsl);
117
+ state.hwb = hslToHwb(hsl);
82
118
  if (alpha) state.alpha = getAlpha(value.alpha);
83
119
  }
84
120
  //#endregion
85
- export { getColorState, setHSLColor, setHexColor, setRGBColor };
121
+ export { getColorState, setHSLColor, setHWBColor, setHexColor, setRGBColor };
@@ -30,6 +30,21 @@ type HSLColor = {
30
30
  */
31
31
  saturation: number;
32
32
  };
33
+ type HWBColor = {
34
+ /**
35
+ * Blackness of the color _(in percentage; 0-100)_
36
+ */
37
+ blackness: number;
38
+ /**
39
+ * Hue of the color _(in degrees; 0-360)_
40
+ */
41
+ hue: number;
42
+ /**
43
+ * Whiteness of the color _(in percentage; 0-100)_
44
+ */
45
+ whiteness: number;
46
+ };
47
+ type HWBAColor = HWBColor & ColorWithAlpha;
33
48
  /**
34
49
  * An _RGB_ color with an alpha channel _(opacity)_
35
50
  */
@@ -51,13 +66,14 @@ type RGBColor = {
51
66
  */
52
67
  red: number;
53
68
  };
54
- type ColorProperty = 'alpha' | 'blue' | 'green' | 'hue' | 'lightness' | 'red' | 'saturation';
69
+ type ColorProperty = 'alpha' | keyof HSLColor | keyof HWBColor | keyof RGBColor;
55
70
  type ColorSpace = 'hsl' | 'rgb';
56
71
  type ColorState = {
57
72
  alpha: Alpha;
58
73
  hex: string;
59
74
  hsl: HSLColor;
75
+ hwb: HWBColor;
60
76
  rgb: RGBColor;
61
77
  };
62
78
  //#endregion
63
- export { Alpha, ColorProperty, ColorSpace, ColorState, HSLAColor, HSLColor, RGBAColor, RGBColor };
79
+ export { Alpha, ColorProperty, ColorSpace, ColorState, HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor };
@@ -1,5 +1,4 @@
1
- import { HSLAColor, HSLColor, RGBAColor, RGBColor } from "../models.mjs";
2
-
1
+ import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
3
2
  //#region src/color/space/hex.d.ts
4
3
  /**
5
4
  * Get the normalized hex color from a value
@@ -11,12 +10,46 @@ import { HSLAColor, HSLColor, RGBAColor, RGBColor } from "../models.mjs";
11
10
  * @returns Normalized hex color
12
11
  */
13
12
  declare function getNormalizedHex(value: unknown, alpha?: boolean): string;
13
+ /**
14
+ * Convert a hex color string to a _HSL_ color
15
+ *
16
+ * _If the value is unable to be converted, a black _HSL_ color will be returned_
17
+ *
18
+ * @param value Hex color string
19
+ * @returns _HSL_ color
20
+ */
14
21
  declare function hexToHsl(value: string): HSLColor;
22
+ /**
23
+ * Convert a hex color string to a _HSLA_ color
24
+ *
25
+ * _If the value is unable to be converted, a black _HSLA_ color will be returned_
26
+ *
27
+ * @param value Hex color string
28
+ * @returns _HSLA_ color
29
+ */
15
30
  declare function hexToHsla(value: string): HSLAColor;
31
+ /**
32
+ * Convert a hex color string to a _HWB_ color
33
+ *
34
+ * _If the value is unable to be converted, a black _HWB_ color will be returned_
35
+ *
36
+ * @param value Hex color string
37
+ * @returns _HWB_ color
38
+ */
39
+ declare function hexToHwb(value: string): HWBColor;
40
+ /**
41
+ * Convert a hex color string to a _HWBA_ color
42
+ *
43
+ * _If the value is unable to be converted, a black _HWBA_ color will be returned_
44
+ *
45
+ * @param value Hex color string
46
+ * @returns _HWBA_ color
47
+ */
48
+ declare function hexToHwba(value: string): HWBAColor;
16
49
  /**
17
50
  * Convert a hex color to an _RGB_ color
18
51
  *
19
- * _If the value is unable to be converted, a black RGB color will be returned_
52
+ * _If the value is unable to be converted, a black _RGB_ color will be returned_
20
53
  *
21
54
  * @param value Original value
22
55
  * @returns _RGB_ color
@@ -25,11 +58,11 @@ declare function hexToRgb(value: string): RGBColor;
25
58
  /**
26
59
  * Convert a hex color to an _RGBA_ color
27
60
  *
28
- * _If the value is unable to be converted, a black RGBA color with an alpha channel (opacity) of `0` will be returned_
61
+ * _If the value is unable to be converted, a black _RGBA_ color will be returned_
29
62
  *
30
63
  * @param value Original value
31
64
  * @returns _RGBA_ color
32
65
  */
33
66
  declare function hexToRgba(value: string): RGBAColor;
34
67
  //#endregion
35
- export { getNormalizedHex, hexToHsl, hexToHsla, hexToRgb, hexToRgba };
68
+ export { getNormalizedHex, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba };
@@ -1,7 +1,8 @@
1
1
  import { join } from "../../internal/string.mjs";
2
2
  import { ALPHA_FULL_HEX_LONG, EXPRESSION_HEX_LONG, EXPRESSION_PREFIX, HEX_BLACK } from "../constants.mjs";
3
+ import { getHexValue, getPercentage } from "../misc/get.mjs";
3
4
  import { isHexColor } from "../misc/is.mjs";
4
- import { convertRgbToHsla } from "./rgb.mjs";
5
+ import { convertRgbToHsla, convertRgbToHwba } from "./rgb.mjs";
5
6
  //#region src/color/space/hex.ts
6
7
  function convertHexToRgba(value) {
7
8
  const normalized = getNormalizedHex(value, true);
@@ -10,10 +11,10 @@ function convertHexToRgba(value) {
10
11
  const { length } = pairs;
11
12
  for (let index = 1; index < length; index += 1) values.push(Number.parseInt(pairs[index], 16));
12
13
  return {
13
- alpha: values[3] / 255,
14
- blue: values[2],
15
- green: values[1],
16
- red: values[0]
14
+ alpha: getPercentage(values[3] / 255 * 100),
15
+ blue: getHexValue(values[2]),
16
+ green: getHexValue(values[1]),
17
+ red: getHexValue(values[0])
17
18
  };
18
19
  }
19
20
  /**
@@ -29,9 +30,21 @@ function getNormalizedHex(value, alpha) {
29
30
  const includeAlpha = alpha ?? false;
30
31
  if (!isHexColor(value)) return `${HEX_BLACK}${includeAlpha ? ALPHA_FULL_HEX_LONG : ""}`;
31
32
  const normalized = value.replace(EXPRESSION_PREFIX, "");
32
- if (normalized.length < 6) return join(`${normalized.slice(0, 3)}${includeAlpha ? normalized[3] ?? "f" : ""}`.split("").map((character) => character.repeat(2)));
33
+ if (normalized.length < 6) {
34
+ const hex = normalized.slice(0, 3);
35
+ const a = includeAlpha ? normalized[3] ?? "f" : "";
36
+ return join(`${hex}${a}`.split("").map((character) => character.repeat(2)));
37
+ }
33
38
  return `${normalized.slice(0, 6)}${includeAlpha ? normalized.slice(6) || ALPHA_FULL_HEX_LONG : ""}`;
34
39
  }
40
+ /**
41
+ * Convert a hex color string to a _HSL_ color
42
+ *
43
+ * _If the value is unable to be converted, a black _HSL_ color will be returned_
44
+ *
45
+ * @param value Hex color string
46
+ * @returns _HSL_ color
47
+ */
35
48
  function hexToHsl(value) {
36
49
  const { hue, lightness, saturation } = hexToHsla(value);
37
50
  return {
@@ -40,13 +53,48 @@ function hexToHsl(value) {
40
53
  saturation
41
54
  };
42
55
  }
56
+ /**
57
+ * Convert a hex color string to a _HSLA_ color
58
+ *
59
+ * _If the value is unable to be converted, a black _HSLA_ color will be returned_
60
+ *
61
+ * @param value Hex color string
62
+ * @returns _HSLA_ color
63
+ */
43
64
  function hexToHsla(value) {
44
65
  return convertRgbToHsla(convertHexToRgba(value));
45
66
  }
46
67
  /**
68
+ * Convert a hex color string to a _HWB_ color
69
+ *
70
+ * _If the value is unable to be converted, a black _HWB_ color will be returned_
71
+ *
72
+ * @param value Hex color string
73
+ * @returns _HWB_ color
74
+ */
75
+ function hexToHwb(value) {
76
+ const { blackness, hue, whiteness } = hexToHwba(value);
77
+ return {
78
+ hue,
79
+ whiteness,
80
+ blackness
81
+ };
82
+ }
83
+ /**
84
+ * Convert a hex color string to a _HWBA_ color
85
+ *
86
+ * _If the value is unable to be converted, a black _HWBA_ color will be returned_
87
+ *
88
+ * @param value Hex color string
89
+ * @returns _HWBA_ color
90
+ */
91
+ function hexToHwba(value) {
92
+ return convertRgbToHwba(convertHexToRgba(value));
93
+ }
94
+ /**
47
95
  * Convert a hex color to an _RGB_ color
48
96
  *
49
- * _If the value is unable to be converted, a black RGB color will be returned_
97
+ * _If the value is unable to be converted, a black _RGB_ color will be returned_
50
98
  *
51
99
  * @param value Original value
52
100
  * @returns _RGB_ color
@@ -62,7 +110,7 @@ function hexToRgb(value) {
62
110
  /**
63
111
  * Convert a hex color to an _RGBA_ color
64
112
  *
65
- * _If the value is unable to be converted, a black RGBA color with an alpha channel (opacity) of `0` will be returned_
113
+ * _If the value is unable to be converted, a black _RGBA_ color will be returned_
66
114
  *
67
115
  * @param value Original value
68
116
  * @returns _RGBA_ color
@@ -71,4 +119,4 @@ function hexToRgba(value) {
71
119
  return convertHexToRgba(value);
72
120
  }
73
121
  //#endregion
74
- export { getNormalizedHex, hexToHsl, hexToHsla, hexToRgb, hexToRgba };
122
+ export { getNormalizedHex, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba };
@@ -1,29 +1,55 @@
1
- import { HSLAColor, HSLColor, RGBAColor, RGBColor } from "../models.mjs";
2
-
1
+ import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
3
2
  //#region src/color/space/hsl.d.ts
4
3
  declare function getHslValue(value: Record<keyof HSLColor, unknown>): HSLColor;
4
+ /**
5
+ * Convert an _HSL(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
6
+ *
7
+ * _If the value is unable to be converted, a black hex color will be returned_
8
+ *
9
+ * @param hsl _HSL(A)_ color
10
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
11
+ * @returns Hex color string
12
+ */
5
13
  declare function hslToHex(hsl: HSLAColor | HSLColor, alpha?: boolean): string;
14
+ /**
15
+ * Convert an _HSL(A)_ color to an _HWB_ color
16
+ *
17
+ * _If the value is unable to be converted, a black _HWB_ color will be returned_
18
+ *
19
+ * @param hsl HSL(A) color
20
+ * @returns _HWB_ color
21
+ */
22
+ declare function hslToHwb(hsl: HSLAColor | HSLColor): HWBColor;
23
+ /**
24
+ * Convert an _HSL(A)_ color to an _HWBA_ color
25
+ *
26
+ * _If the value is unable to be converted, a black _HWBA_ color will be returned_
27
+ *
28
+ * @param hsl HSL(A) color
29
+ * @returns _HWBA_ color
30
+ */
31
+ declare function hslToHwba(hsl: HSLAColor | HSLColor): HWBAColor;
6
32
  /**
7
33
  * Convert an _HSL(A)_ color to an _RGB_ color
8
34
  *
9
- * _If the value is unable to be converted, a black RGB color will be returned_
35
+ * _If the value is unable to be converted, a black _RGB_ color will be returned_
10
36
  *
11
37
  * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
12
38
  *
13
39
  * @param hsl HSL(A) color
14
- * @returns RGB color
40
+ * @returns _RGB_ color
15
41
  */
16
42
  declare function hslToRgb(hsl: HSLAColor | HSLColor): RGBColor;
17
43
  /**
18
44
  * Convert an _HSL(A)_ color to an _RGBA_ color
19
45
  *
20
- * _If the value is unable to be converted, a black RGBA color with an alpha channel (opactiy) of `0` will be returned_
46
+ * _If the value is unable to be converted, a black _RGBA_ color will be returned_
21
47
  *
22
48
  * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
23
49
  *
24
50
  * @param hsl HSL(A) color
25
- * @returns RGBA color
51
+ * @returns _RGBA_ color
26
52
  */
27
53
  declare function hslToRgba(hsl: HSLAColor | HSLColor): RGBAColor;
28
54
  //#endregion
29
- export { getHslValue, hslToHex, hslToRgb, hslToRgba };
55
+ export { getHslValue, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba };
@@ -1,19 +1,34 @@
1
1
  import { DEFAULT_HSL } from "../constants.mjs";
2
2
  import { getAlphaValue } from "../misc/alpha.mjs";
3
- import { isHslLike } from "../misc/is.mjs";
4
3
  import { getDegrees, getHexValue, getPercentage } from "../misc/get.mjs";
4
+ import { isHslLike } from "../misc/is.mjs";
5
5
  import { convertRgbToHex } from "./rgb.mjs";
6
6
  //#region src/color/space/hsl.ts
7
- function convertHslToRgba(value) {
8
- const hsl = isHslLike(value) ? getHslValue(value) : { ...DEFAULT_HSL };
7
+ function convertHslToHwba(input) {
8
+ let { hue, lightness, saturation } = isHslLike(input) ? getHslValue(input) : { ...DEFAULT_HSL };
9
+ lightness /= 100;
10
+ saturation /= 100;
11
+ const value = lightness + saturation * Math.min(lightness, 1 - lightness);
12
+ if (value === 0) saturation = 0;
13
+ else saturation = 2 * (1 - lightness / value);
14
+ const blackness = 1 - value;
15
+ return {
16
+ hue,
17
+ whiteness: (1 - saturation) * value * 100,
18
+ blackness: blackness * 100,
19
+ alpha: getAlphaValue(input?.alpha ?? 100)
20
+ };
21
+ }
22
+ function convertHslToRgba(input) {
23
+ const hsl = isHslLike(input) ? getHslValue(input) : { ...DEFAULT_HSL };
9
24
  const hue = hsl.hue % 360;
10
25
  const saturation = hsl.saturation / 100;
11
26
  const lightness = hsl.lightness / 100;
12
27
  return {
13
- alpha: getAlphaValue(value.alpha ?? 1),
14
- blue: getHexValue(Math.round(getHexyValue(hue, lightness, saturation, 4))),
15
- green: getHexValue(Math.round(getHexyValue(hue, lightness, saturation, 8))),
16
- red: getHexValue(Math.round(getHexyValue(hue, lightness, saturation, 0)))
28
+ alpha: getAlphaValue(input?.alpha ?? 100),
29
+ blue: getHexValue(getHexyValue(hue, lightness, saturation, 4)),
30
+ green: getHexValue(getHexyValue(hue, lightness, saturation, 8)),
31
+ red: getHexValue(getHexyValue(hue, lightness, saturation, 0))
17
32
  };
18
33
  }
19
34
  function getHexyValue(hue, lightness, saturation, value) {
@@ -23,22 +38,58 @@ function getHexyValue(hue, lightness, saturation, value) {
23
38
  function getHslValue(value) {
24
39
  return {
25
40
  hue: getDegrees(value.hue),
26
- lightness: getPercentage(value.lightness),
27
- saturation: getPercentage(value.saturation)
41
+ saturation: getPercentage(value.saturation),
42
+ lightness: getPercentage(value.lightness)
28
43
  };
29
44
  }
45
+ /**
46
+ * Convert an _HSL(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
47
+ *
48
+ * _If the value is unable to be converted, a black hex color will be returned_
49
+ *
50
+ * @param hsl _HSL(A)_ color
51
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
52
+ * @returns Hex color string
53
+ */
30
54
  function hslToHex(hsl, alpha) {
31
55
  return convertRgbToHex(convertHslToRgba(hsl), alpha ?? false);
32
56
  }
33
57
  /**
58
+ * Convert an _HSL(A)_ color to an _HWB_ color
59
+ *
60
+ * _If the value is unable to be converted, a black _HWB_ color will be returned_
61
+ *
62
+ * @param hsl HSL(A) color
63
+ * @returns _HWB_ color
64
+ */
65
+ function hslToHwb(hsl) {
66
+ const { blackness, hue, whiteness } = convertHslToHwba(hsl);
67
+ return {
68
+ hue,
69
+ whiteness,
70
+ blackness
71
+ };
72
+ }
73
+ /**
74
+ * Convert an _HSL(A)_ color to an _HWBA_ color
75
+ *
76
+ * _If the value is unable to be converted, a black _HWBA_ color will be returned_
77
+ *
78
+ * @param hsl HSL(A) color
79
+ * @returns _HWBA_ color
80
+ */
81
+ function hslToHwba(hsl) {
82
+ return convertHslToHwba(hsl);
83
+ }
84
+ /**
34
85
  * Convert an _HSL(A)_ color to an _RGB_ color
35
86
  *
36
- * _If the value is unable to be converted, a black RGB color will be returned_
87
+ * _If the value is unable to be converted, a black _RGB_ color will be returned_
37
88
  *
38
89
  * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
39
90
  *
40
91
  * @param hsl HSL(A) color
41
- * @returns RGB color
92
+ * @returns _RGB_ color
42
93
  */
43
94
  function hslToRgb(hsl) {
44
95
  const { blue, green, red } = convertHslToRgba(hsl);
@@ -51,15 +102,15 @@ function hslToRgb(hsl) {
51
102
  /**
52
103
  * Convert an _HSL(A)_ color to an _RGBA_ color
53
104
  *
54
- * _If the value is unable to be converted, a black RGBA color with an alpha channel (opactiy) of `0` will be returned_
105
+ * _If the value is unable to be converted, a black _RGBA_ color will be returned_
55
106
  *
56
107
  * _Thanks, https://github.com/color-js/color.js/blob/main/src/spaces/hsl.js#L61_
57
108
  *
58
109
  * @param hsl HSL(A) color
59
- * @returns RGBA color
110
+ * @returns _RGBA_ color
60
111
  */
61
112
  function hslToRgba(hsl) {
62
113
  return convertHslToRgba(hsl);
63
114
  }
64
115
  //#endregion
65
- export { getHslValue, hslToHex, hslToRgb, hslToRgba };
116
+ export { getHslValue, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba };
@@ -0,0 +1,52 @@
1
+ import { HSLAColor, HSLColor, HWBAColor, HWBColor, RGBAColor, RGBColor } from "../models.mjs";
2
+ //#region src/color/space/hwb.d.ts
3
+ declare function convertHwbToHsl(input: HWBAColor | HWBColor): HSLAColor;
4
+ declare function getHwbValue(value: Record<keyof HWBColor, unknown>): HWBColor;
5
+ /**
6
+ * Convert an _HWB(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
7
+ *
8
+ * _If the value is unable to be converted, a black hex color will be returned_
9
+ *
10
+ * @param hwb _HWB(A)_ color
11
+ * @param alpha Include alpha channel _(opacity)_? _(defaults to `false`)_
12
+ * @returns Hex color string
13
+ */
14
+ declare function hwbToHex(hwb: HWBAColor | HWBColor, alpha?: boolean): string;
15
+ /**
16
+ * Convert an _HWB(A)_ color to an _RGB_ color
17
+ *
18
+ * _If the value is unable to be converted, a black _RGB_ color will be returned_
19
+ *
20
+ * @param hwb _HWB(A)_ color
21
+ * @returns _RGB_ color
22
+ */
23
+ declare function hwbToHsl(hwb: HWBAColor | HWBColor): HSLColor;
24
+ /**
25
+ * Convert an _HWB(A)_ color to an _HSLA_ color
26
+ *
27
+ * _If the value is unable to be converted, a black _HSLA_ color will be returned_
28
+ *
29
+ * @param hwb _HWB(A)_ color
30
+ * @returns _HSLA_ color
31
+ */
32
+ declare function hwbToHsla(hwb: HWBAColor | HWBColor): HSLAColor;
33
+ /**
34
+ * Convert an _HWB(A)_ color to an _RGB_ color
35
+ *
36
+ * _If the value is unable to be converted, a black _RGB_ color will be returned_
37
+ *
38
+ * @param hwb _HWB(A)_ color
39
+ * @returns _RGB_ color
40
+ */
41
+ declare function hwbToRgb(hwb: HWBAColor | HWBColor): RGBColor;
42
+ /**
43
+ * Convert an _HWB(A)_ color to an _RGBA_ color
44
+ *
45
+ * _If the value is unable to be converted, a black _RGBA_ color will be returned_
46
+ *
47
+ * @param hwb _HWB(A)_ color
48
+ * @returns _RGBA_ color
49
+ */
50
+ declare function hwbToRgba(hwb: HWBAColor | HWBColor): RGBAColor;
51
+ //#endregion
52
+ export { convertHwbToHsl, getHwbValue, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba };