omni-color 0.1.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 (293) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +1042 -0
  3. package/dist/__test__/color-interop.test.d.ts +1 -0
  4. package/dist/__test__/color-interop.test.js +82 -0
  5. package/dist/__test__/comparison_culori.test.d.ts +1 -0
  6. package/dist/__test__/comparison_culori.test.js +169 -0
  7. package/dist/__test__/interop-chroma.test.d.ts +1 -0
  8. package/dist/__test__/interop-chroma.test.js +676 -0
  9. package/dist/__test__/interop-tinycolor.test.d.ts +1 -0
  10. package/dist/__test__/interop-tinycolor.test.js +499 -0
  11. package/dist/color/__test__/baseColor.test.d.ts +2 -0
  12. package/dist/color/__test__/baseColor.test.d.ts.map +1 -0
  13. package/dist/color/__test__/baseColor.test.js +34 -0
  14. package/dist/color/__test__/baseColor.test.js.map +1 -0
  15. package/dist/color/__test__/blank.d.ts +1 -0
  16. package/dist/color/__test__/blank.js +3 -0
  17. package/dist/color/__test__/color.test.d.ts +1 -0
  18. package/dist/color/__test__/color.test.js +1073 -0
  19. package/dist/color/__test__/colorSpaces.test.d.ts +1 -0
  20. package/dist/color/__test__/colorSpaces.test.js +69 -0
  21. package/dist/color/__test__/combinations.test.d.ts +1 -0
  22. package/dist/color/__test__/combinations.test.js +665 -0
  23. package/dist/color/__test__/conversions.test.d.ts +1 -0
  24. package/dist/color/__test__/conversions.test.js +719 -0
  25. package/dist/color/__test__/deltaE.test.d.ts +1 -0
  26. package/dist/color/__test__/deltaE.test.js +120 -0
  27. package/dist/color/__test__/formats.test.d.ts +1 -0
  28. package/dist/color/__test__/formats.test.js +514 -0
  29. package/dist/color/__test__/gradients.test.d.ts +1 -0
  30. package/dist/color/__test__/gradients.test.js +414 -0
  31. package/dist/color/__test__/harmonies.test.d.ts +1 -0
  32. package/dist/color/__test__/harmonies.test.js +676 -0
  33. package/dist/color/__test__/manipulations.test.d.ts +1 -0
  34. package/dist/color/__test__/manipulations.test.js +264 -0
  35. package/dist/color/__test__/names.test.d.ts +1 -0
  36. package/dist/color/__test__/names.test.js +67 -0
  37. package/dist/color/__test__/parse.test.d.ts +1 -0
  38. package/dist/color/__test__/parse.test.js +256 -0
  39. package/dist/color/__test__/random.test.d.ts +1 -0
  40. package/dist/color/__test__/random.test.js +262 -0
  41. package/dist/color/__test__/readability.test.d.ts +1 -0
  42. package/dist/color/__test__/readability.test.js +2596 -0
  43. package/dist/color/__test__/srgb.test.d.ts +1 -0
  44. package/dist/color/__test__/srgb.test.js +43 -0
  45. package/dist/color/__test__/swatch.test.d.ts +1 -0
  46. package/dist/color/__test__/swatch.test.js +267 -0
  47. package/dist/color/__test__/temperature.test.d.ts +1 -0
  48. package/dist/color/__test__/temperature.test.js +223 -0
  49. package/dist/color/__test__/utils.test.d.ts +1 -0
  50. package/dist/color/__test__/utils.test.js +409 -0
  51. package/dist/color/__test__/validations.test.d.ts +1 -0
  52. package/dist/color/__test__/validations.test.js +239 -0
  53. package/dist/color/color.constants.d.ts +4 -0
  54. package/dist/color/color.constants.d.ts.map +1 -0
  55. package/dist/color/color.constants.js +152 -0
  56. package/dist/color/color.constants.js.map +1 -0
  57. package/dist/color/color.consts.d.ts +4 -0
  58. package/dist/color/color.consts.js +152 -0
  59. package/dist/color/color.d.ts +788 -0
  60. package/dist/color/color.d.ts.map +1 -0
  61. package/dist/color/color.helpers.d.ts +6 -0
  62. package/dist/color/color.helpers.js +4 -0
  63. package/dist/color/color.js +919 -0
  64. package/dist/color/color.js.map +1 -0
  65. package/dist/color/colorSpaces.d.ts +15 -0
  66. package/dist/color/colorSpaces.js +127 -0
  67. package/dist/color/combinations.d.ts +45 -0
  68. package/dist/color/combinations.js +562 -0
  69. package/dist/color/conversions.d.ts +16 -0
  70. package/dist/color/conversions.d.ts.map +1 -0
  71. package/dist/color/conversions.js +705 -0
  72. package/dist/color/conversions.js.map +1 -0
  73. package/dist/color/deltaE.d.ts +59 -0
  74. package/dist/color/deltaE.js +110 -0
  75. package/dist/color/formats.d.ts +128 -0
  76. package/dist/color/formats.d.ts.map +1 -0
  77. package/dist/color/formats.js +153 -0
  78. package/dist/color/formats.js.map +1 -0
  79. package/dist/color/gradients.d.ts +49 -0
  80. package/dist/color/gradients.js +672 -0
  81. package/dist/color/harmonies.d.ts +18 -0
  82. package/dist/color/harmonies.js +128 -0
  83. package/dist/color/manipulations.d.ts +29 -0
  84. package/dist/color/manipulations.js +106 -0
  85. package/dist/color/names.d.ts +38 -0
  86. package/dist/color/names.js +108 -0
  87. package/dist/color/parse.d.ts +2 -0
  88. package/dist/color/parse.js +437 -0
  89. package/dist/color/random.d.ts +30 -0
  90. package/dist/color/random.js +65 -0
  91. package/dist/color/readability.d.ts +53 -0
  92. package/dist/color/readability.js +284 -0
  93. package/dist/color/srgb.d.ts +8 -0
  94. package/dist/color/srgb.js +33 -0
  95. package/dist/color/swatch.d.ts +49 -0
  96. package/dist/color/swatch.js +118 -0
  97. package/dist/color/temperature.d.ts +26 -0
  98. package/dist/color/temperature.js +141 -0
  99. package/dist/color/utils.d.ts +49 -0
  100. package/dist/color/utils.d.ts.map +1 -0
  101. package/dist/color/utils.js +104 -0
  102. package/dist/color/utils.js.map +1 -0
  103. package/dist/color/validations.d.ts +2 -0
  104. package/dist/color/validations.d.ts.map +1 -0
  105. package/dist/color/validations.js +178 -0
  106. package/dist/color/validations.js.map +1 -0
  107. package/dist/demo/src/AppFooter.d.ts +1 -0
  108. package/dist/demo/src/AppFooter.js +4 -0
  109. package/dist/demo/src/AppHeader.d.ts +5 -0
  110. package/dist/demo/src/AppHeader.js +6 -0
  111. package/dist/demo/src/components/Card.d.ts +9 -0
  112. package/dist/demo/src/components/Card.js +9 -0
  113. package/dist/demo/src/components/Chip.d.ts +21 -0
  114. package/dist/demo/src/components/Chip.js +23 -0
  115. package/dist/demo/src/components/ColorBox.d.ts +16 -0
  116. package/dist/demo/src/components/ColorBox.js +45 -0
  117. package/dist/demo/src/components/ColorInfoCard.d.ts +7 -0
  118. package/dist/demo/src/components/ColorInfoCard.js +50 -0
  119. package/dist/demo/src/components/ExpandableCodeSnippet.d.ts +5 -0
  120. package/dist/demo/src/components/ExpandableCodeSnippet.js +17 -0
  121. package/dist/demo/src/components/Icon.d.ts +11 -0
  122. package/dist/demo/src/components/Icon.js +50 -0
  123. package/dist/demo/src/components/Icon.types.d.ts +14 -0
  124. package/dist/demo/src/components/Icon.types.js +15 -0
  125. package/dist/demo/src/components/SectionContainer.d.ts +10 -0
  126. package/dist/demo/src/components/SectionContainer.js +12 -0
  127. package/dist/demo/src/components/VSpace.d.ts +5 -0
  128. package/dist/demo/src/components/VSpace.js +4 -0
  129. package/dist/demo/src/components/inputs/Checkbox.d.ts +7 -0
  130. package/dist/demo/src/components/inputs/Checkbox.js +4 -0
  131. package/dist/demo/src/components/inputs/InputGroup.d.ts +6 -0
  132. package/dist/demo/src/components/inputs/InputGroup.js +4 -0
  133. package/dist/demo/src/components/inputs/NumberInput.d.ts +10 -0
  134. package/dist/demo/src/components/inputs/NumberInput.js +8 -0
  135. package/dist/demo/src/components/inputs/Select.d.ts +12 -0
  136. package/dist/demo/src/components/inputs/Select.js +6 -0
  137. package/dist/demo/src/components/inputs/Slider.d.ts +10 -0
  138. package/dist/demo/src/components/inputs/Slider.js +4 -0
  139. package/dist/demo/src/components/utils.d.ts +17 -0
  140. package/dist/demo/src/components/utils.js +24 -0
  141. package/dist/demo/src/demo/ColorDemo.d.ts +1 -0
  142. package/dist/demo/src/demo/ColorDemo.js +45 -0
  143. package/dist/demo/src/demo/ColorHarmonyDemo.d.ts +6 -0
  144. package/dist/demo/src/demo/ColorHarmonyDemo.js +18 -0
  145. package/dist/demo/src/demo/ColorInfo.d.ts +6 -0
  146. package/dist/demo/src/demo/ColorInfo.js +14 -0
  147. package/dist/demo/src/demo/ColorInput.d.ts +7 -0
  148. package/dist/demo/src/demo/ColorInput.js +58 -0
  149. package/dist/demo/src/demo/ColorManipulationDemo.d.ts +6 -0
  150. package/dist/demo/src/demo/ColorManipulationDemo.js +63 -0
  151. package/dist/demo/src/demo/ColorSwatch.d.ts +13 -0
  152. package/dist/demo/src/demo/ColorSwatch.js +15 -0
  153. package/dist/demo/src/demo/ReadabilityDemo.d.ts +6 -0
  154. package/dist/demo/src/demo/ReadabilityDemo.js +24 -0
  155. package/dist/demo/src/demo/combinations/AverageColorsOptionInputs.d.ts +7 -0
  156. package/dist/demo/src/demo/combinations/AverageColorsOptionInputs.js +7 -0
  157. package/dist/demo/src/demo/combinations/BlendColorsOptionInputs.d.ts +7 -0
  158. package/dist/demo/src/demo/combinations/BlendColorsOptionInputs.js +13 -0
  159. package/dist/demo/src/demo/combinations/ColorCombinationDemo.d.ts +6 -0
  160. package/dist/demo/src/demo/combinations/ColorCombinationDemo.js +78 -0
  161. package/dist/demo/src/demo/combinations/MixColorsOptionInputs.d.ts +7 -0
  162. package/dist/demo/src/demo/combinations/MixColorsOptionInputs.js +10 -0
  163. package/dist/demo/src/demo/combinations/colorCombinationDemo.consts.d.ts +4 -0
  164. package/dist/demo/src/demo/combinations/colorCombinationDemo.consts.js +12 -0
  165. package/dist/demo/src/demo/gradients/GradientOptionInputs.d.ts +10 -0
  166. package/dist/demo/src/demo/gradients/GradientOptionInputs.js +42 -0
  167. package/dist/demo/src/demo/gradients/GradientThroughCard.d.ts +6 -0
  168. package/dist/demo/src/demo/gradients/GradientThroughCard.js +50 -0
  169. package/dist/demo/src/demo/gradients/GradientToCard.d.ts +6 -0
  170. package/dist/demo/src/demo/gradients/GradientToCard.js +54 -0
  171. package/dist/demo/src/demo/gradients/GradientsDemo.d.ts +6 -0
  172. package/dist/demo/src/demo/gradients/GradientsDemo.js +6 -0
  173. package/dist/demo/src/demo/gradients/gradientOptions.consts.d.ts +3 -0
  174. package/dist/demo/src/demo/gradients/gradientOptions.consts.js +16 -0
  175. package/dist/demo/src/demo/palette/ColorPaletteDemo.d.ts +6 -0
  176. package/dist/demo/src/demo/palette/ColorPaletteDemo.js +26 -0
  177. package/dist/demo/src/demo/palette/PaletteGenerationOptions.d.ts +8 -0
  178. package/dist/demo/src/demo/palette/PaletteGenerationOptions.js +53 -0
  179. package/dist/demo/src/demo/palette/PaletteHarmonyOptions.d.ts +7 -0
  180. package/dist/demo/src/demo/palette/PaletteHarmonyOptions.js +5 -0
  181. package/dist/demo/src/main.d.ts +1 -0
  182. package/dist/demo/src/main.js +14 -0
  183. package/dist/demo/src/pages/DemoPage.d.ts +1 -0
  184. package/dist/demo/src/pages/DemoPage.js +28 -0
  185. package/dist/demo/src/pages/PlaygroundPage.d.ts +1 -0
  186. package/dist/demo/src/pages/PlaygroundPage.js +24 -0
  187. package/dist/demo/src/playground/Playground.d.ts +1 -0
  188. package/dist/demo/src/playground/Playground.js +42 -0
  189. package/dist/demo/src/playground/playgroundUtils.d.ts +16 -0
  190. package/dist/demo/src/playground/playgroundUtils.js +202 -0
  191. package/dist/demo/src/seo/PageHead.d.ts +9 -0
  192. package/dist/demo/src/seo/PageHead.js +76 -0
  193. package/dist/demo/src/seo/StructuredData.d.ts +6 -0
  194. package/dist/demo/src/seo/StructuredData.js +13 -0
  195. package/dist/demo/src/toast/ToastProvider.d.ts +2 -0
  196. package/dist/demo/src/toast/ToastProvider.js +62 -0
  197. package/dist/demo/src/toast/index.d.ts +2 -0
  198. package/dist/demo/src/toast/index.js +2 -0
  199. package/dist/demo/src/toast/toastBus.d.ts +13 -0
  200. package/dist/demo/src/toast/toastBus.js +27 -0
  201. package/dist/index.d.ts +1233 -0
  202. package/dist/index.d.ts.map +1 -0
  203. package/dist/index.js +5235 -0
  204. package/dist/index.js.map +1 -0
  205. package/dist/palette/__test__/palette.test.d.ts +1 -0
  206. package/dist/palette/__test__/palette.test.js +397 -0
  207. package/dist/palette/palette.d.ts +41 -0
  208. package/dist/palette/palette.js +126 -0
  209. package/dist/src/__test__/interop-chroma.test.d.ts +1 -0
  210. package/dist/src/__test__/interop-chroma.test.js +673 -0
  211. package/dist/src/__test__/interop-tinycolor.test.d.ts +1 -0
  212. package/dist/src/__test__/interop-tinycolor.test.js +499 -0
  213. package/dist/src/color/__test__/color.test.d.ts +1 -0
  214. package/dist/src/color/__test__/color.test.js +1071 -0
  215. package/dist/src/color/__test__/colorSpaces.test.d.ts +1 -0
  216. package/dist/src/color/__test__/colorSpaces.test.js +69 -0
  217. package/dist/src/color/__test__/combinations.test.d.ts +1 -0
  218. package/dist/src/color/__test__/combinations.test.js +665 -0
  219. package/dist/src/color/__test__/conversions.test.d.ts +1 -0
  220. package/dist/src/color/__test__/conversions.test.js +719 -0
  221. package/dist/src/color/__test__/deltaE.test.d.ts +1 -0
  222. package/dist/src/color/__test__/deltaE.test.js +120 -0
  223. package/dist/src/color/__test__/formats.test.d.ts +1 -0
  224. package/dist/src/color/__test__/formats.test.js +470 -0
  225. package/dist/src/color/__test__/gradients.test.d.ts +1 -0
  226. package/dist/src/color/__test__/gradients.test.js +414 -0
  227. package/dist/src/color/__test__/harmonies.test.d.ts +1 -0
  228. package/dist/src/color/__test__/harmonies.test.js +734 -0
  229. package/dist/src/color/__test__/manipulations.test.d.ts +1 -0
  230. package/dist/src/color/__test__/manipulations.test.js +264 -0
  231. package/dist/src/color/__test__/names.test.d.ts +1 -0
  232. package/dist/src/color/__test__/names.test.js +67 -0
  233. package/dist/src/color/__test__/parse.test.d.ts +1 -0
  234. package/dist/src/color/__test__/parse.test.js +251 -0
  235. package/dist/src/color/__test__/random.test.d.ts +1 -0
  236. package/dist/src/color/__test__/random.test.js +262 -0
  237. package/dist/src/color/__test__/readability.test.d.ts +1 -0
  238. package/dist/src/color/__test__/readability.test.js +2596 -0
  239. package/dist/src/color/__test__/swatch.test.d.ts +1 -0
  240. package/dist/src/color/__test__/swatch.test.js +267 -0
  241. package/dist/src/color/__test__/temperature.test.d.ts +1 -0
  242. package/dist/src/color/__test__/temperature.test.js +223 -0
  243. package/dist/src/color/__test__/utils.test.d.ts +1 -0
  244. package/dist/src/color/__test__/utils.test.js +409 -0
  245. package/dist/src/color/__test__/validations.test.d.ts +1 -0
  246. package/dist/src/color/__test__/validations.test.js +239 -0
  247. package/dist/src/color/color.consts.d.ts +4 -0
  248. package/dist/src/color/color.consts.js +152 -0
  249. package/dist/src/color/color.d.ts +775 -0
  250. package/dist/src/color/color.js +903 -0
  251. package/dist/src/color/colorSpaces.d.ts +15 -0
  252. package/dist/src/color/colorSpaces.js +127 -0
  253. package/dist/src/color/combinations.d.ts +45 -0
  254. package/dist/src/color/combinations.js +557 -0
  255. package/dist/src/color/conversions.d.ts +16 -0
  256. package/dist/src/color/conversions.js +705 -0
  257. package/dist/src/color/deltaE.d.ts +59 -0
  258. package/dist/src/color/deltaE.js +110 -0
  259. package/dist/src/color/formats.d.ts +126 -0
  260. package/dist/src/color/formats.js +150 -0
  261. package/dist/src/color/gradients.d.ts +49 -0
  262. package/dist/src/color/gradients.js +673 -0
  263. package/dist/src/color/harmonies.d.ts +18 -0
  264. package/dist/src/color/harmonies.js +129 -0
  265. package/dist/src/color/manipulations.d.ts +29 -0
  266. package/dist/src/color/manipulations.js +107 -0
  267. package/dist/src/color/names.d.ts +38 -0
  268. package/dist/src/color/names.js +108 -0
  269. package/dist/src/color/parse.d.ts +2 -0
  270. package/dist/src/color/parse.js +438 -0
  271. package/dist/src/color/random.d.ts +30 -0
  272. package/dist/src/color/random.js +65 -0
  273. package/dist/src/color/readability.d.ts +53 -0
  274. package/dist/src/color/readability.js +284 -0
  275. package/dist/src/color/swatch.d.ts +49 -0
  276. package/dist/src/color/swatch.js +117 -0
  277. package/dist/src/color/temperature.d.ts +26 -0
  278. package/dist/src/color/temperature.js +142 -0
  279. package/dist/src/color/utils.d.ts +57 -0
  280. package/dist/src/color/utils.js +142 -0
  281. package/dist/src/color/validations.d.ts +2 -0
  282. package/dist/src/color/validations.js +178 -0
  283. package/dist/src/index.d.ts +20 -0
  284. package/dist/src/index.js +2 -0
  285. package/dist/src/palette/__test__/palette.test.d.ts +1 -0
  286. package/dist/src/palette/__test__/palette.test.js +397 -0
  287. package/dist/src/palette/palette.d.ts +41 -0
  288. package/dist/src/palette/palette.js +127 -0
  289. package/dist/src/utils.d.ts +20 -0
  290. package/dist/src/utils.js +42 -0
  291. package/dist/utils.d.ts +20 -0
  292. package/dist/utils.js +42 -0
  293. package/package.json +96 -0
@@ -0,0 +1,142 @@
1
+ import { clampValue, resolveCaseInsensitiveOption } from '../utils';
2
+ import { Color } from './color';
3
+ import { CSS_COLOR_NAME_TO_HEX_MAP } from './color.consts';
4
+ import { toRGBA } from './conversions';
5
+ import { parseCSSColorFormatString } from './parse';
6
+ import { getRandomColorRGBA } from './random';
7
+ import { getColorFromTemperatureLabel, matchPartialColorTemperatureLabel } from './temperature';
8
+ export function getColorRGBAFromInput(color) {
9
+ if (color instanceof Color) {
10
+ return color.toRGBA();
11
+ }
12
+ if (typeof color === 'string') {
13
+ const colorString = color.trim().toLowerCase();
14
+ // Hex string (e.g. "#ff0000"):
15
+ if (colorString.startsWith('#')) {
16
+ return toRGBA(colorString);
17
+ }
18
+ // Named color (e.g. "red"):
19
+ const namedColorHex = CSS_COLOR_NAME_TO_HEX_MAP[colorString.replace(/ /g, '')];
20
+ if (namedColorHex) {
21
+ return toRGBA(namedColorHex);
22
+ }
23
+ const matchedColorTemperatureLabel = matchPartialColorTemperatureLabel(colorString);
24
+ if (matchedColorTemperatureLabel) {
25
+ return getColorFromTemperatureLabel(matchedColorTemperatureLabel).toRGBA();
26
+ }
27
+ // Other CSS color format string (e.g. "rgb(255, 0, 0)"):
28
+ const parsedColor = parseCSSColorFormatString(colorString);
29
+ if (parsedColor) {
30
+ return parsedColor.toRGBA();
31
+ }
32
+ throw new Error(`unknown color name or format: "${color}"`);
33
+ }
34
+ return color ? toRGBA(color) : getRandomColorRGBA();
35
+ }
36
+ const RGB_CHANNEL_MAX_VALUE = 255; // 8‑bit channel ceiling
37
+ const SRGB_LINEAR_SEGMENT_DIVISOR = 12.92; // slope for low‑intensity segment
38
+ const SRGB_GAMMA_OFFSET = 0.055; // additive term in sRGB gamma curve
39
+ const SRGB_GAMMA_SCALE = 1.055; // multiplicative term in sRGB gamma curve
40
+ const SRGB_GAMMA_EXPONENT = 2.4; // exponent in sRGB transfer function
41
+ const SRGB_GAMMA_PIVOT_OPTIONS = {
42
+ SRGB: 0.04045,
43
+ WCAG: 0.03928,
44
+ };
45
+ // Does a gamma correction by converting the given sRGB channel value (0 to 255)
46
+ // to a linear RGB channel value (0 to 1).
47
+ export function srgbChannelToLinear(srgbChannelVal, pivot) {
48
+ const c = clampValue(srgbChannelVal, 0, RGB_CHANNEL_MAX_VALUE) / RGB_CHANNEL_MAX_VALUE; // normalize
49
+ let result;
50
+ if (c <= SRGB_GAMMA_PIVOT_OPTIONS[pivot]) {
51
+ result = c / SRGB_LINEAR_SEGMENT_DIVISOR; // linear portion for dark colors
52
+ }
53
+ else {
54
+ result = Math.pow((c + SRGB_GAMMA_OFFSET) / SRGB_GAMMA_SCALE, SRGB_GAMMA_EXPONENT); // gamma correction for brighter colors
55
+ }
56
+ return clampValue(result, 0, 1);
57
+ }
58
+ // Inverts `srgbChannelToLinear()`. Takes a linearized channel value (0 to 1) and returns
59
+ // the original sRGB channel value (0 to 255).
60
+ export function linearChannelToSrgb(linearChannelVal, pivot) {
61
+ const c = clampValue(linearChannelVal, 0, 1);
62
+ const threshold = SRGB_GAMMA_PIVOT_OPTIONS[pivot] / SRGB_LINEAR_SEGMENT_DIVISOR;
63
+ let result;
64
+ if (c > threshold) {
65
+ result = SRGB_GAMMA_SCALE * Math.pow(c, 1 / SRGB_GAMMA_EXPONENT) - SRGB_GAMMA_OFFSET;
66
+ }
67
+ else {
68
+ result = c * SRGB_LINEAR_SEGMENT_DIVISOR;
69
+ }
70
+ return clampValue(result * RGB_CHANNEL_MAX_VALUE, 0, RGB_CHANNEL_MAX_VALUE);
71
+ }
72
+ export function getRelativeLuminance(rgb) {
73
+ const r = srgbChannelToLinear(rgb.r, 'WCAG');
74
+ const g = srgbChannelToLinear(rgb.g, 'WCAG');
75
+ const b = srgbChannelToLinear(rgb.b, 'WCAG');
76
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b; // standard luminance formula
77
+ }
78
+ /**
79
+ * The algorithm mode to use for calculating if a color is dark.
80
+ */
81
+ const COLOR_DARKNESS_MODES = ['WCAG', 'YIQ'];
82
+ function isColorDarkWCAG(color, threshold) {
83
+ return getRelativeLuminance(color.toRGBA()) < threshold;
84
+ }
85
+ function isColorDarkYIQ(color, threshold) {
86
+ // Weighted RGB luminance calculation:
87
+ const { r, g, b } = color.toRGB();
88
+ const brightness = (299 * r + 587 * g + 114 * b) / 1000;
89
+ return brightness < threshold;
90
+ }
91
+ // Checks if a color is dark based on the specified algorithm and threshold.
92
+ export function isColorDark(color, options = {}) {
93
+ const colorDarknessMode = resolveCaseInsensitiveOption({
94
+ allowedValues: COLOR_DARKNESS_MODES,
95
+ defaultValue: 'WCAG',
96
+ key: 'colorDarknessMode',
97
+ options,
98
+ });
99
+ if (colorDarknessMode === 'YIQ') {
100
+ return isColorDarkYIQ(color, options.yiqThreshold ?? 128);
101
+ }
102
+ return isColorDarkWCAG(color, options.wcagThreshold ?? 0.179);
103
+ }
104
+ export function isColorOffWhite(color) {
105
+ const { r, g, b } = color.toRGB();
106
+ const brightness = (299 * r + 587 * g + 114 * b) / 1000;
107
+ const maxChannel = Math.max(r, g, b);
108
+ const minChannel = Math.min(r, g, b);
109
+ return brightness >= 240 && maxChannel - minChannel <= 30;
110
+ }
111
+ const RGB_CHANNEL_EPSILON = 1e-2; // allow minor floating‑point drift on RGB channels
112
+ const ALPHA_EPSILON = 1e-6; // alpha values should match unless the difference is imperceptible floating‑point noise
113
+ function normalizeChannel(value) {
114
+ const rounded = Math.round(value);
115
+ return Math.abs(value - rounded) <= RGB_CHANNEL_EPSILON ? rounded : value;
116
+ }
117
+ function normalizeAlpha(value) {
118
+ const rounded = Math.round(value * 1000) / 1000;
119
+ return Math.abs(value - rounded) <= ALPHA_EPSILON ? rounded : value;
120
+ }
121
+ export function areColorsEqual(color1, color2) {
122
+ const c1 = color1.toRGBA();
123
+ const c2 = color2.toRGBA();
124
+ const r1 = normalizeChannel(c1.r);
125
+ const g1 = normalizeChannel(c1.g);
126
+ const b1 = normalizeChannel(c1.b);
127
+ const r2 = normalizeChannel(c2.r);
128
+ const g2 = normalizeChannel(c2.g);
129
+ const b2 = normalizeChannel(c2.b);
130
+ const a1 = normalizeAlpha(c1.a ?? 1);
131
+ const a2 = normalizeAlpha(c2.a ?? 1);
132
+ return r1 === r2 && g1 === g2 && b1 === b2 && Math.abs(a1 - a2) <= ALPHA_EPSILON;
133
+ }
134
+ function isColor(value) {
135
+ return value instanceof Color;
136
+ }
137
+ export function getColorList(candidates) {
138
+ if (Array.isArray(candidates)) {
139
+ return candidates.map((candidate) => (isColor(candidate) ? candidate : new Color(candidate)));
140
+ }
141
+ return Object.values(candidates).filter(isColor);
142
+ }
@@ -0,0 +1,2 @@
1
+ import type { ColorFormat } from './formats';
2
+ export declare function validateColorOrThrow(color?: ColorFormat | null): void;
@@ -0,0 +1,178 @@
1
+ import { getColorFormatType } from './formats';
2
+ const HEX_COLOR_REGEX = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
3
+ function isValidHexColor(color) {
4
+ return HEX_COLOR_REGEX.test(color);
5
+ }
6
+ function isValueInRangeInclusive(value, min, max) {
7
+ return typeof value === 'number' && value >= min && value <= max;
8
+ }
9
+ function isValidAlphaValue(value) {
10
+ return value === undefined || (typeof value === 'number' && value >= 0 && value <= 1);
11
+ }
12
+ function isValidRGBColor(color) {
13
+ const { r, g, b } = color;
14
+ return (isValueInRangeInclusive(r, 0, 255) &&
15
+ isValueInRangeInclusive(g, 0, 255) &&
16
+ isValueInRangeInclusive(b, 0, 255));
17
+ }
18
+ function isValidRGBAColor(color) {
19
+ return isValidRGBColor(color) && isValidAlphaValue(color.a);
20
+ }
21
+ function isValidHSLColor(color) {
22
+ const { h, s, l } = color;
23
+ return (isValueInRangeInclusive(h, 0, 360) &&
24
+ isValueInRangeInclusive(s, 0, 100) &&
25
+ isValueInRangeInclusive(l, 0, 100));
26
+ }
27
+ function isValidHSLAColor(color) {
28
+ return isValidHSLColor(color) && isValidAlphaValue(color.a);
29
+ }
30
+ function isValidHSVColor(color) {
31
+ const { h, s, v } = color;
32
+ return (isValueInRangeInclusive(h, 0, 360) &&
33
+ isValueInRangeInclusive(s, 0, 100) &&
34
+ isValueInRangeInclusive(v, 0, 100));
35
+ }
36
+ function isValidHSVAColor(color) {
37
+ return isValidHSVColor(color) && isValidAlphaValue(color.a);
38
+ }
39
+ function isValidHWBColor(color) {
40
+ const { h, w, b } = color;
41
+ return (isValueInRangeInclusive(h, 0, 360) &&
42
+ isValueInRangeInclusive(w, 0, 100) &&
43
+ isValueInRangeInclusive(b, 0, 100));
44
+ }
45
+ function isValidHWBAColor(color) {
46
+ return isValidHWBColor(color) && isValidAlphaValue(color.a);
47
+ }
48
+ function isValidCMYKColor(color) {
49
+ const { c, m, y, k } = color;
50
+ const check = (value) => typeof value === 'number' && value >= 0 && value <= 100;
51
+ return [c, m, y, k].every(check);
52
+ }
53
+ function isValidLABColor(color) {
54
+ const { l, a, b } = color;
55
+ return (typeof l === 'number' &&
56
+ l >= 0 &&
57
+ l <= 100 &&
58
+ typeof a === 'number' &&
59
+ Number.isFinite(a) &&
60
+ typeof b === 'number' &&
61
+ Number.isFinite(b));
62
+ }
63
+ function isValidOKLABColor(color) {
64
+ const { l, a, b } = color;
65
+ return (typeof l === 'number' &&
66
+ l >= 0 &&
67
+ l <= 1 &&
68
+ typeof a === 'number' &&
69
+ Number.isFinite(a) &&
70
+ typeof b === 'number' &&
71
+ Number.isFinite(b));
72
+ }
73
+ function isValidLCHColor(color) {
74
+ const { l, c, h } = color;
75
+ return (typeof l === 'number' &&
76
+ l >= 0 &&
77
+ l <= 100 &&
78
+ typeof c === 'number' &&
79
+ c >= 0 &&
80
+ typeof h === 'number' &&
81
+ h >= 0 &&
82
+ h <= 360);
83
+ }
84
+ function isValidOKLCHColor(color) {
85
+ const { l, c, h } = color;
86
+ return (typeof l === 'number' &&
87
+ l >= 0 &&
88
+ l <= 1 &&
89
+ typeof c === 'number' &&
90
+ c >= 0 &&
91
+ typeof h === 'number' &&
92
+ h >= 0 &&
93
+ h <= 360);
94
+ }
95
+ export function validateColorOrThrow(color) {
96
+ if (color === undefined) {
97
+ throw new Error('color is undefined');
98
+ }
99
+ if (color === null) {
100
+ throw new Error('color is null');
101
+ }
102
+ const { formatType, value } = getColorFormatType(color);
103
+ switch (formatType) {
104
+ case 'HEX':
105
+ case 'HEX8':
106
+ if (!isValidHexColor(value)) {
107
+ throw new Error(`invalid hex color: "${value}"`);
108
+ }
109
+ break;
110
+ case 'RGB':
111
+ if (!isValidRGBColor(value)) {
112
+ throw new Error(`invalid RGB color: "${JSON.stringify(value)}"`);
113
+ }
114
+ break;
115
+ case 'RGBA':
116
+ if (!isValidRGBAColor(value)) {
117
+ throw new Error(`invalid RGBA color: "${JSON.stringify(value)}"`);
118
+ }
119
+ break;
120
+ case 'HSL':
121
+ if (!isValidHSLColor(value)) {
122
+ throw new Error(`invalid HSL color: "${JSON.stringify(value)}"`);
123
+ }
124
+ break;
125
+ case 'HSLA':
126
+ if (!isValidHSLAColor(value)) {
127
+ throw new Error(`invalid HSLA color: "${JSON.stringify(value)}"`);
128
+ }
129
+ break;
130
+ case 'HSV':
131
+ if (!isValidHSVColor(value)) {
132
+ throw new Error(`invalid HSV color: "${JSON.stringify(value)}"`);
133
+ }
134
+ break;
135
+ case 'HSVA':
136
+ if (!isValidHSVAColor(value)) {
137
+ throw new Error(`invalid HSVA color: "${JSON.stringify(value)}"`);
138
+ }
139
+ break;
140
+ case 'HWB':
141
+ if (!isValidHWBColor(value)) {
142
+ throw new Error(`invalid HWB color: "${JSON.stringify(value)}"`);
143
+ }
144
+ break;
145
+ case 'HWBA':
146
+ if (!isValidHWBAColor(value)) {
147
+ throw new Error(`invalid HWBA color: "${JSON.stringify(value)}"`);
148
+ }
149
+ break;
150
+ case 'CMYK':
151
+ if (!isValidCMYKColor(value)) {
152
+ throw new Error(`invalid CMYK color: "${JSON.stringify(value)}"`);
153
+ }
154
+ break;
155
+ case 'LAB':
156
+ if (!isValidLABColor(value)) {
157
+ throw new Error(`invalid LAB color: "${JSON.stringify(value)}"`);
158
+ }
159
+ break;
160
+ case 'OKLAB':
161
+ if (!isValidOKLABColor(value)) {
162
+ throw new Error(`invalid OKLAB color: "${JSON.stringify(value)}"`);
163
+ }
164
+ break;
165
+ case 'LCH':
166
+ if (!isValidLCHColor(value)) {
167
+ throw new Error(`invalid LCH color: "${JSON.stringify(value)}"`);
168
+ }
169
+ break;
170
+ case 'OKLCH':
171
+ if (!isValidOKLCHColor(value)) {
172
+ throw new Error(`invalid OKLCH color: "${JSON.stringify(value)}"`);
173
+ }
174
+ break;
175
+ default:
176
+ throw new Error(`unknown color format: "${formatType}"`);
177
+ }
178
+ }
@@ -0,0 +1,20 @@
1
+ import { Color } from './color/color';
2
+ import type { ColorSpace, ColorStringOptions } from './color/colorSpaces';
3
+ import type { BlendMode, BlendSpace, MixSpace, MixType } from './color/combinations';
4
+ import { AverageColorsOptions, BlendColorsOptions, MixColorsOptions } from './color/combinations';
5
+ import type { CIE94Options, CIEDE2000Options, DeltaEMethod, DeltaEOptions } from './color/deltaE';
6
+ import { ColorCMYK, type ColorFormat, ColorHex, ColorHSL, ColorHSLA, ColorHSV, ColorHSVA, ColorHWB, ColorHWBA, ColorLAB, ColorLCH, ColorOKLAB, ColorOKLCH, ColorRGB, ColorRGBA } from './color/formats';
7
+ import type { ColorGradientEasing, ColorGradientInterpolation, ColorGradientOptions, ColorGradientSpace, HueInterpolationMode } from './color/gradients';
8
+ import type { ColorHarmony, ColorHarmonyOptions, GrayscaleHandlingMode } from './color/harmonies';
9
+ import type { ColorBrightnessOptions, ColorBrightnessSpace, ColorSaturationOptions, ColorSaturationSpace } from './color/manipulations';
10
+ import type { BaseColorName, ColorLightnessModifier } from './color/names';
11
+ import { ColorNameAndLightness } from './color/names';
12
+ import { RandomColorOptions } from './color/random';
13
+ import type { APCAReadabilityOptions, APCAReadabilityPolicy, APCAReadabilityReport, APCAThresholdPreset, ReadabilityAlgorithm, ReadabilityOptions, WCAGReadabilityConformanceLevel, WCAGReadabilityTextSizeOptions } from './color/readability';
14
+ import { WCAGReadabilityOptions, WCAGReadabilityReport } from './color/readability';
15
+ import { ColorSwatch, ColorSwatchOptions, ExtendedColorSwatch } from './color/swatch';
16
+ import type { ColorTemperatureLabel } from './color/temperature';
17
+ import { ColorTemperatureAndLabel, ColorTemperatureStringFormatOptions } from './color/temperature';
18
+ import { ColorDarknessMode, IsColorDarkOptions } from './color/utils';
19
+ import { ColorPalette, GenerateColorPaletteOptions } from './palette/palette';
20
+ export { APCAReadabilityOptions, type APCAReadabilityPolicy, APCAReadabilityReport, type APCAThresholdPreset, AverageColorsOptions, type BaseColorName, BlendColorsOptions, type BlendMode, type BlendSpace, type CIE94Options, type CIEDE2000Options, Color, ColorBrightnessOptions, type ColorBrightnessSpace, ColorCMYK, ColorDarknessMode, type ColorFormat, ColorGradientEasing, ColorGradientInterpolation, ColorGradientOptions, ColorGradientSpace, type ColorHarmony, type ColorHarmonyOptions, ColorHex, ColorHSL, ColorHSLA, ColorHSV, ColorHSVA, ColorHWB, ColorHWBA, ColorLAB, ColorLCH, type ColorLightnessModifier, ColorNameAndLightness, ColorOKLAB, ColorOKLCH, ColorPalette, ColorRGB, ColorRGBA, ColorSaturationOptions, type ColorSaturationSpace, type ColorSpace, type ColorStringOptions, ColorSwatch, ColorSwatchOptions, ColorTemperatureAndLabel, type ColorTemperatureLabel, ColorTemperatureStringFormatOptions, type DeltaEMethod, type DeltaEOptions, ExtendedColorSwatch, GenerateColorPaletteOptions, type GrayscaleHandlingMode, HueInterpolationMode, IsColorDarkOptions, MixColorsOptions, type MixSpace, type MixType, RandomColorOptions, type ReadabilityAlgorithm, ReadabilityOptions, type WCAGReadabilityConformanceLevel, WCAGReadabilityOptions, WCAGReadabilityReport, type WCAGReadabilityTextSizeOptions, };
@@ -0,0 +1,2 @@
1
+ import { Color } from './color/color';
2
+ export { Color, };
@@ -0,0 +1 @@
1
+ export {};