@digdir/designsystemet 0.1.0-alpha.14 → 0.1.0-alpha.16

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 (125) hide show
  1. package/dist/bin/designsystemet.js +32 -3439
  2. package/dist/src/colors/colorUtils.js +301 -343
  3. package/dist/src/colors/index.js +3 -607
  4. package/dist/src/colors/themeUtils.js +319 -348
  5. package/dist/src/colors/types.js +1 -1
  6. package/dist/src/init/createTokensPackage.js +228 -0
  7. package/dist/src/init/generateMetadataJson.js +16 -0
  8. package/dist/src/init/generateThemesJson.js +50 -0
  9. package/dist/src/init/index.js +7 -0
  10. package/dist/src/init/nextStepsMarkdown.js +90 -0
  11. package/dist/src/init/template/default-files/README.md +10 -0
  12. package/dist/src/init/template/default-files/design-tokens/README.md +3 -0
  13. package/dist/src/init/template/default-files/design-tokens/primitives/globals.json +197 -0
  14. package/dist/src/init/template/default-files/design-tokens/primitives/typography/default.json +86 -0
  15. package/dist/src/init/template/default-files/design-tokens/semantic/color.json +562 -0
  16. package/dist/src/init/template/default-files/design-tokens/semantic/style.json +543 -0
  17. package/dist/src/init/template/prettier.config.js +7 -0
  18. package/dist/src/init/template/template-files/design-tokens/primitives/colors/contrast/global.json +376 -0
  19. package/dist/src/init/template/template-files/design-tokens/primitives/colors/contrast/theme-template.json +314 -0
  20. package/dist/src/init/template/template-files/design-tokens/primitives/colors/dark/global.json +376 -0
  21. package/dist/src/init/template/template-files/design-tokens/primitives/colors/dark/theme-template.json +314 -0
  22. package/dist/src/init/template/template-files/design-tokens/primitives/colors/light/global.json +376 -0
  23. package/dist/src/init/template/template-files/design-tokens/primitives/colors/light/theme-template.json +314 -0
  24. package/dist/src/init/template/template-files/design-tokens/themes/theme-template.json +314 -0
  25. package/dist/src/init/template/template-files/package.json +23 -0
  26. package/dist/src/init/utils.js +11 -0
  27. package/dist/src/migrations/beta-to-v1.js +341 -407
  28. package/dist/src/migrations/codemods/css/plugins.js +36 -42
  29. package/dist/src/migrations/codemods/css/run.js +20 -17
  30. package/dist/src/migrations/codemods/jsx/classname-prefix.js +63 -59
  31. package/dist/src/migrations/codemods/jsx/run.js +20 -20
  32. package/dist/src/migrations/index.js +5 -436
  33. package/dist/src/migrations/react-beta-to-v1.js +4 -27
  34. package/dist/src/test/a.css +39 -0
  35. package/dist/src/test/jsx-test.js +12 -0
  36. package/dist/src/tokens/actions.js +27 -2176
  37. package/dist/src/tokens/build.js +54 -2974
  38. package/dist/src/tokens/configs.js +234 -2874
  39. package/dist/src/tokens/formats/css.js +153 -2632
  40. package/dist/src/tokens/formats/js-tokens.js +28 -42
  41. package/dist/src/tokens/transformers.js +44 -82
  42. package/dist/src/tokens/utils/noCase.js +26 -28
  43. package/dist/src/tokens/utils/permutateThemes.js +65 -217
  44. package/dist/src/tokens/utils/utils.js +25 -14
  45. package/dist/types/bin/designsystemet.d.ts +3 -0
  46. package/dist/types/bin/designsystemet.d.ts.map +1 -0
  47. package/dist/types/src/colors/colorUtils.d.ts +126 -0
  48. package/dist/types/src/colors/colorUtils.d.ts.map +1 -0
  49. package/dist/types/src/colors/index.d.ts +4 -0
  50. package/dist/types/src/colors/index.d.ts.map +1 -0
  51. package/dist/types/src/colors/themeUtils.d.ts +102 -0
  52. package/dist/types/src/colors/themeUtils.d.ts.map +1 -0
  53. package/dist/types/src/colors/types.d.ts +16 -0
  54. package/dist/types/src/colors/types.d.ts.map +1 -0
  55. package/dist/types/src/init/createTokensPackage.d.ts +5 -0
  56. package/dist/types/src/init/createTokensPackage.d.ts.map +1 -0
  57. package/dist/types/src/init/generateMetadataJson.d.ts +6 -0
  58. package/dist/types/src/init/generateMetadataJson.d.ts.map +1 -0
  59. package/dist/types/src/init/generateThemesJson.d.ts +3 -0
  60. package/dist/types/src/init/generateThemesJson.d.ts.map +1 -0
  61. package/dist/types/src/init/index.d.ts +3 -0
  62. package/dist/types/src/init/index.d.ts.map +1 -0
  63. package/dist/types/src/init/nextStepsMarkdown.d.ts +3 -0
  64. package/dist/types/src/init/nextStepsMarkdown.d.ts.map +1 -0
  65. package/dist/types/src/init/template/prettier.config.d.mts +9 -0
  66. package/dist/types/src/init/template/prettier.config.d.mts.map +1 -0
  67. package/dist/types/src/init/utils.d.ts +4 -0
  68. package/dist/types/src/init/utils.d.ts.map +1 -0
  69. package/dist/types/src/migrations/beta-to-v1.d.ts +3 -0
  70. package/dist/types/src/migrations/beta-to-v1.d.ts.map +1 -0
  71. package/dist/types/src/migrations/codemods/css/plugins.d.ts +6 -0
  72. package/dist/types/src/migrations/codemods/css/plugins.d.ts.map +1 -0
  73. package/dist/types/src/migrations/codemods/css/run.d.ts +8 -0
  74. package/dist/types/src/migrations/codemods/css/run.d.ts.map +1 -0
  75. package/dist/types/src/migrations/codemods/jsx/classname-prefix.d.ts +10 -0
  76. package/dist/types/src/migrations/codemods/jsx/classname-prefix.d.ts.map +1 -0
  77. package/dist/types/src/migrations/codemods/jsx/run.d.ts +7 -0
  78. package/dist/types/src/migrations/codemods/jsx/run.d.ts.map +1 -0
  79. package/dist/types/src/migrations/index.d.ts +6 -0
  80. package/dist/types/src/migrations/index.d.ts.map +1 -0
  81. package/dist/types/src/migrations/react-beta-to-v1.d.ts +3 -0
  82. package/dist/types/src/migrations/react-beta-to-v1.d.ts.map +1 -0
  83. package/dist/types/src/test/jsx-test.d.ts +4 -0
  84. package/dist/types/src/test/jsx-test.d.ts.map +1 -0
  85. package/dist/types/src/tokens/actions.d.ts +6 -0
  86. package/dist/types/src/tokens/actions.d.ts.map +1 -0
  87. package/dist/types/src/tokens/build.d.ts +11 -0
  88. package/dist/types/src/tokens/build.d.ts.map +1 -0
  89. package/dist/types/src/tokens/configs.d.ts +31 -0
  90. package/dist/types/src/tokens/configs.d.ts.map +1 -0
  91. package/dist/types/src/tokens/formats/css.d.ts +5 -0
  92. package/dist/types/src/tokens/formats/css.d.ts.map +1 -0
  93. package/dist/types/src/tokens/formats/js-tokens.d.ts +6 -0
  94. package/dist/types/src/tokens/formats/js-tokens.d.ts.map +1 -0
  95. package/dist/types/src/tokens/transformers.d.ts +5 -0
  96. package/dist/types/src/tokens/transformers.d.ts.map +1 -0
  97. package/dist/types/src/tokens/utils/noCase.d.ts +11 -0
  98. package/dist/types/src/tokens/utils/noCase.d.ts.map +1 -0
  99. package/dist/types/src/tokens/utils/permutateThemes.d.ts +17 -0
  100. package/dist/types/src/tokens/utils/permutateThemes.d.ts.map +1 -0
  101. package/dist/types/src/tokens/utils/utils.d.ts +24 -0
  102. package/dist/types/src/tokens/utils/utils.d.ts.map +1 -0
  103. package/package.json +11 -17
  104. package/LICENSE +0 -7
  105. package/dist/bin/designsystemet.js.map +0 -1
  106. package/dist/src/colors/colorUtils.js.map +0 -1
  107. package/dist/src/colors/index.js.map +0 -1
  108. package/dist/src/colors/themeUtils.js.map +0 -1
  109. package/dist/src/colors/types.js.map +0 -1
  110. package/dist/src/migrations/beta-to-v1.js.map +0 -1
  111. package/dist/src/migrations/codemods/css/plugins.js.map +0 -1
  112. package/dist/src/migrations/codemods/css/run.js.map +0 -1
  113. package/dist/src/migrations/codemods/jsx/classname-prefix.js.map +0 -1
  114. package/dist/src/migrations/codemods/jsx/run.js.map +0 -1
  115. package/dist/src/migrations/index.js.map +0 -1
  116. package/dist/src/migrations/react-beta-to-v1.js.map +0 -1
  117. package/dist/src/tokens/actions.js.map +0 -1
  118. package/dist/src/tokens/build.js.map +0 -1
  119. package/dist/src/tokens/configs.js.map +0 -1
  120. package/dist/src/tokens/formats/css.js.map +0 -1
  121. package/dist/src/tokens/formats/js-tokens.js.map +0 -1
  122. package/dist/src/tokens/transformers.js.map +0 -1
  123. package/dist/src/tokens/utils/noCase.js.map +0 -1
  124. package/dist/src/tokens/utils/permutateThemes.js.map +0 -1
  125. package/dist/src/tokens/utils/utils.js.map +0 -1
@@ -1,364 +1,322 @@
1
- // src/colors/colorUtils.ts
2
1
  import { Hsluv } from "hsluv";
3
2
  import chroma from "chroma-js";
4
-
5
- // ../../node_modules/apca-w3/src/apca-w3.js
6
- var SA98G = {
7
- mainTRC: 2.4,
8
- // 2.4 exponent for emulating actual monitor perception
9
- // For reverseAPCA
10
- get mainTRCencode() {
11
- return 1 / this.mainTRC;
12
- },
13
- // sRGB coefficients
14
- sRco: 0.2126729,
15
- sGco: 0.7151522,
16
- sBco: 0.072175,
17
- // G-4g constants for use with 2.4 exponent
18
- normBG: 0.56,
19
- normTXT: 0.57,
20
- revTXT: 0.62,
21
- revBG: 0.65,
22
- // G-4g Clamps and Scalers
23
- blkThrs: 0.022,
24
- blkClmp: 1.414,
25
- scaleBoW: 1.14,
26
- scaleWoB: 1.14,
27
- loBoWoffset: 0.027,
28
- loWoBoffset: 0.027,
29
- deltaYmin: 5e-4,
30
- loClip: 0.1,
31
- ///// MAGIC NUMBERS for UNCLAMP, for use with 0.022 & 1.414 /////
32
- // Magic Numbers for reverseAPCA
33
- mFactor: 1.9468554433171,
34
- get mFactInv() {
35
- return 1 / this.mFactor;
36
- },
37
- mOffsetIn: 0.0387393816571401,
38
- mExpAdj: 0.283343396420869,
39
- get mExp() {
40
- return this.mExpAdj / this.blkClmp;
41
- },
42
- mOffsetOut: 0.312865795870758
43
- };
44
- function APCAcontrast(txtY, bgY, places = -1) {
45
- const icp = [0, 1.1];
46
- if (isNaN(txtY) || isNaN(bgY) || Math.min(txtY, bgY) < icp[0] || Math.max(txtY, bgY) > icp[1]) {
47
- return 0;
48
- }
49
- ;
50
- let SAPC = 0;
51
- let outputContrast = 0;
52
- let polCat = "BoW";
53
- txtY = txtY > SA98G.blkThrs ? txtY : txtY + Math.pow(SA98G.blkThrs - txtY, SA98G.blkClmp);
54
- bgY = bgY > SA98G.blkThrs ? bgY : bgY + Math.pow(SA98G.blkThrs - bgY, SA98G.blkClmp);
55
- if (Math.abs(bgY - txtY) < SA98G.deltaYmin) {
56
- return 0;
57
- }
58
- if (bgY > txtY) {
59
- SAPC = (Math.pow(bgY, SA98G.normBG) - Math.pow(txtY, SA98G.normTXT)) * SA98G.scaleBoW;
60
- outputContrast = SAPC < SA98G.loClip ? 0 : SAPC - SA98G.loBoWoffset;
61
- } else {
62
- polCat = "WoB";
63
- SAPC = (Math.pow(bgY, SA98G.revBG) - Math.pow(txtY, SA98G.revTXT)) * SA98G.scaleWoB;
64
- outputContrast = SAPC > -SA98G.loClip ? 0 : SAPC + SA98G.loWoBoffset;
65
- }
66
- if (places < 0) {
67
- return outputContrast * 100;
68
- } else if (places == 0) {
69
- return Math.round(Math.abs(outputContrast) * 100) + "<sub>" + polCat + "</sub>";
70
- } else if (Number.isInteger(places)) {
71
- return (outputContrast * 100).toFixed(places);
72
- } else {
73
- return 0;
74
- }
75
- }
76
- function sRGBtoY(rgb = [0, 0, 0]) {
77
- function simpleExp(chan) {
78
- return Math.pow(chan / 255, SA98G.mainTRC);
79
- }
80
- ;
81
- return SA98G.sRco * simpleExp(rgb[0]) + SA98G.sGco * simpleExp(rgb[1]) + SA98G.sBco * simpleExp(rgb[2]);
82
- }
83
-
84
- // src/colors/colorUtils.ts
85
- var hexToCssHsl = (hex, valuesOnly = false) => {
86
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
87
- let r = 0;
88
- let g = 0;
89
- let b = 0;
90
- let cssString = "";
91
- if (result) {
92
- r = parseInt(result[1], 16);
93
- g = parseInt(result[2], 16);
94
- b = parseInt(result[3], 16);
95
- }
96
- r /= 255, g /= 255, b /= 255;
97
- const max = Math.max(r, g, b), min = Math.min(r, g, b);
98
- let h, s, l = (max + min) / 2;
99
- if (max == min) {
100
- h = s = 0;
101
- } else {
102
- const d = max - min;
103
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
104
- switch (max) {
105
- case r:
106
- h = (g - b) / d + (g < b ? 6 : 0);
107
- break;
108
- case g:
109
- h = (b - r) / d + 2;
110
- break;
111
- case b:
112
- h = (r - g) / d + 4;
113
- break;
3
+ import { APCAcontrast, sRGBtoY } from "apca-w3";
4
+ /**
5
+ * Converts a HEX color '#xxxxxx' into a CSS HSL string 'hsl(x,x,x)'
6
+ *
7
+ * @param hex A hex color string
8
+ * @param valuesOnly If true, only the values are returned
9
+ * @returns A CSS HSL string
10
+ */ export const hexToCssHsl = (hex, valuesOnly = false)=>{
11
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
12
+ let r = 0;
13
+ let g = 0;
14
+ let b = 0;
15
+ let cssString = '';
16
+ if (result) {
17
+ r = parseInt(result[1], 16);
18
+ g = parseInt(result[2], 16);
19
+ b = parseInt(result[3], 16);
114
20
  }
115
- }
116
- h = Math.round(h ? h * 360 : 0);
117
- s = Math.round(s * 100);
118
- l = Math.round(l * 100);
119
- cssString = h + "," + s + "%," + l + "%";
120
- cssString = !valuesOnly ? "hsl(" + cssString + ")" : cssString;
121
- return cssString;
21
+ r /= 255, g /= 255, b /= 255;
22
+ const max = Math.max(r, g, b), min = Math.min(r, g, b);
23
+ let h, s, l = (max + min) / 2;
24
+ if (max == min) {
25
+ h = s = 0; // achromatic
26
+ } else {
27
+ const d = max - min;
28
+ s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
29
+ switch(max){
30
+ case r:
31
+ h = (g - b) / d + (g < b ? 6 : 0);
32
+ break;
33
+ case g:
34
+ h = (b - r) / d + 2;
35
+ break;
36
+ case b:
37
+ h = (r - g) / d + 4;
38
+ break;
39
+ }
40
+ }
41
+ h = Math.round(h ? h * 360 : 0);
42
+ s = Math.round(s * 100);
43
+ l = Math.round(l * 100);
44
+ cssString = h + ',' + s + '%,' + l + '%';
45
+ cssString = !valuesOnly ? 'hsl(' + cssString + ')' : cssString;
46
+ return cssString;
122
47
  };
123
- var hexToHSL = (H) => {
124
- let r = 0, g = 0, b = 0;
125
- if (H.length == 4) {
126
- r = parseInt("0x" + H[1] + H[1]);
127
- g = parseInt("0x" + H[2] + H[2]);
128
- b = parseInt("0x" + H[3] + H[3]);
129
- } else if (H.length == 7) {
130
- r = parseInt("0x" + H[1] + H[2]);
131
- g = parseInt("0x" + H[3] + H[4]);
132
- b = parseInt("0x" + H[5] + H[6]);
133
- }
134
- r /= 255;
135
- g /= 255;
136
- b /= 255;
137
- let h = 0, s = 0, l = 0;
138
- const cmin = Math.min(r, g, b), cmax = Math.max(r, g, b), delta = cmax - cmin;
139
- if (delta == 0) h = 0;
140
- else if (cmax == r) h = (g - b) / delta % 6;
141
- else if (cmax == g) h = (b - r) / delta + 2;
142
- else h = (r - g) / delta + 4;
143
- h = Math.round(h * 60);
144
- if (h < 0) h += 360;
145
- l = (cmax + cmin) / 2;
146
- s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
147
- s = +(s * 100).toFixed(1);
148
- l = +(l * 100).toFixed(1);
149
- return [h, s, l];
48
+ /**
49
+ * Converts a HEX string '#xxxxxx' into an array of HSL values '[h,s,l]'
50
+ *
51
+ * @param H A Hex color string
52
+ * @returns HSL values in an array
53
+ */ export const hexToHSL = (H)=>{
54
+ // Convert hex to RGB first
55
+ let r = 0, g = 0, b = 0;
56
+ if (H.length == 4) {
57
+ r = parseInt('0x' + H[1] + H[1]);
58
+ g = parseInt('0x' + H[2] + H[2]);
59
+ b = parseInt('0x' + H[3] + H[3]);
60
+ } else if (H.length == 7) {
61
+ r = parseInt('0x' + H[1] + H[2]);
62
+ g = parseInt('0x' + H[3] + H[4]);
63
+ b = parseInt('0x' + H[5] + H[6]);
64
+ }
65
+ // Then to HSL
66
+ r /= 255;
67
+ g /= 255;
68
+ b /= 255;
69
+ let h = 0, s = 0, l = 0;
70
+ const cmin = Math.min(r, g, b), cmax = Math.max(r, g, b), delta = cmax - cmin;
71
+ if (delta == 0) h = 0;
72
+ else if (cmax == r) h = (g - b) / delta % 6;
73
+ else if (cmax == g) h = (b - r) / delta + 2;
74
+ else h = (r - g) / delta + 4;
75
+ h = Math.round(h * 60);
76
+ if (h < 0) h += 360;
77
+ l = (cmax + cmin) / 2;
78
+ s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
79
+ s = +(s * 100).toFixed(1);
80
+ l = +(l * 100).toFixed(1);
81
+ return [
82
+ h,
83
+ s,
84
+ l
85
+ ];
150
86
  };
151
- var hexToHsluv = (hex) => {
152
- const conv = new Hsluv();
153
- conv.hex = hex;
154
- conv.hexToHsluv();
155
- return [conv.hsluv_h, conv.hsluv_s, conv.hsluv_l];
87
+ /**
88
+ * Converts a HEX color '#xxxxxx' into an array of HSLuv values '[h,s,l]'
89
+ *
90
+ * @param hex A hex color string
91
+ * @returns
92
+ */ export const hexToHsluv = (hex)=>{
93
+ const conv = new Hsluv();
94
+ conv.hex = hex;
95
+ conv.hexToHsluv();
96
+ return [
97
+ conv.hsluv_h,
98
+ conv.hsluv_s,
99
+ conv.hsluv_l
100
+ ];
156
101
  };
157
- var hslArrToCss = (HSL) => {
158
- return "hsl(" + HSL[0] + "," + HSL[1] + "%," + HSL[2] + "%)";
102
+ /**
103
+ * Converts a HSL number array '[h,s,l]' into HSL CSS string 'hsl(x,x,x)'
104
+ *
105
+ * @param HSL A HSL number array '[h,s,l]'
106
+ * @returns A hex color string
107
+ */ export const hslArrToCss = (HSL)=>{
108
+ return 'hsl(' + HSL[0] + ',' + HSL[1] + '%,' + HSL[2] + '%)';
159
109
  };
160
- var HSLToHex = (h, s, l) => {
161
- s /= 100;
162
- l /= 100;
163
- let r = 0, g = 0, b = 0;
164
- const c = (1 - Math.abs(2 * l - 1)) * s, x = c * (1 - Math.abs(h / 60 % 2 - 1)), m = l - c / 2;
165
- if (0 <= h && h < 60) {
166
- r = c;
167
- g = x;
168
- b = 0;
169
- } else if (60 <= h && h < 120) {
170
- r = x;
171
- g = c;
172
- b = 0;
173
- } else if (120 <= h && h < 180) {
174
- r = 0;
175
- g = c;
176
- b = x;
177
- } else if (180 <= h && h < 240) {
178
- r = 0;
179
- g = x;
180
- b = c;
181
- } else if (240 <= h && h < 300) {
182
- r = x;
183
- g = 0;
184
- b = c;
185
- } else if (300 <= h && h < 360) {
186
- r = c;
187
- g = 0;
188
- b = x;
189
- }
190
- r = parseInt(Math.round((r + m) * 255).toString(16), 16);
191
- g = parseInt(Math.round((g + m) * 255).toString(16), 16);
192
- b = parseInt(Math.round((b + m) * 255).toString(16), 16);
193
- if (r.toString().length == 1) r = parseInt("0" + r.toString(), 10);
194
- if (g.toString().length == 1) g = parseInt("0" + g.toString(), 10);
195
- if (b.toString().length == 1) b = parseInt("0" + b.toString(), 10);
196
- return "#" + r + g + b;
110
+ /**
111
+ * Converts a HSL CSS string 'hsl(x,x,x)' into an array of HSL values '[h,s,l]'
112
+ *
113
+ * @param h The HSL hue
114
+ * @param s The HSL saturation
115
+ * @param l The HSL lightness
116
+ * @returns HEX color string
117
+ */ export const HSLToHex = (h, s, l)=>{
118
+ s /= 100;
119
+ l /= 100;
120
+ let r = 0, g = 0, b = 0;
121
+ const c = (1 - Math.abs(2 * l - 1)) * s, x = c * (1 - Math.abs(h / 60 % 2 - 1)), m = l - c / 2;
122
+ if (0 <= h && h < 60) {
123
+ r = c;
124
+ g = x;
125
+ b = 0;
126
+ } else if (60 <= h && h < 120) {
127
+ r = x;
128
+ g = c;
129
+ b = 0;
130
+ } else if (120 <= h && h < 180) {
131
+ r = 0;
132
+ g = c;
133
+ b = x;
134
+ } else if (180 <= h && h < 240) {
135
+ r = 0;
136
+ g = x;
137
+ b = c;
138
+ } else if (240 <= h && h < 300) {
139
+ r = x;
140
+ g = 0;
141
+ b = c;
142
+ } else if (300 <= h && h < 360) {
143
+ r = c;
144
+ g = 0;
145
+ b = x;
146
+ }
147
+ // Having obtained RGB, convert channels to hex
148
+ r = parseInt(Math.round((r + m) * 255).toString(16), 16);
149
+ g = parseInt(Math.round((g + m) * 255).toString(16), 16);
150
+ b = parseInt(Math.round((b + m) * 255).toString(16), 16);
151
+ // Prepend 0s, if necessary
152
+ if (r.toString().length == 1) r = parseInt('0' + r.toString(), 10);
153
+ if (g.toString().length == 1) g = parseInt('0' + g.toString(), 10);
154
+ if (b.toString().length == 1) b = parseInt('0' + b.toString(), 10);
155
+ return '#' + r + g + b;
197
156
  };
198
- var hexToRgb = (hex) => {
199
- const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
200
- hex = hex.replace(shorthandRegex, function(m, r, g, b) {
201
- return r + r + g + g + b + b;
202
- });
203
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
204
- return result ? {
205
- r: parseInt(result[1], 16),
206
- g: parseInt(result[2], 16),
207
- b: parseInt(result[3], 16)
208
- } : null;
157
+ /**
158
+ * Converts a HEX color '#xxxxxx' into an array of RGB values '[R, G, B]'
159
+ *
160
+ * @param hex A hex color string
161
+ * @returns RGB values in an array
162
+ */ export const hexToRgb = (hex)=>{
163
+ const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
164
+ hex = hex.replace(shorthandRegex, function(m, r, g, b) {
165
+ return r + r + g + g + b + b;
166
+ });
167
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
168
+ return result ? {
169
+ r: parseInt(result[1], 16),
170
+ g: parseInt(result[2], 16),
171
+ b: parseInt(result[3], 16)
172
+ } : null;
209
173
  };
210
- var luminanceFromRgb = (r, g, b) => {
211
- const a = [Number(r), Number(g), Number(b)].map(function(v) {
212
- v /= 255;
213
- return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
214
- });
215
- return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
174
+ /**
175
+ * Get the luminance of an RGB color
176
+ *
177
+ * @param r RGB red value
178
+ * @param G RGB green value
179
+ * @param b RGB blue value
180
+ * @returns
181
+ */ export const luminanceFromRgb = (r, g, b)=>{
182
+ const a = [
183
+ Number(r),
184
+ Number(g),
185
+ Number(b)
186
+ ].map(function(v) {
187
+ v /= 255;
188
+ return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
189
+ });
190
+ return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
216
191
  };
217
- var luminanceFromHex = (hex) => {
218
- const rgb = hexToRgb(hex);
219
- if (rgb) {
220
- const r = rgb.r.toString();
221
- const g = rgb.g.toString();
222
- const b = rgb.b.toString();
223
- return luminanceFromRgb(r, g, b);
224
- }
225
- return 2;
192
+ /**
193
+ * Get the luminance of a HEX color
194
+ *
195
+ * @param hex A hex color string
196
+ * @returns
197
+ */ export const luminanceFromHex = (hex)=>{
198
+ const rgb = hexToRgb(hex);
199
+ if (rgb) {
200
+ const r = rgb.r.toString();
201
+ const g = rgb.g.toString();
202
+ const b = rgb.b.toString();
203
+ return luminanceFromRgb(r, g, b);
204
+ }
205
+ return 2;
226
206
  };
227
- var getRatioFromLum = (lum1, lum2) => {
228
- if (lum1 !== null && lum2 !== null) {
229
- return (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
230
- } else {
231
- return -1;
232
- }
207
+ /**
208
+ * Get the contrast ratio between two luminance values
209
+ *
210
+ * @param lum1 The first luminance value
211
+ * @param lum2 The second luminance value
212
+ * @returns
213
+ */ export const getRatioFromLum = (lum1, lum2)=>{
214
+ if (lum1 !== null && lum2 !== null) {
215
+ return (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
216
+ } else {
217
+ return -1;
218
+ }
233
219
  };
234
- var getHslLightessFromHex = (hex) => {
235
- return chroma(hex).hsl()[2];
220
+ /**
221
+ * Get the HSL lightness from a HEX color
222
+ *
223
+ * @param hex A hex color string
224
+ * @returns
225
+ */ export const getHslLightessFromHex = (hex)=>{
226
+ return chroma(hex).hsl()[2];
236
227
  };
237
- var getHslSaturationFromHex = (hex) => {
238
- return chroma(hex).hsl()[1];
228
+ export const getHslSaturationFromHex = (hex)=>{
229
+ return chroma(hex).hsl()[1];
239
230
  };
240
- var getLightnessFromHex = (hex) => {
241
- const conv = new Hsluv();
242
- conv.hex = hex;
243
- conv.hexToHsluv();
244
- return Number(conv.hsluv_l.toFixed(0));
231
+ /**
232
+ * Get the HSLuv lightness from a HEX color
233
+ *
234
+ * @param hex A hex color string
235
+ * @returns
236
+ */ export const getLightnessFromHex = (hex)=>{
237
+ const conv = new Hsluv();
238
+ conv.hex = hex;
239
+ conv.hexToHsluv();
240
+ return Number(conv.hsluv_l.toFixed(0));
245
241
  };
246
- var getContrastFromHex = (color1, color2) => {
247
- const lum1 = luminanceFromHex(color1);
248
- const lum2 = luminanceFromHex(color2);
249
- if (lum1 !== null && lum2 !== null) {
250
- return getRatioFromLum(lum1, lum2);
251
- }
252
- return -1;
242
+ /**
243
+ * Get the contrast ratio between two HEX colors
244
+ *
245
+ * @param {CssColor} color1 The first color
246
+ * @param {CssColor} color2 The second color
247
+ * @returns
248
+ */ export const getContrastFromHex = (color1, color2)=>{
249
+ const lum1 = luminanceFromHex(color1);
250
+ const lum2 = luminanceFromHex(color2);
251
+ if (lum1 !== null && lum2 !== null) {
252
+ return getRatioFromLum(lum1, lum2);
253
+ }
254
+ return -1;
253
255
  };
254
- var getContrastFromLightness = (lightness, mainColor, backgroundColor) => {
255
- const conv = new Hsluv();
256
- conv.hex = mainColor;
257
- conv.hexToHsluv();
258
- conv.hsluv_l = lightness;
259
- conv.hsluvToHex();
260
- const lightMainColor = conv.hex;
261
- const lum1 = luminanceFromHex(lightMainColor);
262
- const lum2 = luminanceFromHex(backgroundColor);
263
- const ratio = getRatioFromLum(lum1 ?? 0, lum2 ?? 0);
264
- return ratio;
256
+ /**
257
+ * Get the contrast ratio between two colors at a specific lightness
258
+ *
259
+ * @param lightness The lightness value
260
+ * @param mainColor The main color
261
+ * @param backgroundColor The background color
262
+ * @returns The contrast ratio
263
+ */ export const getContrastFromLightness = (lightness, mainColor, backgroundColor)=>{
264
+ const conv = new Hsluv();
265
+ conv.hex = mainColor;
266
+ conv.hexToHsluv();
267
+ conv.hsluv_l = lightness;
268
+ conv.hsluvToHex();
269
+ const lightMainColor = conv.hex;
270
+ const lum1 = luminanceFromHex(lightMainColor);
271
+ const lum2 = luminanceFromHex(backgroundColor);
272
+ const ratio = getRatioFromLum(lum1 ?? 0, lum2 ?? 0);
273
+ return ratio;
265
274
  };
266
- var areColorsContrasting = (color1, color2, type = "aa") => {
267
- const contrast = getContrastFromHex(color1, color2);
268
- if (contrast !== null) {
269
- if (type === "aaa") {
270
- return contrast >= 7;
271
- } else if (type === "aa") {
272
- return contrast >= 4.5;
273
- } else {
274
- return contrast >= 3;
275
+ /**
276
+ * Maps the numbers from [start1 - end1] to the range [start2 - end2], maintaining the proportionality between the numbers in the ranges using lineaer interpolation.
277
+ */ // const mapRange = (value: number, start1: number, end1: number, start2: number, end2: number) => {
278
+ // return start2 + ((value - start1) * (end2 - start2)) / (end1 - start1);
279
+ // };
280
+ /**
281
+ * Check if two colors have enough contrast to be used together
282
+ *
283
+ * @param {CssColor} color1 The first color
284
+ * @param {CssColor} color2 The second color
285
+ * @returns {boolean} If the colors have enough contrast
286
+ */ export const areColorsContrasting = (color1, color2, type = 'aa')=>{
287
+ const contrast = getContrastFromHex(color1, color2);
288
+ if (contrast !== null) {
289
+ if (type === 'aaa') {
290
+ return contrast >= 7;
291
+ } else if (type === 'aa') {
292
+ return contrast >= 4.5;
293
+ } else {
294
+ return contrast >= 3;
295
+ }
275
296
  }
276
- }
277
- return false;
278
- };
279
- var getApcaContrastLc = (textColor, backgroundColor) => {
280
- const textColorRgb = hexToRgb(textColor);
281
- const backgroundColorRgb = hexToRgb(backgroundColor);
282
- if (textColorRgb && backgroundColorRgb) {
283
- return APCAcontrast(
284
- sRGBtoY([textColorRgb.r, textColorRgb.g, textColorRgb.b]),
285
- sRGBtoY([backgroundColorRgb.r, backgroundColorRgb.g, backgroundColorRgb.b])
286
- );
287
- }
288
- return 0;
297
+ return false;
289
298
  };
290
- var isHexColor = (hex) => {
291
- return typeof hex === "string" && hex.length === 6 && !isNaN(Number("0x" + hex));
299
+ export const getApcaContrastLc = (textColor, backgroundColor)=>{
300
+ const textColorRgb = hexToRgb(textColor);
301
+ const backgroundColorRgb = hexToRgb(backgroundColor);
302
+ if (textColorRgb && backgroundColorRgb) {
303
+ return APCAcontrast(sRGBtoY([
304
+ textColorRgb.r,
305
+ textColorRgb.g,
306
+ textColorRgb.b
307
+ ]), sRGBtoY([
308
+ backgroundColorRgb.r,
309
+ backgroundColorRgb.g,
310
+ backgroundColorRgb.b
311
+ ]));
312
+ }
313
+ return 0;
292
314
  };
293
- export {
294
- HSLToHex,
295
- areColorsContrasting,
296
- getApcaContrastLc,
297
- getContrastFromHex,
298
- getContrastFromLightness,
299
- getHslLightessFromHex,
300
- getHslSaturationFromHex,
301
- getLightnessFromHex,
302
- getRatioFromLum,
303
- hexToCssHsl,
304
- hexToHSL,
305
- hexToHsluv,
306
- hexToRgb,
307
- hslArrToCss,
308
- isHexColor,
309
- luminanceFromHex,
310
- luminanceFromRgb
315
+ /**
316
+ * Check if aa string value is a HEX color
317
+ *
318
+ * @param {string} hex The string to check
319
+ * @returns {boolean} If the string is a HEX color
320
+ */ export const isHexColor = (hex)=>{
321
+ return typeof hex === 'string' && hex.length === 6 && !isNaN(Number('0x' + hex));
311
322
  };
312
- /*! Bundled license information:
313
-
314
- colorparsley/src/colorparsley.js:
315
- (** @preserve
316
- ///// CoLoR PaRsLeY a simple set of color parsing thingies!
317
- ///// Beta 0.1.8 Revision date: June 04, 2022
318
- /////
319
- ///// Functions to parse color values and return array
320
- ///// Copyright (c) 2019-2022 by Andrew Somers. All Rights Reserved.
321
- ///// LICENSE: AGPL 3
322
- ///// CONTACT: Please use the ISSUES or DISCUSSIONS tab at:
323
- ///// https://github.com/Myndex/colorparsley/
324
- /////
325
- ///////////////////////////////////////////////////////////////////////////////
326
- /////
327
- ///// IMPORT:
328
- ///// import { colorParsley } from 'colorparsley';
329
- /////
330
- ///// let rgbaArray = colorParsley('#abcdef');
331
- /////
332
- ///// Output as array: [r,g,b,a,isValid,colorspace]
333
- ///// Example: [123,123,123,1.0,true,'sRGB']
334
- // *)
335
-
336
- apca-w3/src/apca-w3.js:
337
- (** @preserve
338
- ///// SAPC APCA - Advanced Perceptual Contrast Algorithm
339
- ///// Beta 0.1.9 W3 • contrast function only
340
- ///// DIST: W3 • Revision date: July 3, 2022
341
- ///// Function to parse color values and determine Lc contrast
342
- ///// Copyright © 2019-2022 by Andrew Somers. All Rights Reserved.
343
- ///// LICENSE: W3 LICENSE
344
- ///// CONTACT: Please use the ISSUES or DISCUSSIONS tab at:
345
- ///// https://github.com/Myndex/SAPC-APCA/
346
- /////
347
- ///////////////////////////////////////////////////////////////////////////////
348
- /////
349
- ///// MINIMAL IMPORTS:
350
- ///// import { APCAcontrast, sRGBtoY, displayP3toY,
351
- ///// calcAPCA, fontLookupAPCA } from 'apca-w3';
352
- ///// import { colorParsley } from 'colorparsley';
353
- /////
354
- ///// FORWARD CONTRAST USAGE:
355
- ///// Lc = APCAcontrast( sRGBtoY( TEXTcolor ) , sRGBtoY( BACKGNDcolor ) );
356
- ///// Where the colors are sent as an rgba array [255,255,255,1]
357
- /////
358
- ///// Retrieving an array of font sizes for the contrast:
359
- ///// fontArray = fontLookupAPCA(Lc);
360
- /////
361
- ///// Live Demonstrator at https://www.myndex.com/APCA/
362
- // *)
363
- */
364
- //# sourceMappingURL=colorUtils.js.map