@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.
- package/dist/bin/designsystemet.js +32 -3439
- package/dist/src/colors/colorUtils.js +301 -343
- package/dist/src/colors/index.js +3 -607
- package/dist/src/colors/themeUtils.js +319 -348
- package/dist/src/colors/types.js +1 -1
- package/dist/src/init/createTokensPackage.js +228 -0
- package/dist/src/init/generateMetadataJson.js +16 -0
- package/dist/src/init/generateThemesJson.js +50 -0
- package/dist/src/init/index.js +7 -0
- package/dist/src/init/nextStepsMarkdown.js +90 -0
- package/dist/src/init/template/default-files/README.md +10 -0
- package/dist/src/init/template/default-files/design-tokens/README.md +3 -0
- package/dist/src/init/template/default-files/design-tokens/primitives/globals.json +197 -0
- package/dist/src/init/template/default-files/design-tokens/primitives/typography/default.json +86 -0
- package/dist/src/init/template/default-files/design-tokens/semantic/color.json +562 -0
- package/dist/src/init/template/default-files/design-tokens/semantic/style.json +543 -0
- package/dist/src/init/template/prettier.config.js +7 -0
- package/dist/src/init/template/template-files/design-tokens/primitives/colors/contrast/global.json +376 -0
- package/dist/src/init/template/template-files/design-tokens/primitives/colors/contrast/theme-template.json +314 -0
- package/dist/src/init/template/template-files/design-tokens/primitives/colors/dark/global.json +376 -0
- package/dist/src/init/template/template-files/design-tokens/primitives/colors/dark/theme-template.json +314 -0
- package/dist/src/init/template/template-files/design-tokens/primitives/colors/light/global.json +376 -0
- package/dist/src/init/template/template-files/design-tokens/primitives/colors/light/theme-template.json +314 -0
- package/dist/src/init/template/template-files/design-tokens/themes/theme-template.json +314 -0
- package/dist/src/init/template/template-files/package.json +23 -0
- package/dist/src/init/utils.js +11 -0
- package/dist/src/migrations/beta-to-v1.js +341 -407
- package/dist/src/migrations/codemods/css/plugins.js +36 -42
- package/dist/src/migrations/codemods/css/run.js +20 -17
- package/dist/src/migrations/codemods/jsx/classname-prefix.js +63 -59
- package/dist/src/migrations/codemods/jsx/run.js +20 -20
- package/dist/src/migrations/index.js +5 -436
- package/dist/src/migrations/react-beta-to-v1.js +4 -27
- package/dist/src/test/a.css +39 -0
- package/dist/src/test/jsx-test.js +12 -0
- package/dist/src/tokens/actions.js +27 -2176
- package/dist/src/tokens/build.js +54 -2974
- package/dist/src/tokens/configs.js +234 -2874
- package/dist/src/tokens/formats/css.js +153 -2632
- package/dist/src/tokens/formats/js-tokens.js +28 -42
- package/dist/src/tokens/transformers.js +44 -82
- package/dist/src/tokens/utils/noCase.js +26 -28
- package/dist/src/tokens/utils/permutateThemes.js +65 -217
- package/dist/src/tokens/utils/utils.js +25 -14
- package/dist/types/bin/designsystemet.d.ts +3 -0
- package/dist/types/bin/designsystemet.d.ts.map +1 -0
- package/dist/types/src/colors/colorUtils.d.ts +126 -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/init/createTokensPackage.d.ts +5 -0
- package/dist/types/src/init/createTokensPackage.d.ts.map +1 -0
- package/dist/types/src/init/generateMetadataJson.d.ts +6 -0
- package/dist/types/src/init/generateMetadataJson.d.ts.map +1 -0
- package/dist/types/src/init/generateThemesJson.d.ts +3 -0
- package/dist/types/src/init/generateThemesJson.d.ts.map +1 -0
- package/dist/types/src/init/index.d.ts +3 -0
- package/dist/types/src/init/index.d.ts.map +1 -0
- package/dist/types/src/init/nextStepsMarkdown.d.ts +3 -0
- package/dist/types/src/init/nextStepsMarkdown.d.ts.map +1 -0
- package/dist/types/src/init/template/prettier.config.d.mts +9 -0
- package/dist/types/src/init/template/prettier.config.d.mts.map +1 -0
- package/dist/types/src/init/utils.d.ts +4 -0
- package/dist/types/src/init/utils.d.ts.map +1 -0
- package/dist/types/src/migrations/beta-to-v1.d.ts +3 -0
- package/dist/types/src/migrations/beta-to-v1.d.ts.map +1 -0
- package/dist/types/src/migrations/codemods/css/plugins.d.ts +6 -0
- package/dist/types/src/migrations/codemods/css/plugins.d.ts.map +1 -0
- package/dist/types/src/migrations/codemods/css/run.d.ts +8 -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 +10 -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 +6 -0
- package/dist/types/src/migrations/index.d.ts.map +1 -0
- 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 +4 -0
- package/dist/types/src/test/jsx-test.d.ts.map +1 -0
- package/dist/types/src/tokens/actions.d.ts +6 -0
- package/dist/types/src/tokens/actions.d.ts.map +1 -0
- package/dist/types/src/tokens/build.d.ts +11 -0
- package/dist/types/src/tokens/build.d.ts.map +1 -0
- package/dist/types/src/tokens/configs.d.ts +31 -0
- package/dist/types/src/tokens/configs.d.ts.map +1 -0
- package/dist/types/src/tokens/formats/css.d.ts +5 -0
- package/dist/types/src/tokens/formats/css.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 +5 -0
- package/dist/types/src/tokens/transformers.d.ts.map +1 -0
- package/dist/types/src/tokens/utils/noCase.d.ts +11 -0
- package/dist/types/src/tokens/utils/noCase.d.ts.map +1 -0
- package/dist/types/src/tokens/utils/permutateThemes.d.ts +17 -0
- package/dist/types/src/tokens/utils/permutateThemes.d.ts.map +1 -0
- package/dist/types/src/tokens/utils/utils.d.ts +24 -0
- package/dist/types/src/tokens/utils/utils.d.ts.map +1 -0
- package/package.json +11 -17
- package/LICENSE +0 -7
- package/dist/bin/designsystemet.js.map +0 -1
- package/dist/src/colors/colorUtils.js.map +0 -1
- package/dist/src/colors/index.js.map +0 -1
- package/dist/src/colors/themeUtils.js.map +0 -1
- package/dist/src/colors/types.js.map +0 -1
- package/dist/src/migrations/beta-to-v1.js.map +0 -1
- package/dist/src/migrations/codemods/css/plugins.js.map +0 -1
- package/dist/src/migrations/codemods/css/run.js.map +0 -1
- package/dist/src/migrations/codemods/jsx/classname-prefix.js.map +0 -1
- package/dist/src/migrations/codemods/jsx/run.js.map +0 -1
- package/dist/src/migrations/index.js.map +0 -1
- package/dist/src/migrations/react-beta-to-v1.js.map +0 -1
- package/dist/src/tokens/actions.js.map +0 -1
- package/dist/src/tokens/build.js.map +0 -1
- package/dist/src/tokens/configs.js.map +0 -1
- package/dist/src/tokens/formats/css.js.map +0 -1
- package/dist/src/tokens/formats/js-tokens.js.map +0 -1
- package/dist/src/tokens/transformers.js.map +0 -1
- package/dist/src/tokens/utils/noCase.js.map +0 -1
- package/dist/src/tokens/utils/permutateThemes.js.map +0 -1
- 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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
r =
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
158
|
-
|
|
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
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
r =
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
|
|
235
|
-
|
|
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
|
-
|
|
238
|
-
|
|
228
|
+
export const getHslSaturationFromHex = (hex)=>{
|
|
229
|
+
return chroma(hex).hsl()[1];
|
|
239
230
|
};
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
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
|
-
|
|
291
|
-
|
|
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
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
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
|