@digdir/designsystemet 1.13.2 → 1.14.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.
- package/dist/bin/config.js +50 -658
- package/dist/bin/designsystemet.d.ts.map +1 -1
- package/dist/bin/designsystemet.js +136 -4623
- package/dist/bin/options.js +22 -14
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/BorderValues.js +9 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/BoxShadowTypes.js +8 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/BoxShadowValues.js +13 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/ColorModifierTypes.js +10 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/ColorSpaceTypes.js +10 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/Properties.js +53 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/StrokeStyleValues.js +14 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/TokenSetStatus.js +9 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/TokenTypes.js +32 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/TypographyValues.js +15 -0
- package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/types/index.js +11 -0
- package/dist/package.js +5 -0
- package/dist/src/colors/colorMetadata.js +255 -261
- package/dist/src/colors/index.js +5 -522
- package/dist/src/colors/theme.js +104 -343
- package/dist/src/colors/types.js +22 -25
- package/dist/src/colors/utils.js +241 -154
- package/dist/src/config.js +103 -444
- package/dist/src/index.js +10 -3588
- package/dist/src/migrations/beta-to-v1.js +339 -537
- package/dist/src/migrations/codemods/css/plugins.js +31 -42
- package/dist/src/migrations/codemods/css/run.js +20 -151
- package/dist/src/migrations/color-rename-next49.js +44 -220
- package/dist/src/migrations/index.js +7 -577
- package/dist/src/scripts/update-preview-tokens.d.ts.map +1 -1
- package/dist/src/tokens/build.js +39 -1816
- package/dist/src/tokens/create/files.d.ts +10 -5
- package/dist/src/tokens/create/files.d.ts.map +1 -1
- package/dist/src/tokens/create/files.js +44 -601
- package/dist/src/tokens/create/generators/$designsystemet.js +8 -97
- package/dist/src/tokens/create/generators/$metadata.d.ts +7 -3
- package/dist/src/tokens/create/generators/$metadata.d.ts.map +1 -1
- package/dist/src/tokens/create/generators/$metadata.js +24 -21
- package/dist/src/tokens/create/generators/$themes.d.ts +2 -4
- package/dist/src/tokens/create/generators/$themes.d.ts.map +1 -1
- package/dist/src/tokens/create/generators/$themes.js +139 -312
- package/dist/src/tokens/create/generators/primitives/color-scheme.js +68 -443
- package/dist/src/tokens/create/generators/primitives/globals.js +141 -147
- package/dist/src/tokens/create/generators/primitives/size.js +146 -156
- package/dist/src/tokens/create/generators/primitives/typography.js +213 -217
- package/dist/src/tokens/create/generators/semantic/color-modes.js +24 -59
- package/dist/src/tokens/create/generators/semantic/color.js +42 -326
- package/dist/src/tokens/create/generators/semantic/style.js +379 -382
- package/dist/src/tokens/create/generators/themes/theme.js +108 -392
- package/dist/src/tokens/create.d.ts +2 -1
- package/dist/src/tokens/create.d.ts.map +1 -1
- package/dist/src/tokens/create.js +50 -1591
- package/dist/src/tokens/format.d.ts.map +1 -1
- package/dist/src/tokens/format.js +35 -3324
- package/dist/src/tokens/generate-config.js +155 -298
- package/dist/src/tokens/index.js +3 -3344
- package/dist/src/tokens/process/configs/color.js +50 -1085
- package/dist/src/tokens/process/configs/semantic.js +45 -1083
- package/dist/src/tokens/process/configs/shared.js +18 -109
- package/dist/src/tokens/process/configs/size-mode.js +27 -1082
- package/dist/src/tokens/process/configs/size.js +32 -1083
- package/dist/src/tokens/process/configs/type-scale.js +49 -1083
- package/dist/src/tokens/process/configs/typography.js +63 -1084
- package/dist/src/tokens/process/configs.js +91 -1224
- package/dist/src/tokens/process/formats/css/color.js +58 -1079
- package/dist/src/tokens/process/formats/css/semantic.js +27 -1081
- package/dist/src/tokens/process/formats/css/size-mode.js +43 -1077
- package/dist/src/tokens/process/formats/css/size.js +86 -1079
- package/dist/src/tokens/process/formats/css/type-scale.js +53 -1083
- package/dist/src/tokens/process/formats/css/typography.js +27 -1081
- package/dist/src/tokens/process/formats/css.js +18 -1081
- package/dist/src/tokens/process/output/declarations.js +18 -1201
- package/dist/src/tokens/process/output/tailwind.js +26 -40
- package/dist/src/tokens/process/output/theme.js +73 -206
- package/dist/src/tokens/process/platform.js +165 -1355
- package/dist/src/tokens/process/transformers.js +49 -89
- package/dist/src/tokens/process/utils/getMultidimensionalThemes.js +100 -1183
- package/dist/src/tokens/process/utils/kebab-case.js +7 -5
- package/dist/src/tokens/types.d.ts +6 -0
- package/dist/src/tokens/types.d.ts.map +1 -1
- package/dist/src/tokens/types.js +6 -7
- package/dist/src/tokens/utils.d.ts +2 -1
- package/dist/src/tokens/utils.d.ts.map +1 -1
- package/dist/src/tokens/utils.js +107 -93
- package/dist/src/types.js +1 -5
- package/dist/src/utils/filesystem.js +112 -124
- package/package.json +12 -15
- package/configs/test-tokens.config.json +0 -82
- package/dist/src/scripts/createJsonSchema.js +0 -409
- package/dist/src/scripts/update-preview-tokens.js +0 -3353
package/dist/src/colors/utils.js
CHANGED
|
@@ -1,157 +1,244 @@
|
|
|
1
|
-
// src/colors/utils.ts
|
|
2
1
|
import chroma from "chroma-js";
|
|
3
2
|
import Colorjs from "colorjs.io";
|
|
4
3
|
import { Hsluv } from "hsluv";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
4
|
+
//#region src/colors/utils.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts a HEX color '#xxxxxx' into a CSS HSL string 'hsl(x,x,x)'
|
|
7
|
+
*
|
|
8
|
+
* @param hex A hex color string
|
|
9
|
+
* @param valuesOnly If true, only the values are returned
|
|
10
|
+
* @returns A CSS HSL string
|
|
11
|
+
*/
|
|
12
|
+
const hexToCssHsl = (hex, valuesOnly = false) => {
|
|
13
|
+
const [h, s, l] = chroma(hex).hsl();
|
|
14
|
+
const cssString = `${Math.round(h)},${Math.round(s * 100)}%,${Math.round(l * 100)}%`;
|
|
15
|
+
return valuesOnly ? cssString : `hsl(${cssString})`;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Converts a HEX string '#xxxxxx' into an array of HSL values '[h,s,l]'
|
|
19
|
+
*
|
|
20
|
+
* @param H A Hex color string
|
|
21
|
+
* @returns HSL values in an array
|
|
22
|
+
*/
|
|
23
|
+
const hexToHSL = (hex) => {
|
|
24
|
+
const [h, s, l] = chroma(hex).hsl();
|
|
25
|
+
return [
|
|
26
|
+
Math.round(h),
|
|
27
|
+
Math.round(s * 100),
|
|
28
|
+
Math.round(l * 100)
|
|
29
|
+
];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Converts a HEX color '#xxxxxx' into an array of HSLuv values '[h,s,l]'
|
|
33
|
+
*
|
|
34
|
+
* @param hex A hex color string
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
const hexToHsluv = (hex) => {
|
|
38
|
+
const conv = new Hsluv();
|
|
39
|
+
conv.hex = hex;
|
|
40
|
+
conv.hexToHsluv();
|
|
41
|
+
return [
|
|
42
|
+
conv.hsluv_h,
|
|
43
|
+
conv.hsluv_s,
|
|
44
|
+
conv.hsluv_l
|
|
45
|
+
];
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Converts a HSL number array '[h,s,l]' into HSL CSS string 'hsl(x,x,x)'
|
|
49
|
+
*
|
|
50
|
+
* @param HSL A HSL number array '[h,s,l]'
|
|
51
|
+
* @returns A hex color string
|
|
52
|
+
*/
|
|
53
|
+
const hslArrToCss = (HSL) => {
|
|
54
|
+
return "hsl(" + HSL[0] + "," + HSL[1] + "%," + HSL[2] + "%)";
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Converts a HSL CSS string 'hsl(x,x,x)' into an array of HSL values '[h,s,l]'
|
|
58
|
+
*
|
|
59
|
+
* @param h The HSL hue
|
|
60
|
+
* @param s The HSL saturation
|
|
61
|
+
* @param l The HSL lightness
|
|
62
|
+
* @returns HEX color string
|
|
63
|
+
*/
|
|
64
|
+
const HSLToHex = (h, s, l) => {
|
|
65
|
+
return chroma.hsl(h, s / 100, l / 100).hex();
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Converts a HEX color '#xxxxxx' into an array of RGB values '[R, G, B]'
|
|
69
|
+
*
|
|
70
|
+
* @param hex A hex color string
|
|
71
|
+
* @param type The type of RGB values to return
|
|
72
|
+
* @returns RGB values in an array
|
|
73
|
+
*/
|
|
74
|
+
const hexToRgb = (hex, type = "255") => {
|
|
75
|
+
const rgb = chroma(hex).rgb();
|
|
76
|
+
return {
|
|
77
|
+
r: type === "255" ? rgb[0] : rgb[0] / 255,
|
|
78
|
+
g: type === "255" ? rgb[1] : rgb[1] / 255,
|
|
79
|
+
b: type === "255" ? rgb[2] : rgb[2] / 255
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Get the contrast ratio between two HEX colors
|
|
84
|
+
*
|
|
85
|
+
* @param color1 The first color to compare
|
|
86
|
+
* @param color2 The second color to compare
|
|
87
|
+
* @returns
|
|
88
|
+
*/
|
|
89
|
+
const getContrastFromHex = (color1, color2) => {
|
|
90
|
+
const lum1 = chroma(color1).luminance();
|
|
91
|
+
const lum2 = chroma(color2).luminance();
|
|
92
|
+
return (Math.max(lum1, lum2) + .05) / (Math.min(lum1, lum2) + .05);
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Get the contrast ratio between two colors at a specific lightness
|
|
96
|
+
*
|
|
97
|
+
* @param lightness The lightness value
|
|
98
|
+
* @param mainColor The main color
|
|
99
|
+
* @param backgroundColor The background color
|
|
100
|
+
* @returns The contrast ratio
|
|
101
|
+
*/
|
|
102
|
+
const getContrastFromLightness = (lightness, mainColor, backgroundColor) => {
|
|
103
|
+
const conv = new Hsluv();
|
|
104
|
+
conv.hex = mainColor;
|
|
105
|
+
conv.hexToHsluv();
|
|
106
|
+
conv.hsluv_l = lightness;
|
|
107
|
+
conv.hsluvToHex();
|
|
108
|
+
const lightMainColor = conv.hex;
|
|
109
|
+
const lum1 = chroma(lightMainColor).luminance();
|
|
110
|
+
const lum2 = chroma(backgroundColor).luminance();
|
|
111
|
+
return (Math.max(lum1, lum2) + .05) / (Math.min(lum1, lum2) + .05);
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Check if two colors have enough contrast to be used together
|
|
115
|
+
*
|
|
116
|
+
* @param color1 The first color
|
|
117
|
+
* @param color2 The second color
|
|
118
|
+
* @returns If the colors have enough contrast
|
|
119
|
+
*/
|
|
120
|
+
const areColorsContrasting = (color1, color2, type = "aa") => {
|
|
121
|
+
const contrast = getContrastFromHex(color1, color2);
|
|
122
|
+
if (contrast !== null) {
|
|
123
|
+
if (type === "aaa") return contrast >= 7;
|
|
124
|
+
if (type === "aa") return contrast >= 4.5;
|
|
125
|
+
return contrast >= 3;
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Check if aa string value is a HEX color
|
|
131
|
+
*
|
|
132
|
+
* @param hex The hex color
|
|
133
|
+
*/
|
|
134
|
+
const isHexColor = (hex) => {
|
|
135
|
+
return typeof hex === "string" && hex.length === 6 && !Number.isNaN(Number("0x" + hex));
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Get the luminance value from a lightness value
|
|
139
|
+
*
|
|
140
|
+
* @param lightness The lightness value
|
|
141
|
+
*/
|
|
142
|
+
const getLuminanceFromLightness = (lightness) => {
|
|
143
|
+
const conv = new Hsluv();
|
|
144
|
+
conv.hsluv_l = lightness;
|
|
145
|
+
conv.hsluvToHex();
|
|
146
|
+
return chroma(conv.hex).luminance();
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Get the relative luminance from any valid css color
|
|
150
|
+
*
|
|
151
|
+
* @param color
|
|
152
|
+
*/
|
|
153
|
+
const getLuminanceFromColor = (color) => {
|
|
154
|
+
return chroma(color).luminance();
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Get the HSLuv lightness from a HEX color
|
|
158
|
+
*
|
|
159
|
+
* @param hex The hex color
|
|
160
|
+
*/
|
|
161
|
+
const getLightnessFromHex = (hex) => {
|
|
162
|
+
const conv = new Hsluv();
|
|
163
|
+
conv.hex = hex;
|
|
164
|
+
conv.hexToHsluv();
|
|
165
|
+
return conv.hsluv_l;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
*
|
|
169
|
+
* This function checks if white or black text can be used on 2 different colors at 4.5:1 contrast.
|
|
170
|
+
*
|
|
171
|
+
* @param baseDefaultColor Base default color
|
|
172
|
+
* @param baseActiveColor Base active color
|
|
173
|
+
*/
|
|
174
|
+
const canTextBeUsedOnColors = (baseDefaultColor, baseActiveColor) => {
|
|
175
|
+
const defaultAgainstWhite = getContrastFromHex(baseDefaultColor, "#ffffff");
|
|
176
|
+
const defaultAgainstBlack = getContrastFromHex(baseDefaultColor, "#000000");
|
|
177
|
+
const activeAgainstWhite = getContrastFromHex(baseActiveColor, "#ffffff");
|
|
178
|
+
const activeAgainstBlack = getContrastFromHex(baseActiveColor, "#000000");
|
|
179
|
+
if (defaultAgainstWhite >= 4.5 && activeAgainstWhite >= 4.5) return true;
|
|
180
|
+
if (defaultAgainstBlack >= 4.5 && activeAgainstBlack >= 4.5) return true;
|
|
181
|
+
return false;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Converts a color to a HEX color
|
|
185
|
+
*
|
|
186
|
+
* @param color
|
|
187
|
+
* @returns
|
|
188
|
+
*/
|
|
189
|
+
const convertToHex = (color) => {
|
|
190
|
+
if (!color) return "#000000";
|
|
191
|
+
if (/^#[0-9A-Fa-f]{6}$/.test(color)) return color;
|
|
192
|
+
return chroma(color).hex();
|
|
193
|
+
};
|
|
194
|
+
const rgbToHex = (rgb) => {
|
|
195
|
+
return `#${[
|
|
196
|
+
rgb.r,
|
|
197
|
+
rgb.g,
|
|
198
|
+
rgb.b
|
|
199
|
+
].map((x) => {
|
|
200
|
+
const hex = Math.round(x * 255).toString(16);
|
|
201
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
202
|
+
}).join("")}`;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Convert a color to a different format
|
|
206
|
+
*
|
|
207
|
+
* @param cssColor Any valid css color
|
|
208
|
+
* @param format Color space/format supported here https://colorjs.io/docs/spaces
|
|
209
|
+
*/
|
|
210
|
+
const convertColor = (cssColor, format) => {
|
|
211
|
+
const color = new Colorjs(cssColor);
|
|
212
|
+
switch (format) {
|
|
213
|
+
case "rgb":
|
|
214
|
+
case "rgba": return color.toString({
|
|
215
|
+
format: {
|
|
216
|
+
name: format,
|
|
217
|
+
coords: [
|
|
218
|
+
"<number>[0, 255]",
|
|
219
|
+
"<number>[0, 255]",
|
|
220
|
+
"<number>[0, 255]"
|
|
221
|
+
]
|
|
222
|
+
},
|
|
223
|
+
precision: 3
|
|
224
|
+
});
|
|
225
|
+
case "hex": return color.toString({
|
|
226
|
+
format,
|
|
227
|
+
precision: 3
|
|
228
|
+
});
|
|
229
|
+
case "hct": return color.to(format).toString({
|
|
230
|
+
format: {
|
|
231
|
+
name: format,
|
|
232
|
+
coords: [
|
|
233
|
+
"<number>",
|
|
234
|
+
"<number>",
|
|
235
|
+
"<number>"
|
|
236
|
+
]
|
|
237
|
+
},
|
|
238
|
+
precision: 3
|
|
239
|
+
});
|
|
240
|
+
default: return color.to(format).toString({ precision: 3 });
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
//#endregion
|
|
244
|
+
export { HSLToHex, areColorsContrasting, canTextBeUsedOnColors, convertColor, convertToHex, getContrastFromHex, getContrastFromLightness, getLightnessFromHex, getLuminanceFromColor, getLuminanceFromLightness, hexToCssHsl, hexToHSL, hexToHsluv, hexToRgb, hslArrToCss, isHexColor, rgbToHex };
|