@digdir/designsystemet 0.1.0-alpha.1 → 0.1.0-alpha.10
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.
- package/dist/build/bin/designsystemet.js +1 -1
- package/dist/build/src/colors/colorUtils.js +314 -0
- package/dist/build/src/colors/index.js +3 -0
- package/dist/build/src/colors/themeUtils.js +293 -0
- package/dist/build/src/colors/types.js +1 -0
- package/dist/build/src/migrations/beta-to-v1.js +67 -7
- package/dist/build/src/migrations/codemods/jsx/run.js +19 -0
- package/dist/build/src/migrations/index.js +3 -1
- package/dist/build/src/migrations/react-beta-to-v1.js +2 -0
- package/dist/build/src/test/jsx-test.js +1 -1
- package/dist/build/src/tokens/actions.js +20 -0
- package/dist/build/src/tokens/build.js +48 -129
- package/dist/build/src/tokens/configs.js +229 -0
- package/dist/build/src/tokens/formats/css-classes.js +50 -0
- package/dist/build/src/tokens/formats/css-variables.js +39 -0
- package/dist/build/src/tokens/formats/js-tokens.js +29 -0
- package/dist/build/src/tokens/transformers.js +18 -15
- package/dist/build/src/tokens/utils/permutateThemes.js +56 -0
- package/dist/build/src/tokens/utils/utils.js +21 -0
- package/dist/build/tsconfig.tsbuildinfo +1 -1
- package/dist/types/src/colors/colorUtils.d.ts +118 -0
- package/dist/types/src/colors/colorUtils.d.ts.map +1 -0
- package/dist/types/src/colors/index.d.ts +4 -0
- package/dist/types/src/colors/index.d.ts.map +1 -0
- package/dist/types/src/colors/themeUtils.d.ts +102 -0
- package/dist/types/src/colors/themeUtils.d.ts.map +1 -0
- package/dist/types/src/colors/types.d.ts +16 -0
- package/dist/types/src/colors/types.d.ts.map +1 -0
- package/dist/types/src/migrations/beta-to-v1.d.ts.map +1 -1
- package/dist/types/src/migrations/codemods/css/plugins.d.ts.map +1 -0
- package/dist/types/src/migrations/codemods/css/run.d.ts.map +1 -0
- package/dist/types/src/migrations/codemods/jsx/classname-prefix.d.ts.map +1 -0
- package/dist/types/src/migrations/codemods/jsx/run.d.ts +7 -0
- package/dist/types/src/migrations/codemods/jsx/run.d.ts.map +1 -0
- package/dist/types/src/migrations/index.d.ts +2 -1
- package/dist/types/src/migrations/index.d.ts.map +1 -1
- package/dist/types/src/migrations/react-beta-to-v1.d.ts +3 -0
- package/dist/types/src/migrations/react-beta-to-v1.d.ts.map +1 -0
- package/dist/types/src/test/jsx-test.d.ts.map +1 -1
- package/dist/types/src/tokens/actions.d.ts +3 -0
- package/dist/types/src/tokens/actions.d.ts.map +1 -0
- package/dist/types/src/tokens/build.d.ts.map +1 -1
- package/dist/types/src/tokens/configs.d.ts +23 -0
- package/dist/types/src/tokens/configs.d.ts.map +1 -0
- package/dist/types/src/tokens/formats/css-classes.d.ts +6 -0
- package/dist/types/src/tokens/formats/css-classes.d.ts.map +1 -0
- package/dist/types/src/tokens/formats/css-variables.d.ts +3 -0
- package/dist/types/src/tokens/formats/css-variables.d.ts.map +1 -0
- package/dist/types/src/tokens/formats/js-tokens.d.ts +6 -0
- package/dist/types/src/tokens/formats/js-tokens.d.ts.map +1 -0
- package/dist/types/src/tokens/transformers.d.ts.map +1 -1
- package/dist/types/src/tokens/utils/noCase.d.ts.map +1 -0
- package/dist/types/src/tokens/utils/permutateThemes.d.ts +7 -0
- package/dist/types/src/tokens/utils/permutateThemes.d.ts.map +1 -0
- package/dist/types/src/tokens/utils/utils.d.ts +17 -0
- package/dist/types/src/tokens/utils/utils.d.ts.map +1 -0
- package/package.json +20 -14
- package/dist/build/src/tokens/formatters.js +0 -79
- package/dist/types/src/codemods/css/plugins.d.ts.map +0 -1
- package/dist/types/src/codemods/css/run.d.ts.map +0 -1
- package/dist/types/src/codemods/jsx/classname-prefix.d.ts.map +0 -1
- package/dist/types/src/tokens/formatters.d.ts +0 -10
- package/dist/types/src/tokens/formatters.d.ts.map +0 -1
- package/dist/types/src/tokens/noCase.d.ts.map +0 -1
- /package/dist/build/src/{codemods → migrations/codemods}/css/plugins.js +0 -0
- /package/dist/build/src/{codemods → migrations/codemods}/css/run.js +0 -0
- /package/dist/build/src/{codemods → migrations/codemods}/jsx/classname-prefix.js +0 -0
- /package/dist/build/src/tokens/{noCase.js → utils/noCase.js} +0 -0
- /package/dist/types/src/{codemods → migrations/codemods}/css/plugins.d.ts +0 -0
- /package/dist/types/src/{codemods → migrations/codemods}/css/run.d.ts +0 -0
- /package/dist/types/src/{codemods → migrations/codemods}/jsx/classname-prefix.d.ts +0 -0
- /package/dist/types/src/tokens/{noCase.d.ts → utils/noCase.d.ts} +0 -0
|
@@ -24,7 +24,7 @@ program
|
|
|
24
24
|
.description('run a Designsystemet migration')
|
|
25
25
|
.addArgument(new Argument('[migration]', 'Available migrations').choices(Object.keys(migrations)))
|
|
26
26
|
.option('-l --list', 'List available migrations')
|
|
27
|
-
.option('-g --glob <glob>', 'Glob for files upon which to apply the migration', './**/*.css')
|
|
27
|
+
.option('-g --glob <glob>', 'Glob for files upon which to apply the migration', './**/*.(tsx|css)')
|
|
28
28
|
.action((migrationKey, opts) => {
|
|
29
29
|
const { glob, list } = opts;
|
|
30
30
|
if (list) {
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { Hsluv } from 'hsluv';
|
|
2
|
+
import chroma from 'chroma-js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts a HEX color '#xxxxxx' into a CSS HSL string 'hsl(x,x,x)'
|
|
5
|
+
*
|
|
6
|
+
* @param hex A hex color string
|
|
7
|
+
* @param valuesOnly If true, only the values are returned
|
|
8
|
+
* @returns A CSS HSL string
|
|
9
|
+
*/
|
|
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);
|
|
20
|
+
}
|
|
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
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
const d = max - min;
|
|
29
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
30
|
+
switch (max) {
|
|
31
|
+
case r:
|
|
32
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
33
|
+
break;
|
|
34
|
+
case g:
|
|
35
|
+
h = (b - r) / d + 2;
|
|
36
|
+
break;
|
|
37
|
+
case b:
|
|
38
|
+
h = (r - g) / d + 4;
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
h = Math.round(h ? h * 360 : 0);
|
|
43
|
+
s = Math.round(s * 100);
|
|
44
|
+
l = Math.round(l * 100);
|
|
45
|
+
cssString = h + ',' + s + '%,' + l + '%';
|
|
46
|
+
cssString = !valuesOnly ? 'hsl(' + cssString + ')' : cssString;
|
|
47
|
+
return cssString;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Converts a HEX string '#xxxxxx' into an array of HSL values '[h,s,l]'
|
|
51
|
+
*
|
|
52
|
+
* @param H A Hex color string
|
|
53
|
+
* @returns HSL values in an array
|
|
54
|
+
*/
|
|
55
|
+
export const hexToHSL = (H) => {
|
|
56
|
+
// Convert hex to RGB first
|
|
57
|
+
let r = 0, g = 0, b = 0;
|
|
58
|
+
if (H.length == 4) {
|
|
59
|
+
r = parseInt('0x' + H[1] + H[1]);
|
|
60
|
+
g = parseInt('0x' + H[2] + H[2]);
|
|
61
|
+
b = parseInt('0x' + H[3] + H[3]);
|
|
62
|
+
}
|
|
63
|
+
else if (H.length == 7) {
|
|
64
|
+
r = parseInt('0x' + H[1] + H[2]);
|
|
65
|
+
g = parseInt('0x' + H[3] + H[4]);
|
|
66
|
+
b = parseInt('0x' + H[5] + H[6]);
|
|
67
|
+
}
|
|
68
|
+
// Then to HSL
|
|
69
|
+
r /= 255;
|
|
70
|
+
g /= 255;
|
|
71
|
+
b /= 255;
|
|
72
|
+
let h = 0, s = 0, l = 0;
|
|
73
|
+
const cmin = Math.min(r, g, b), cmax = Math.max(r, g, b), delta = cmax - cmin;
|
|
74
|
+
if (delta == 0)
|
|
75
|
+
h = 0;
|
|
76
|
+
else if (cmax == r)
|
|
77
|
+
h = ((g - b) / delta) % 6;
|
|
78
|
+
else if (cmax == g)
|
|
79
|
+
h = (b - r) / delta + 2;
|
|
80
|
+
else
|
|
81
|
+
h = (r - g) / delta + 4;
|
|
82
|
+
h = Math.round(h * 60);
|
|
83
|
+
if (h < 0)
|
|
84
|
+
h += 360;
|
|
85
|
+
l = (cmax + cmin) / 2;
|
|
86
|
+
s = delta == 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
|
|
87
|
+
s = +(s * 100).toFixed(1);
|
|
88
|
+
l = +(l * 100).toFixed(1);
|
|
89
|
+
return [h, s, l];
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Converts a HSL number array '[h,s,l]' into HSL CSS string 'hsl(x,x,x)'
|
|
93
|
+
*
|
|
94
|
+
* @param HSL A HSL number array '[h,s,l]'
|
|
95
|
+
* @returns A hex color string
|
|
96
|
+
*/
|
|
97
|
+
export const hslArrToCss = (HSL) => {
|
|
98
|
+
return 'hsl(' + HSL[0] + ',' + HSL[1] + '%,' + HSL[2] + '%)';
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Converts a HSL CSS string 'hsl(x,x,x)' into an array of HSL values '[h,s,l]'
|
|
102
|
+
*
|
|
103
|
+
* @param h The HSL hue
|
|
104
|
+
* @param s The HSL saturation
|
|
105
|
+
* @param l The HSL lightness
|
|
106
|
+
* @returns HEX color string
|
|
107
|
+
*/
|
|
108
|
+
export const HSLToHex = (h, s, l) => {
|
|
109
|
+
s /= 100;
|
|
110
|
+
l /= 100;
|
|
111
|
+
let r = 0, g = 0, b = 0;
|
|
112
|
+
const c = (1 - Math.abs(2 * l - 1)) * s, x = c * (1 - Math.abs(((h / 60) % 2) - 1)), m = l - c / 2;
|
|
113
|
+
if (0 <= h && h < 60) {
|
|
114
|
+
r = c;
|
|
115
|
+
g = x;
|
|
116
|
+
b = 0;
|
|
117
|
+
}
|
|
118
|
+
else if (60 <= h && h < 120) {
|
|
119
|
+
r = x;
|
|
120
|
+
g = c;
|
|
121
|
+
b = 0;
|
|
122
|
+
}
|
|
123
|
+
else if (120 <= h && h < 180) {
|
|
124
|
+
r = 0;
|
|
125
|
+
g = c;
|
|
126
|
+
b = x;
|
|
127
|
+
}
|
|
128
|
+
else if (180 <= h && h < 240) {
|
|
129
|
+
r = 0;
|
|
130
|
+
g = x;
|
|
131
|
+
b = c;
|
|
132
|
+
}
|
|
133
|
+
else if (240 <= h && h < 300) {
|
|
134
|
+
r = x;
|
|
135
|
+
g = 0;
|
|
136
|
+
b = c;
|
|
137
|
+
}
|
|
138
|
+
else if (300 <= h && h < 360) {
|
|
139
|
+
r = c;
|
|
140
|
+
g = 0;
|
|
141
|
+
b = x;
|
|
142
|
+
}
|
|
143
|
+
// Having obtained RGB, convert channels to hex
|
|
144
|
+
r = parseInt(Math.round((r + m) * 255).toString(16), 16);
|
|
145
|
+
g = parseInt(Math.round((g + m) * 255).toString(16), 16);
|
|
146
|
+
b = parseInt(Math.round((b + m) * 255).toString(16), 16);
|
|
147
|
+
// Prepend 0s, if necessary
|
|
148
|
+
if (r.toString().length == 1)
|
|
149
|
+
r = parseInt('0' + r.toString(), 10);
|
|
150
|
+
if (g.toString().length == 1)
|
|
151
|
+
g = parseInt('0' + g.toString(), 10);
|
|
152
|
+
if (b.toString().length == 1)
|
|
153
|
+
b = parseInt('0' + b.toString(), 10);
|
|
154
|
+
return '#' + r + g + b;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Converts a HEX color '#xxxxxx' into an array of RGB values '[R, G, B]'
|
|
158
|
+
*
|
|
159
|
+
* @param hex A hex color string
|
|
160
|
+
* @returns RGB values in an array
|
|
161
|
+
*/
|
|
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
|
+
? {
|
|
170
|
+
r: parseInt(result[1], 16),
|
|
171
|
+
g: parseInt(result[2], 16),
|
|
172
|
+
b: parseInt(result[3], 16),
|
|
173
|
+
}
|
|
174
|
+
: null;
|
|
175
|
+
};
|
|
176
|
+
/**
|
|
177
|
+
* Get the luminance of an RGB color
|
|
178
|
+
*
|
|
179
|
+
* @param r RGB red value
|
|
180
|
+
* @param G RGB green value
|
|
181
|
+
* @param b RGB blue value
|
|
182
|
+
* @returns
|
|
183
|
+
*/
|
|
184
|
+
export const luminanceFromRgb = (r, g, b) => {
|
|
185
|
+
const a = [Number(r), Number(g), Number(b)].map(function (v) {
|
|
186
|
+
v /= 255;
|
|
187
|
+
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
188
|
+
});
|
|
189
|
+
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Get the luminance of a HEX color
|
|
193
|
+
*
|
|
194
|
+
* @param hex A hex color string
|
|
195
|
+
* @returns
|
|
196
|
+
*/
|
|
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;
|
|
206
|
+
};
|
|
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
|
+
*/
|
|
214
|
+
export const getRatioFromLum = (lum1, lum2) => {
|
|
215
|
+
if (lum1 !== null && lum2 !== null) {
|
|
216
|
+
return (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
return -1;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
/**
|
|
223
|
+
* Get the HSL lightness from a HEX color
|
|
224
|
+
*
|
|
225
|
+
* @param hex A hex color string
|
|
226
|
+
* @returns
|
|
227
|
+
*/
|
|
228
|
+
export const getHslLightessFromHex = (hex) => {
|
|
229
|
+
return chroma(hex).hsl()[2];
|
|
230
|
+
};
|
|
231
|
+
export const getHslSaturationFromHex = (hex) => {
|
|
232
|
+
return chroma(hex).hsl()[1];
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* Get the HSLuv lightness from a HEX color
|
|
236
|
+
*
|
|
237
|
+
* @param hex A hex color string
|
|
238
|
+
* @returns
|
|
239
|
+
*/
|
|
240
|
+
export const getLightnessFromHex = (hex) => {
|
|
241
|
+
const conv = new Hsluv();
|
|
242
|
+
conv.hex = hex;
|
|
243
|
+
conv.hexToHsluv();
|
|
244
|
+
return Number(conv.hsluv_l.toFixed(0));
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Get the contrast ratio between two HEX colors
|
|
248
|
+
*
|
|
249
|
+
* @param {CssColor} color1 The first color
|
|
250
|
+
* @param {CssColor} color2 The second color
|
|
251
|
+
* @returns
|
|
252
|
+
*/
|
|
253
|
+
export const getContrastFromHex = (color1, color2) => {
|
|
254
|
+
const lum1 = luminanceFromHex(color1);
|
|
255
|
+
const lum2 = luminanceFromHex(color2);
|
|
256
|
+
if (lum1 !== null && lum2 !== null) {
|
|
257
|
+
return getRatioFromLum(lum1, lum2);
|
|
258
|
+
}
|
|
259
|
+
return -1;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Get the contrast ratio between two colors at a specific lightness
|
|
263
|
+
*
|
|
264
|
+
* @param lightness The lightness value
|
|
265
|
+
* @param mainColor The main color
|
|
266
|
+
* @param backgroundColor The background color
|
|
267
|
+
* @returns The contrast ratio
|
|
268
|
+
*/
|
|
269
|
+
export const getContrastFromLightness = (lightness, mainColor, backgroundColor) => {
|
|
270
|
+
const conv = new Hsluv();
|
|
271
|
+
conv.hex = mainColor;
|
|
272
|
+
conv.hexToHsluv();
|
|
273
|
+
conv.hsluv_l = lightness;
|
|
274
|
+
conv.hsluvToHex();
|
|
275
|
+
const lightMainColor = conv.hex;
|
|
276
|
+
const lum1 = luminanceFromHex(lightMainColor);
|
|
277
|
+
const lum2 = luminanceFromHex(backgroundColor);
|
|
278
|
+
const ratio = getRatioFromLum(lum1 ?? 0, lum2 ?? 0);
|
|
279
|
+
return ratio;
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* Maps the numbers from [start1 - end1] to the range [start2 - end2], maintaining the proportionality between the numbers in the ranges using lineaer interpolation.
|
|
283
|
+
*/
|
|
284
|
+
// const mapRange = (value: number, start1: number, end1: number, start2: number, end2: number) => {
|
|
285
|
+
// return start2 + ((value - start1) * (end2 - start2)) / (end1 - start1);
|
|
286
|
+
// };
|
|
287
|
+
/**
|
|
288
|
+
* Check if two colors have enough contrast to be used together
|
|
289
|
+
*
|
|
290
|
+
* @param {CssColor} color1 The first color
|
|
291
|
+
* @param {CssColor} color2 The second color
|
|
292
|
+
* @returns {boolean} If the colors have enough contrast
|
|
293
|
+
*/
|
|
294
|
+
export const areColorsContrasting = (color1, color2, type = 'text') => {
|
|
295
|
+
const contrast = getContrastFromHex(color1, color2);
|
|
296
|
+
if (contrast !== null) {
|
|
297
|
+
if (type === 'text') {
|
|
298
|
+
return contrast >= 4.5;
|
|
299
|
+
}
|
|
300
|
+
else {
|
|
301
|
+
return contrast >= 3;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return false;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Check if aa string value is a HEX color
|
|
308
|
+
*
|
|
309
|
+
* @param {string} hex The string to check
|
|
310
|
+
* @returns {boolean} If the string is a HEX color
|
|
311
|
+
*/
|
|
312
|
+
export const isHexColor = (hex) => {
|
|
313
|
+
return typeof hex === 'string' && hex.length === 6 && !isNaN(Number('0x' + hex));
|
|
314
|
+
};
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import { BackgroundColor, Color, Theme } from '@adobe/leonardo-contrast-colors';
|
|
2
|
+
import { Hsluv } from 'hsluv';
|
|
3
|
+
import { getContrastFromHex, getContrastFromLightness, getLightnessFromHex } from './colorUtils';
|
|
4
|
+
const blueBaseColor = '#0A71C0';
|
|
5
|
+
const greenBaseColor = '#078D19';
|
|
6
|
+
const orangeBaseColor = '#CA5C21';
|
|
7
|
+
const purpleBaseColor = '#663299';
|
|
8
|
+
const redBaseColor = '#C01B1B';
|
|
9
|
+
const yellowBaseColor = '#EABF28';
|
|
10
|
+
/**
|
|
11
|
+
* Generates a Leonardo theme color that is used to create a Leonardo Theme
|
|
12
|
+
*
|
|
13
|
+
* @param color CssColor
|
|
14
|
+
* @param mode Light, Dark or Contrastmode
|
|
15
|
+
* @param contrastMode Contrast mode
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
const generateThemeColor = (color, mode, contrastMode = 'aa') => {
|
|
19
|
+
const leoBackgroundColor = new BackgroundColor({
|
|
20
|
+
name: 'backgroundColor',
|
|
21
|
+
colorKeys: ['#ffffff'],
|
|
22
|
+
ratios: [1],
|
|
23
|
+
});
|
|
24
|
+
let colorLightness = getLightnessFromHex(color);
|
|
25
|
+
if (mode === 'dark' || mode === 'contrast') {
|
|
26
|
+
color = getBaseColor(color);
|
|
27
|
+
colorLightness = colorLightness <= 30 ? 70 : 100 - colorLightness;
|
|
28
|
+
}
|
|
29
|
+
const multiplier = colorLightness <= 30 ? -8 : 8;
|
|
30
|
+
const baseDefaultContrast = getContrastFromLightness(colorLightness, color, leoBackgroundColor.colorKeys[0]);
|
|
31
|
+
const baseHoverContrast = getContrastFromLightness(colorLightness - multiplier, color, leoBackgroundColor.colorKeys[0]);
|
|
32
|
+
const baseActiveContrast = getContrastFromLightness(colorLightness - multiplier * 2, color, leoBackgroundColor.colorKeys[0]);
|
|
33
|
+
const textSubLightLightness = contrastMode === 'aa' ? 42 : 30;
|
|
34
|
+
const textDefLightLightness = contrastMode === 'aa' ? 20 : 17;
|
|
35
|
+
const textSubDarkLightness = contrastMode === 'aa' ? 65 : 78;
|
|
36
|
+
const textDefDarkLightness = contrastMode === 'aa' ? 89 : 93;
|
|
37
|
+
let lightnessScale = [];
|
|
38
|
+
if (mode === 'light') {
|
|
39
|
+
lightnessScale = [100, 96, 90, 84, 78, 76, 54, 33, textSubLightLightness, textDefLightLightness];
|
|
40
|
+
}
|
|
41
|
+
else if (mode === 'dark') {
|
|
42
|
+
lightnessScale = [10, 14, 20, 26, 32, 35, 47, 77, textSubDarkLightness, textDefDarkLightness];
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
lightnessScale = [1, 6, 14, 20, 26, 58, 70, 82, 80, 95];
|
|
46
|
+
}
|
|
47
|
+
const getColorContrasts = (color, lightnessScale, backgroundColor) => {
|
|
48
|
+
return lightnessScale.map((lightness) => getContrastFromLightness(lightness, color, backgroundColor));
|
|
49
|
+
};
|
|
50
|
+
return new Color({
|
|
51
|
+
name: 'color',
|
|
52
|
+
colorKeys: [color],
|
|
53
|
+
ratios: [
|
|
54
|
+
...getColorContrasts(color, lightnessScale.slice(0, 8), leoBackgroundColor.colorKeys[0]),
|
|
55
|
+
baseDefaultContrast,
|
|
56
|
+
baseHoverContrast,
|
|
57
|
+
baseActiveContrast,
|
|
58
|
+
...getColorContrasts(color, lightnessScale.slice(8), leoBackgroundColor.colorKeys[0]),
|
|
59
|
+
],
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
*
|
|
64
|
+
* Generates a color scale based on a base color and a mode.
|
|
65
|
+
*
|
|
66
|
+
* @param color The base color that is used to generate the color scale
|
|
67
|
+
* @param mode The mode of the theme
|
|
68
|
+
*/
|
|
69
|
+
export const generateScaleForColor = (color, mode, contrastMode = 'aa') => {
|
|
70
|
+
const themeColor = generateThemeColor(color, mode, contrastMode);
|
|
71
|
+
const leoBackgroundColor = new BackgroundColor({
|
|
72
|
+
name: 'backgroundColor',
|
|
73
|
+
colorKeys: ['#ffffff'],
|
|
74
|
+
ratios: [1],
|
|
75
|
+
});
|
|
76
|
+
const theme = new Theme({
|
|
77
|
+
colors: [themeColor],
|
|
78
|
+
backgroundColor: leoBackgroundColor,
|
|
79
|
+
lightness: 100,
|
|
80
|
+
});
|
|
81
|
+
const outputArray = [];
|
|
82
|
+
for (let i = 0; i < theme.contrastColorValues.length; i++) {
|
|
83
|
+
outputArray.push({
|
|
84
|
+
hex: theme.contrastColorValues[i],
|
|
85
|
+
number: (i + 1),
|
|
86
|
+
name: getColorNameFromNumber((i + 1)),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
outputArray.push({
|
|
90
|
+
hex: calculateContrastOneColor(theme.contrastColorValues[8]),
|
|
91
|
+
number: 14,
|
|
92
|
+
name: getColorNameFromNumber(14),
|
|
93
|
+
});
|
|
94
|
+
outputArray.push({
|
|
95
|
+
hex: calculateContrastTwoColor(theme.contrastColorValues[8]),
|
|
96
|
+
number: 15,
|
|
97
|
+
name: getColorNameFromNumber(15),
|
|
98
|
+
});
|
|
99
|
+
if (mode === 'light') {
|
|
100
|
+
outputArray[8].hex = color;
|
|
101
|
+
}
|
|
102
|
+
return outputArray;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
*
|
|
106
|
+
* Generates a color theme based on a base color. Light, Dark and Contrast scales are includes.
|
|
107
|
+
*
|
|
108
|
+
* @param color The base color that is used to generate the color theme
|
|
109
|
+
*/
|
|
110
|
+
export const generateThemeForColor = (color, contrastMode = 'aa') => {
|
|
111
|
+
const lightScale = generateScaleForColor(color, 'light', contrastMode);
|
|
112
|
+
const darkScale = generateScaleForColor(color, 'dark', contrastMode);
|
|
113
|
+
const contrastScale = generateScaleForColor(color, 'contrast', contrastMode);
|
|
114
|
+
return {
|
|
115
|
+
light: lightScale,
|
|
116
|
+
dark: darkScale,
|
|
117
|
+
contrast: contrastScale,
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
export const generateGlobalColors = ({ contrastMode = 'aa' }) => {
|
|
121
|
+
const blueTheme = generateThemeForColor(blueBaseColor, contrastMode);
|
|
122
|
+
const greenTheme = generateThemeForColor(greenBaseColor, contrastMode);
|
|
123
|
+
const orangeTheme = generateThemeForColor(orangeBaseColor, contrastMode);
|
|
124
|
+
const purpleTheme = generateThemeForColor(purpleBaseColor, contrastMode);
|
|
125
|
+
const redTheme = generateThemeForColor(redBaseColor, contrastMode);
|
|
126
|
+
const yellowTheme = generateThemeForColor(yellowBaseColor, contrastMode);
|
|
127
|
+
return {
|
|
128
|
+
blue: blueTheme,
|
|
129
|
+
green: greenTheme,
|
|
130
|
+
orange: orangeTheme,
|
|
131
|
+
purple: purpleTheme,
|
|
132
|
+
red: redTheme,
|
|
133
|
+
yellow: yellowTheme,
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* This function generates a complete theme for a set of colors.
|
|
138
|
+
*
|
|
139
|
+
* @param colors Which colors to generate the theme for
|
|
140
|
+
* @param contrastMode The contrast mode to use
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
export const generateColorTheme = ({ colors, contrastMode = 'aa' }) => {
|
|
144
|
+
const accentTheme = generateThemeForColor(colors.accent, contrastMode);
|
|
145
|
+
const neutralTheme = generateThemeForColor(colors.neutral, contrastMode);
|
|
146
|
+
const brand1Theme = generateThemeForColor(colors.brand1, contrastMode);
|
|
147
|
+
const brand2Theme = generateThemeForColor(colors.brand2, contrastMode);
|
|
148
|
+
const brand3Theme = generateThemeForColor(colors.brand3, contrastMode);
|
|
149
|
+
return {
|
|
150
|
+
accent: accentTheme,
|
|
151
|
+
neutral: neutralTheme,
|
|
152
|
+
brand1: brand1Theme,
|
|
153
|
+
brand2: brand2Theme,
|
|
154
|
+
brand3: brand3Theme,
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
*
|
|
159
|
+
* This function calculates a color that can be used as a strong contrast color to a base color.
|
|
160
|
+
*
|
|
161
|
+
* @param baseColor The base color
|
|
162
|
+
*/
|
|
163
|
+
export const calculateContrastOneColor = (baseColor) => {
|
|
164
|
+
const contrastWhite = getContrastFromHex(baseColor, '#ffffff');
|
|
165
|
+
const contrastBlack = getContrastFromHex(baseColor, '#000000');
|
|
166
|
+
const lightness = contrastWhite >= contrastBlack ? 100 : 0;
|
|
167
|
+
// const color = createColorWithLightness(baseColor, lightness);
|
|
168
|
+
return lightness === 0 ? '#000000' : '#ffffff';
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
*
|
|
172
|
+
* This function calculates a color that can be used as a subtle contrast color to a base color.
|
|
173
|
+
*
|
|
174
|
+
* @param color The base color
|
|
175
|
+
*/
|
|
176
|
+
export const calculateContrastTwoColor = (color) => {
|
|
177
|
+
const contrastWhite = getContrastFromHex(color, '#ffffff');
|
|
178
|
+
const contrastBlack = getContrastFromHex(color, '#000000');
|
|
179
|
+
const lightness = getLightnessFromHex(color);
|
|
180
|
+
const doubleALightnessModifier = lightness <= 40 ? 60 : lightness >= 60 ? 60 : 50;
|
|
181
|
+
let targetLightness = 0;
|
|
182
|
+
const contrastDirection = contrastWhite >= contrastBlack ? 'lighten' : 'darken';
|
|
183
|
+
targetLightness =
|
|
184
|
+
contrastDirection === 'lighten' ? lightness + doubleALightnessModifier : lightness - doubleALightnessModifier;
|
|
185
|
+
return createColorWithLightness(color, targetLightness);
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
*
|
|
189
|
+
* This function checks if white or black text can be used on 2 different colors at 4.5:1 contrast.
|
|
190
|
+
*
|
|
191
|
+
* @param baseDefaultColor Base default color
|
|
192
|
+
* @param baseActiveColor Base active color
|
|
193
|
+
*/
|
|
194
|
+
export const canTextBeUsedOnColors = (baseDefaultColor, baseActiveColor) => {
|
|
195
|
+
const defaultAgainstWhite = getContrastFromHex(baseDefaultColor, '#ffffff');
|
|
196
|
+
const defaultAgainstBlack = getContrastFromHex(baseDefaultColor, '#000000');
|
|
197
|
+
const activeAgainstWhite = getContrastFromHex(baseActiveColor, '#ffffff');
|
|
198
|
+
const activeAgainstBlack = getContrastFromHex(baseActiveColor, '#000000');
|
|
199
|
+
if (defaultAgainstWhite >= 4.5 && activeAgainstWhite >= 4.5) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
else if (defaultAgainstBlack >= 4.5 && activeAgainstBlack >= 4.5) {
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
return false;
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
*
|
|
209
|
+
* This function creates a color with a specific lightness value.
|
|
210
|
+
*
|
|
211
|
+
* @param color The base color
|
|
212
|
+
* @param lightness The lightness value from 0 to 100
|
|
213
|
+
*/
|
|
214
|
+
export const createColorWithLightness = (color, lightness) => {
|
|
215
|
+
const leoBackgroundColor = new BackgroundColor({
|
|
216
|
+
name: 'backgroundColor',
|
|
217
|
+
colorKeys: ['#ffffff'],
|
|
218
|
+
ratios: [1],
|
|
219
|
+
});
|
|
220
|
+
const colors = new Color({
|
|
221
|
+
name: 'color',
|
|
222
|
+
colorKeys: [color],
|
|
223
|
+
ratios: [getContrastFromLightness(lightness, color, '#ffffff')],
|
|
224
|
+
});
|
|
225
|
+
const theme = new Theme({
|
|
226
|
+
colors: [colors],
|
|
227
|
+
backgroundColor: leoBackgroundColor,
|
|
228
|
+
lightness: 100,
|
|
229
|
+
});
|
|
230
|
+
return theme.contrastColorValues[0];
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
*
|
|
234
|
+
* This function returns the color number based on the color name.
|
|
235
|
+
*
|
|
236
|
+
* @param name The name of the color
|
|
237
|
+
*/
|
|
238
|
+
export const getColorNumberFromName = (name) => {
|
|
239
|
+
const colorMap = {
|
|
240
|
+
'Background Subtle': 1,
|
|
241
|
+
'Background Default': 2,
|
|
242
|
+
'Surface Default': 3,
|
|
243
|
+
'Surface Hover': 4,
|
|
244
|
+
'Surface Active': 5,
|
|
245
|
+
'Border Subtle': 6,
|
|
246
|
+
'Border Default': 7,
|
|
247
|
+
'Border Strong': 8,
|
|
248
|
+
'Base Default': 9,
|
|
249
|
+
'Base Hover': 10,
|
|
250
|
+
'Base Active': 11,
|
|
251
|
+
'Text Subtle': 12,
|
|
252
|
+
'Text Default': 13,
|
|
253
|
+
};
|
|
254
|
+
return colorMap[name];
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
*
|
|
258
|
+
* This function returns the color name based on the color number.
|
|
259
|
+
*
|
|
260
|
+
* @param number The number of the color
|
|
261
|
+
*/
|
|
262
|
+
export const getColorNameFromNumber = (number) => {
|
|
263
|
+
const colorMap = {
|
|
264
|
+
1: 'Background Subtle',
|
|
265
|
+
2: 'Background Default',
|
|
266
|
+
3: 'Surface Default',
|
|
267
|
+
4: 'Surface Hover',
|
|
268
|
+
5: 'Surface Active',
|
|
269
|
+
6: 'Border Subtle',
|
|
270
|
+
7: 'Border Default',
|
|
271
|
+
8: 'Border Strong',
|
|
272
|
+
9: 'Base Default',
|
|
273
|
+
10: 'Base Hover',
|
|
274
|
+
11: 'Base Active',
|
|
275
|
+
12: 'Text Subtle',
|
|
276
|
+
13: 'Text Default',
|
|
277
|
+
14: 'Contrast Default',
|
|
278
|
+
15: 'Contrast Subtle',
|
|
279
|
+
};
|
|
280
|
+
return colorMap[number];
|
|
281
|
+
};
|
|
282
|
+
export const getBaseColor = (color) => {
|
|
283
|
+
const conv = new Hsluv();
|
|
284
|
+
conv.hex = color;
|
|
285
|
+
conv.hexToHsluv();
|
|
286
|
+
// conv.hsluv_l = 100 - conv.hsluv_l;
|
|
287
|
+
// conv.hsluv_s = getSaturationForDarkMode(color, conv.hsluv_s);
|
|
288
|
+
conv.hsluvToHex();
|
|
289
|
+
return conv.hex;
|
|
290
|
+
};
|
|
291
|
+
export const getCssVariable = (colorType, colorNumber) => {
|
|
292
|
+
return `--ds-${colorType}-${getColorNameFromNumber(colorNumber).toLowerCase().replace(/\s/g, '-')}`;
|
|
293
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|