@oscarpalmer/atoms 0.95.0 → 0.97.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 (117) hide show
  1. package/dist/array/group-by.cjs +1 -1
  2. package/dist/array/group-by.js +1 -1
  3. package/dist/array/sort.cjs +20 -20
  4. package/dist/array/sort.js +20 -20
  5. package/dist/array/to-map.cjs +1 -1
  6. package/dist/array/to-map.js +1 -1
  7. package/dist/color/constants.cjs +33 -0
  8. package/dist/color/constants.js +22 -0
  9. package/dist/color/get.cjs +57 -0
  10. package/dist/color/get.js +52 -0
  11. package/dist/color/index.cjs +97 -0
  12. package/dist/color/index.js +84 -0
  13. package/dist/color/is.cjs +58 -0
  14. package/dist/color/is.js +51 -0
  15. package/dist/color/models.cjs +2 -0
  16. package/dist/color/models.js +1 -0
  17. package/dist/color/value.cjs +90 -0
  18. package/dist/color/value.js +83 -0
  19. package/dist/function.cjs +2 -2
  20. package/dist/function.js +2 -2
  21. package/dist/i18n.cjs +6 -4
  22. package/dist/i18n.js +6 -4
  23. package/dist/index.cjs +13 -12
  24. package/dist/index.js +3 -2
  25. package/dist/internal/array/callbacks.cjs +1 -4
  26. package/dist/internal/array/callbacks.js +1 -4
  27. package/dist/internal/array/find.cjs +4 -4
  28. package/dist/internal/array/find.js +4 -4
  29. package/dist/internal/string/uri.cjs +20 -0
  30. package/dist/internal/string/uri.js +15 -0
  31. package/dist/is.cjs +14 -1
  32. package/dist/is.js +14 -1
  33. package/dist/query.cjs +27 -26
  34. package/dist/query.js +27 -26
  35. package/dist/value/clone.cjs +10 -0
  36. package/dist/value/clone.js +11 -1
  37. package/dist/value/compare.cjs +1 -1
  38. package/dist/value/compare.js +1 -1
  39. package/dist/value/merge.cjs +23 -6
  40. package/dist/value/merge.js +23 -6
  41. package/package.json +7 -7
  42. package/src/array/group-by.ts +1 -1
  43. package/src/array/index.ts +1 -0
  44. package/src/array/models.ts +19 -9
  45. package/src/array/sort.ts +40 -37
  46. package/src/array/to-map.ts +1 -1
  47. package/src/color/constants.ts +29 -0
  48. package/src/color/get.ts +75 -0
  49. package/src/color/index.ts +121 -0
  50. package/src/color/is.ts +88 -0
  51. package/src/color/models.ts +25 -0
  52. package/src/color/value.ts +136 -0
  53. package/src/function.ts +2 -2
  54. package/src/i18n.ts +6 -8
  55. package/src/index.ts +1 -1
  56. package/src/internal/array/callbacks.ts +1 -5
  57. package/src/internal/array/find.ts +4 -6
  58. package/src/internal/string/uri.ts +15 -0
  59. package/src/is.ts +19 -13
  60. package/src/models.ts +17 -4
  61. package/src/query.ts +37 -29
  62. package/src/value/clone.ts +23 -0
  63. package/src/value/compare.ts +1 -1
  64. package/src/value/merge.ts +59 -16
  65. package/types/array/count.d.cts +6 -0
  66. package/types/array/exists.d.cts +6 -0
  67. package/types/array/filter.d.cts +6 -0
  68. package/types/array/find.d.cts +6 -0
  69. package/types/array/group-by.d.cts +7 -1
  70. package/types/array/index-of.d.cts +6 -0
  71. package/types/array/index.d.cts +26 -10
  72. package/types/array/index.d.ts +1 -0
  73. package/types/array/models.d.cts +17 -4
  74. package/types/array/models.d.ts +14 -4
  75. package/types/array/sort.d.cts +25 -9
  76. package/types/array/sort.d.ts +8 -8
  77. package/types/array/to-map.d.cts +6 -0
  78. package/types/array/to-record.d.cts +7 -1
  79. package/types/array/unique.d.cts +6 -0
  80. package/types/color/constants.d.cts +23 -0
  81. package/types/color/constants.d.ts +9 -0
  82. package/types/color/get.d.cts +27 -0
  83. package/types/color/get.d.ts +9 -0
  84. package/types/{color.d.ts → color/index.d.ts} +3 -26
  85. package/types/color/is.d.cts +53 -0
  86. package/types/color/is.d.ts +18 -0
  87. package/types/color/models.d.cts +20 -0
  88. package/types/color/models.d.ts +16 -0
  89. package/types/color/value.d.cts +32 -0
  90. package/types/color/value.d.ts +19 -0
  91. package/types/function.d.cts +1 -1
  92. package/types/function.d.ts +1 -1
  93. package/types/index.d.cts +60 -30
  94. package/types/index.d.ts +1 -1
  95. package/types/internal/array/callbacks.d.cts +1 -1
  96. package/types/internal/string/uri.d.cts +6 -0
  97. package/types/internal/string/uri.d.ts +2 -0
  98. package/types/internal/value/handle.d.cts +3 -0
  99. package/types/is.d.cts +12 -0
  100. package/types/is.d.ts +3 -0
  101. package/types/math.d.cts +3 -0
  102. package/types/models.d.cts +14 -2
  103. package/types/models.d.ts +14 -2
  104. package/types/query.d.cts +3 -0
  105. package/types/string/index.d.cts +3 -0
  106. package/types/string/template.d.cts +3 -0
  107. package/types/value/get.d.cts +3 -0
  108. package/types/value/index.d.cts +16 -2
  109. package/types/value/merge.d.cts +13 -2
  110. package/types/value/merge.d.ts +8 -3
  111. package/types/value/set.d.cts +3 -0
  112. package/types/value/smush.d.cts +3 -0
  113. package/types/value/unsmush.d.cts +3 -0
  114. package/dist/color.cjs +0 -276
  115. package/dist/color.js +0 -263
  116. package/src/color.ts +0 -432
  117. package/types/{color.d.cts → color/index.d.cts} +16 -16
@@ -21,7 +21,7 @@ function groupValues(array, key, value, arrays) {
21
21
  const { length } = array;
22
22
  for (let index = 0; index < length; index += 1) {
23
23
  const item = array[index];
24
- const keyed = callbacks?.key?.(item, index, array) ?? index;
24
+ const keyed = callbacks?.keyed?.(item, index, array) ?? index;
25
25
  const valued = callbacks?.value?.(item, index, array) ?? item;
26
26
  if (arrays) {
27
27
  const existing = record[keyed];
@@ -17,7 +17,7 @@ function groupValues(array, key, value, arrays) {
17
17
  const { length } = array;
18
18
  for (let index = 0; index < length; index += 1) {
19
19
  const item = array[index];
20
- const keyed = callbacks?.key?.(item, index, array) ?? index;
20
+ const keyed = callbacks?.keyed?.(item, index, array) ?? index;
21
21
  const valued = callbacks?.value?.(item, index, array) ?? item;
22
22
  if (arrays) {
23
23
  const existing = record[keyed];
@@ -13,34 +13,34 @@ function sort(array, first, second) {
13
13
  return array;
14
14
  }
15
15
  const direction = first === true || second === true ? "desc" : "asc";
16
- const unknownKeys = Array.isArray(first) ? first : [first];
17
- let { length } = unknownKeys;
18
- const keys = [];
16
+ const unknownSorters = Array.isArray(first) ? first : [first];
17
+ let { length } = unknownSorters;
18
+ const sorters = [];
19
19
  for (let index = 0; index < length; index += 1) {
20
- const key = unknownKeys[index];
21
- const keyWithCallback = {
20
+ const unknownSorter = unknownSorters[index];
21
+ const sorter = {
22
22
  direction,
23
23
  callback: void 0
24
24
  };
25
- if (is.isKey(key)) {
26
- keyWithCallback.callback = (value) => value[key];
27
- } else if (typeof key === "function") {
28
- keyWithCallback.callback = key;
29
- } else if (typeof key?.value === "function" || is.isKey(key?.value)) {
30
- keyWithCallback.direction = key?.direction ?? direction;
31
- keyWithCallback.callback = typeof key.value === "function" ? key.value : (value) => value[key.value];
25
+ if (is.isKey(unknownSorter)) {
26
+ sorter.callback = (value) => value[unknownSorter];
27
+ } else if (typeof unknownSorter === "function") {
28
+ sorter.callback = unknownSorter;
29
+ } else if (typeof unknownSorter?.value === "function" || is.isKey(unknownSorter?.value)) {
30
+ sorter.direction = unknownSorter?.direction ?? direction;
31
+ sorter.callback = typeof unknownSorter.value === "function" ? unknownSorter.value : (value) => value[unknownSorter.value];
32
32
  }
33
- if (typeof keyWithCallback.callback === "function") {
34
- const existing = keys.findIndex(
35
- (existing2) => existing2.callback.toString() === keyWithCallback.callback.toString()
33
+ if (typeof sorter.callback === "function") {
34
+ const existing = sorters.findIndex(
35
+ (existing2) => existing2.callback.toString() === sorter.callback.toString()
36
36
  );
37
37
  if (existing > -1) {
38
- keys.splice(existing, 1);
38
+ sorters.splice(existing, 1);
39
39
  }
40
- keys.push(keyWithCallback);
40
+ sorters.push(sorter);
41
41
  }
42
42
  }
43
- length = keys.length;
43
+ length = sorters.length;
44
44
  if (length === 0) {
45
45
  return array.sort(
46
46
  (first2, second2) => value_compare.compare(first2, second2) * (direction === "asc" ? 1 : -1)
@@ -48,12 +48,12 @@ function sort(array, first, second) {
48
48
  }
49
49
  if (length === 1) {
50
50
  return array.sort(
51
- (first2, second2) => value_compare.compare(keys[0].callback(first2), keys[0].callback(second2)) * (keys[0].direction === "asc" ? 1 : -1)
51
+ (first2, second2) => value_compare.compare(sorters[0].callback(first2), sorters[0].callback(second2)) * (sorters[0].direction === "asc" ? 1 : -1)
52
52
  );
53
53
  }
54
54
  const sorted = array.sort((first2, second2) => {
55
55
  for (let index = 0; index < length; index += 1) {
56
- const { callback, direction: direction2 } = keys[index];
56
+ const { callback, direction: direction2 } = sorters[index];
57
57
  const compared = value_compare.compare(callback(first2), callback(second2)) * (direction2 === "asc" ? 1 : -1);
58
58
  if (compared !== 0) {
59
59
  return compared;
@@ -9,34 +9,34 @@ function sort(array, first, second) {
9
9
  return array;
10
10
  }
11
11
  const direction = first === true || second === true ? "desc" : "asc";
12
- const unknownKeys = Array.isArray(first) ? first : [first];
13
- let { length } = unknownKeys;
14
- const keys = [];
12
+ const unknownSorters = Array.isArray(first) ? first : [first];
13
+ let { length } = unknownSorters;
14
+ const sorters = [];
15
15
  for (let index = 0; index < length; index += 1) {
16
- const key = unknownKeys[index];
17
- const keyWithCallback = {
16
+ const unknownSorter = unknownSorters[index];
17
+ const sorter = {
18
18
  direction,
19
19
  callback: void 0
20
20
  };
21
- if (isKey(key)) {
22
- keyWithCallback.callback = (value) => value[key];
23
- } else if (typeof key === "function") {
24
- keyWithCallback.callback = key;
25
- } else if (typeof key?.value === "function" || isKey(key?.value)) {
26
- keyWithCallback.direction = key?.direction ?? direction;
27
- keyWithCallback.callback = typeof key.value === "function" ? key.value : (value) => value[key.value];
21
+ if (isKey(unknownSorter)) {
22
+ sorter.callback = (value) => value[unknownSorter];
23
+ } else if (typeof unknownSorter === "function") {
24
+ sorter.callback = unknownSorter;
25
+ } else if (typeof unknownSorter?.value === "function" || isKey(unknownSorter?.value)) {
26
+ sorter.direction = unknownSorter?.direction ?? direction;
27
+ sorter.callback = typeof unknownSorter.value === "function" ? unknownSorter.value : (value) => value[unknownSorter.value];
28
28
  }
29
- if (typeof keyWithCallback.callback === "function") {
30
- const existing = keys.findIndex(
31
- (existing2) => existing2.callback.toString() === keyWithCallback.callback.toString()
29
+ if (typeof sorter.callback === "function") {
30
+ const existing = sorters.findIndex(
31
+ (existing2) => existing2.callback.toString() === sorter.callback.toString()
32
32
  );
33
33
  if (existing > -1) {
34
- keys.splice(existing, 1);
34
+ sorters.splice(existing, 1);
35
35
  }
36
- keys.push(keyWithCallback);
36
+ sorters.push(sorter);
37
37
  }
38
38
  }
39
- length = keys.length;
39
+ length = sorters.length;
40
40
  if (length === 0) {
41
41
  return array.sort(
42
42
  (first2, second2) => compare(first2, second2) * (direction === "asc" ? 1 : -1)
@@ -44,12 +44,12 @@ function sort(array, first, second) {
44
44
  }
45
45
  if (length === 1) {
46
46
  return array.sort(
47
- (first2, second2) => compare(keys[0].callback(first2), keys[0].callback(second2)) * (keys[0].direction === "asc" ? 1 : -1)
47
+ (first2, second2) => compare(sorters[0].callback(first2), sorters[0].callback(second2)) * (sorters[0].direction === "asc" ? 1 : -1)
48
48
  );
49
49
  }
50
50
  const sorted = array.sort((first2, second2) => {
51
51
  for (let index = 0; index < length; index += 1) {
52
- const { callback, direction: direction2 } = keys[index];
52
+ const { callback, direction: direction2 } = sorters[index];
53
53
  const compared = compare(callback(first2), callback(second2)) * (direction2 === "asc" ? 1 : -1);
54
54
  if (compared !== 0) {
55
55
  return compared;
@@ -14,7 +14,7 @@ function toMap(array, keyOrArrays, valueOrArrays, arrays) {
14
14
  const { length } = array;
15
15
  for (let index = 0; index < length; index += 1) {
16
16
  const item = array[index];
17
- const key = callbacks?.key?.(item, index, array) ?? index;
17
+ const key = callbacks?.keyed?.(item, index, array) ?? index;
18
18
  const value = callbacks?.value?.(item, index, array) ?? item;
19
19
  if (asArrays) {
20
20
  const existing = map.get(key);
@@ -10,7 +10,7 @@ function toMap(array, keyOrArrays, valueOrArrays, arrays) {
10
10
  const { length } = array;
11
11
  for (let index = 0; index < length; index += 1) {
12
12
  const item = array[index];
13
- const key = callbacks?.key?.(item, index, array) ?? index;
13
+ const key = callbacks?.keyed?.(item, index, array) ?? index;
14
14
  const value = callbacks?.value?.(item, index, array) ?? item;
15
15
  if (asArrays) {
16
16
  const existing = map.get(key);
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const anyPattern = /^#?([a-f0-9]{3}){1,2}$/i;
6
+ const defaultHex = "000000";
7
+ const defaultHsl = {
8
+ hue: 0,
9
+ lightness: 0,
10
+ saturation: 0
11
+ };
12
+ const defaultRgb = {
13
+ blue: 0,
14
+ green: 0,
15
+ red: 0
16
+ };
17
+ const groupedPattern = /^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
18
+ const hslKeys = /* @__PURE__ */ new Set([
19
+ "hue",
20
+ "lightness",
21
+ "saturation"
22
+ ]);
23
+ const prefixPattern = /^#/;
24
+ const rgbKeys = /* @__PURE__ */ new Set(["blue", "green", "red"]);
25
+
26
+ exports.anyPattern = anyPattern;
27
+ exports.defaultHex = defaultHex;
28
+ exports.defaultHsl = defaultHsl;
29
+ exports.defaultRgb = defaultRgb;
30
+ exports.groupedPattern = groupedPattern;
31
+ exports.hslKeys = hslKeys;
32
+ exports.prefixPattern = prefixPattern;
33
+ exports.rgbKeys = rgbKeys;
@@ -0,0 +1,22 @@
1
+ const anyPattern = /^#?([a-f0-9]{3}){1,2}$/i;
2
+ const defaultHex = "000000";
3
+ const defaultHsl = {
4
+ hue: 0,
5
+ lightness: 0,
6
+ saturation: 0
7
+ };
8
+ const defaultRgb = {
9
+ blue: 0,
10
+ green: 0,
11
+ red: 0
12
+ };
13
+ const groupedPattern = /^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i;
14
+ const hslKeys = /* @__PURE__ */ new Set([
15
+ "hue",
16
+ "lightness",
17
+ "saturation"
18
+ ]);
19
+ const prefixPattern = /^#/;
20
+ const rgbKeys = /* @__PURE__ */ new Set(["blue", "green", "red"]);
21
+
22
+ export { anyPattern, defaultHex, defaultHsl, defaultRgb, groupedPattern, hslKeys, prefixPattern, rgbKeys };
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const number = require('../number.cjs');
6
+ const color_constants = require('./constants.cjs');
7
+ const color_is = require('./is.cjs');
8
+ const color_value = require('./value.cjs');
9
+
10
+ function getNormalisedHex(value) {
11
+ if (!color_is.isHexColor(value)) {
12
+ return String(color_constants.defaultHex);
13
+ }
14
+ const normalized = value.replace(color_constants.prefixPattern, "");
15
+ return normalized.length === 3 ? normalized.split("").map((character) => character.repeat(2)).join("") : normalized;
16
+ }
17
+ function getState(value) {
18
+ if (typeof value === "string") {
19
+ const hex = getNormalisedHex(value);
20
+ const rgb = color_value.hexToRgb(hex);
21
+ return {
22
+ hex,
23
+ rgb,
24
+ hsl: color_value.rgbToHsl(rgb)
25
+ };
26
+ }
27
+ const state = {};
28
+ if (typeof value === "object" && value !== null) {
29
+ if (color_is.isHSLColor(value)) {
30
+ state.hsl = {
31
+ hue: number.clamp(value.hue, 0, 360),
32
+ lightness: number.clamp(value.lightness, 0, 100),
33
+ saturation: number.clamp(value.saturation, 0, 100)
34
+ };
35
+ state.rgb = color_value.hslToRgb(state.hsl);
36
+ state.hex = color_value.rgbToHex(state.rgb);
37
+ return state;
38
+ }
39
+ if (color_is.isRGBColor(value)) {
40
+ state.rgb = {
41
+ blue: number.clamp(value.blue, 0, 255),
42
+ green: number.clamp(value.green, 0, 255),
43
+ red: number.clamp(value.red, 0, 255)
44
+ };
45
+ state.hex = color_value.rgbToHex(state.rgb);
46
+ state.hsl = color_value.rgbToHsl(state.rgb);
47
+ return state;
48
+ }
49
+ }
50
+ state.hex = String(color_constants.defaultHex);
51
+ state.hsl = { ...color_constants.defaultHsl };
52
+ state.rgb = { ...color_constants.defaultRgb };
53
+ return state;
54
+ }
55
+
56
+ exports.getNormalisedHex = getNormalisedHex;
57
+ exports.getState = getState;
@@ -0,0 +1,52 @@
1
+ import { clamp } from '../number.js';
2
+ import { defaultHex, prefixPattern, defaultHsl, defaultRgb } from './constants.js';
3
+ import { isHexColor, isHSLColor, isRGBColor } from './is.js';
4
+ import { hexToRgb, rgbToHsl, hslToRgb, rgbToHex } from './value.js';
5
+
6
+ function getNormalisedHex(value) {
7
+ if (!isHexColor(value)) {
8
+ return String(defaultHex);
9
+ }
10
+ const normalized = value.replace(prefixPattern, "");
11
+ return normalized.length === 3 ? normalized.split("").map((character) => character.repeat(2)).join("") : normalized;
12
+ }
13
+ function getState(value) {
14
+ if (typeof value === "string") {
15
+ const hex = getNormalisedHex(value);
16
+ const rgb = hexToRgb(hex);
17
+ return {
18
+ hex,
19
+ rgb,
20
+ hsl: rgbToHsl(rgb)
21
+ };
22
+ }
23
+ const state = {};
24
+ if (typeof value === "object" && value !== null) {
25
+ if (isHSLColor(value)) {
26
+ state.hsl = {
27
+ hue: clamp(value.hue, 0, 360),
28
+ lightness: clamp(value.lightness, 0, 100),
29
+ saturation: clamp(value.saturation, 0, 100)
30
+ };
31
+ state.rgb = hslToRgb(state.hsl);
32
+ state.hex = rgbToHex(state.rgb);
33
+ return state;
34
+ }
35
+ if (isRGBColor(value)) {
36
+ state.rgb = {
37
+ blue: clamp(value.blue, 0, 255),
38
+ green: clamp(value.green, 0, 255),
39
+ red: clamp(value.red, 0, 255)
40
+ };
41
+ state.hex = rgbToHex(state.rgb);
42
+ state.hsl = rgbToHsl(state.rgb);
43
+ return state;
44
+ }
45
+ }
46
+ state.hex = String(defaultHex);
47
+ state.hsl = { ...defaultHsl };
48
+ state.rgb = { ...defaultRgb };
49
+ return state;
50
+ }
51
+
52
+ export { getNormalisedHex, getState };
@@ -0,0 +1,97 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const color_get = require('./get.cjs');
6
+ const color_is = require('./is.cjs');
7
+ const color_value = require('./value.cjs');
8
+
9
+ class Color {
10
+ $color = true;
11
+ state;
12
+ /**
13
+ * Get the color as a hex-color
14
+ */
15
+ get hex() {
16
+ return this.state.hex;
17
+ }
18
+ /**
19
+ * Set colors from a hex-color
20
+ */
21
+ set hex(value) {
22
+ if (color_is.isHexColor(value) && value !== this.state.hex) {
23
+ const hex = color_get.getNormalisedHex(value);
24
+ const rgb = color_value.hexToRgb(hex);
25
+ this.state.hex = hex;
26
+ this.state.hsl = color_value.rgbToHsl(rgb);
27
+ this.state.rgb = rgb;
28
+ }
29
+ }
30
+ /**
31
+ * Get the color as an HSL-color
32
+ */
33
+ get hsl() {
34
+ return this.state.hsl;
35
+ }
36
+ set hsl(value) {
37
+ if (color_is.isHSLColor(value)) {
38
+ const rgb = color_value.hslToRgb(value);
39
+ this.state.hex = color_value.rgbToHex(rgb);
40
+ this.state.hsl = value;
41
+ this.state.rgb = rgb;
42
+ }
43
+ }
44
+ /**
45
+ * Get the color as an RGB-color
46
+ */
47
+ get rgb() {
48
+ return this.state.rgb;
49
+ }
50
+ set rgb(value) {
51
+ if (color_is.isRGBColor(value)) {
52
+ this.state.hex = color_value.rgbToHex(value);
53
+ this.state.hsl = color_value.rgbToHsl(value);
54
+ this.state.rgb = value;
55
+ }
56
+ }
57
+ constructor(value) {
58
+ this.state = color_get.getState(value);
59
+ }
60
+ }
61
+ function getColor(value) {
62
+ return color_is.isColor(value) ? value : new Color(value);
63
+ }
64
+ function getForegroundColor(value) {
65
+ const original = getColor(value);
66
+ const { blue, green, red } = original.rgb;
67
+ const values = [blue / 255, green / 255, red / 255];
68
+ for (let color of values) {
69
+ if (color <= 0.03928) {
70
+ color /= 12.92;
71
+ } else {
72
+ color = ((color + 0.055) / 1.055) ** 2.4;
73
+ }
74
+ }
75
+ const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
76
+ return getColor(luminance > 0.625 ? "000000" : "ffffff");
77
+ }
78
+ function getHexColor(value) {
79
+ return getColor(value).hex;
80
+ }
81
+ function getHSLColor(value) {
82
+ return getColor(value).hsl;
83
+ }
84
+ function getRGBColor(value) {
85
+ return getColor(value).rgb;
86
+ }
87
+
88
+ exports.isColor = color_is.isColor;
89
+ exports.isHSLColor = color_is.isHSLColor;
90
+ exports.isHexColor = color_is.isHexColor;
91
+ exports.isRGBColor = color_is.isRGBColor;
92
+ exports.Color = Color;
93
+ exports.getColor = getColor;
94
+ exports.getForegroundColor = getForegroundColor;
95
+ exports.getHSLColor = getHSLColor;
96
+ exports.getHexColor = getHexColor;
97
+ exports.getRGBColor = getRGBColor;
@@ -0,0 +1,84 @@
1
+ import { getNormalisedHex, getState } from './get.js';
2
+ import { isHexColor, isHSLColor, isRGBColor, isColor } from './is.js';
3
+ import { hexToRgb, rgbToHsl, hslToRgb, rgbToHex } from './value.js';
4
+
5
+ class Color {
6
+ $color = true;
7
+ state;
8
+ /**
9
+ * Get the color as a hex-color
10
+ */
11
+ get hex() {
12
+ return this.state.hex;
13
+ }
14
+ /**
15
+ * Set colors from a hex-color
16
+ */
17
+ set hex(value) {
18
+ if (isHexColor(value) && value !== this.state.hex) {
19
+ const hex = getNormalisedHex(value);
20
+ const rgb = hexToRgb(hex);
21
+ this.state.hex = hex;
22
+ this.state.hsl = rgbToHsl(rgb);
23
+ this.state.rgb = rgb;
24
+ }
25
+ }
26
+ /**
27
+ * Get the color as an HSL-color
28
+ */
29
+ get hsl() {
30
+ return this.state.hsl;
31
+ }
32
+ set hsl(value) {
33
+ if (isHSLColor(value)) {
34
+ const rgb = hslToRgb(value);
35
+ this.state.hex = rgbToHex(rgb);
36
+ this.state.hsl = value;
37
+ this.state.rgb = rgb;
38
+ }
39
+ }
40
+ /**
41
+ * Get the color as an RGB-color
42
+ */
43
+ get rgb() {
44
+ return this.state.rgb;
45
+ }
46
+ set rgb(value) {
47
+ if (isRGBColor(value)) {
48
+ this.state.hex = rgbToHex(value);
49
+ this.state.hsl = rgbToHsl(value);
50
+ this.state.rgb = value;
51
+ }
52
+ }
53
+ constructor(value) {
54
+ this.state = getState(value);
55
+ }
56
+ }
57
+ function getColor(value) {
58
+ return isColor(value) ? value : new Color(value);
59
+ }
60
+ function getForegroundColor(value) {
61
+ const original = getColor(value);
62
+ const { blue, green, red } = original.rgb;
63
+ const values = [blue / 255, green / 255, red / 255];
64
+ for (let color of values) {
65
+ if (color <= 0.03928) {
66
+ color /= 12.92;
67
+ } else {
68
+ color = ((color + 0.055) / 1.055) ** 2.4;
69
+ }
70
+ }
71
+ const luminance = 0.2126 * values[2] + 0.7152 * values[1] + 0.0722 * values[0];
72
+ return getColor(luminance > 0.625 ? "000000" : "ffffff");
73
+ }
74
+ function getHexColor(value) {
75
+ return getColor(value).hex;
76
+ }
77
+ function getHSLColor(value) {
78
+ return getColor(value).hsl;
79
+ }
80
+ function getRGBColor(value) {
81
+ return getColor(value).rgb;
82
+ }
83
+
84
+ export { Color, getColor, getForegroundColor, getHSLColor, getHexColor, getRGBColor, isColor, isHSLColor, isHexColor, isRGBColor };
@@ -0,0 +1,58 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const number = require('../number.cjs');
6
+ const color_constants = require('./constants.cjs');
7
+
8
+ function isColor(value) {
9
+ return typeof value === "object" && value !== null && "$color" in value && value.$color === true;
10
+ }
11
+ function isDegree(value) {
12
+ return typeof value === "number" && number.between(value, 0, 360);
13
+ }
14
+ function isHexColor(value) {
15
+ return typeof value === "string" && color_constants.anyPattern.test(value);
16
+ }
17
+ function isHexy(value) {
18
+ return typeof value === "number" && number.between(value, 0, 255);
19
+ }
20
+ function isHSLColor(value) {
21
+ return isObject(value, color_constants.hslKeys);
22
+ }
23
+ function isRGBColor(value) {
24
+ return isObject(value, color_constants.rgbKeys);
25
+ }
26
+ function isObject(obj, properties) {
27
+ if (typeof obj !== "object" || obj === null) {
28
+ return false;
29
+ }
30
+ const keys = Object.keys(obj);
31
+ const { length } = keys;
32
+ if (length !== properties.size) {
33
+ return false;
34
+ }
35
+ for (let index = 0; index < length; index += 1) {
36
+ const key = keys[index];
37
+ if (!properties.has(key) || !validators[key](obj[key])) {
38
+ return false;
39
+ }
40
+ }
41
+ return true;
42
+ }
43
+ function isPercentage(value) {
44
+ return typeof value === "number" && number.between(value, 0, 100);
45
+ }
46
+ const validators = {
47
+ blue: isHexy,
48
+ green: isHexy,
49
+ hue: isDegree,
50
+ lightness: isPercentage,
51
+ saturation: isPercentage,
52
+ red: isHexy
53
+ };
54
+
55
+ exports.isColor = isColor;
56
+ exports.isHSLColor = isHSLColor;
57
+ exports.isHexColor = isHexColor;
58
+ exports.isRGBColor = isRGBColor;
@@ -0,0 +1,51 @@
1
+ import { between } from '../number.js';
2
+ import { anyPattern, hslKeys, rgbKeys } from './constants.js';
3
+
4
+ function isColor(value) {
5
+ return typeof value === "object" && value !== null && "$color" in value && value.$color === true;
6
+ }
7
+ function isDegree(value) {
8
+ return typeof value === "number" && between(value, 0, 360);
9
+ }
10
+ function isHexColor(value) {
11
+ return typeof value === "string" && anyPattern.test(value);
12
+ }
13
+ function isHexy(value) {
14
+ return typeof value === "number" && between(value, 0, 255);
15
+ }
16
+ function isHSLColor(value) {
17
+ return isObject(value, hslKeys);
18
+ }
19
+ function isRGBColor(value) {
20
+ return isObject(value, rgbKeys);
21
+ }
22
+ function isObject(obj, properties) {
23
+ if (typeof obj !== "object" || obj === null) {
24
+ return false;
25
+ }
26
+ const keys = Object.keys(obj);
27
+ const { length } = keys;
28
+ if (length !== properties.size) {
29
+ return false;
30
+ }
31
+ for (let index = 0; index < length; index += 1) {
32
+ const key = keys[index];
33
+ if (!properties.has(key) || !validators[key](obj[key])) {
34
+ return false;
35
+ }
36
+ }
37
+ return true;
38
+ }
39
+ function isPercentage(value) {
40
+ return typeof value === "number" && between(value, 0, 100);
41
+ }
42
+ const validators = {
43
+ blue: isHexy,
44
+ green: isHexy,
45
+ hue: isDegree,
46
+ lightness: isPercentage,
47
+ saturation: isPercentage,
48
+ red: isHexy
49
+ };
50
+
51
+ export { isColor, isHSLColor, isHexColor, isRGBColor };
@@ -0,0 +1,2 @@
1
+ 'use strict';
2
+
@@ -0,0 +1 @@
1
+