-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpostcss-venia-color-scheme.js
67 lines (63 loc) · 2.48 KB
/
postcss-venia-color-scheme.js
1
2
3
4
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
const postcssRequirePaths = require.resolve.paths('postcss');
const postcssInUse = require.resolve('postcss', {
paths: [process.cwd(), ...postcssRequirePaths]
});
const postcss = require(postcssInUse);
const { hslToRgb, rgbToHsl } = require('./color-utils');
const defaultOptions = {
selector: ':root',
mode: 'dark',
overrides: {}
};
module.exports = postcss.plugin(
'postcss-venia-color-scheme',
(options = []) => {
return root => {
for (const request of options) {
const requestOptions = request.options || {};
const config = { ...defaultOptions, ...requestOptions };
let defaultColors;
root.walkRules(rule => {
if (rule.selector === config.selector) {
defaultColors = rule;
return false;
}
});
if (!defaultColors) {
throw root.error(
`Could not find a "${config.selector}" declaration.`
);
}
const altColors = defaultColors.clone();
altColors.walkDecls(decl => {
if (config.overrides.hasOwnProperty(decl.prop)) {
decl.value = config.overrides[decl.prop];
} else if (decl.prop.startsWith('--')) {
const rgb = decl.value.match(
/^([0-9]{1,3}),\s*([0-9]{1,3}),\s([0-9]{1,3})\s*/
);
if (rgb) {
const [, red, green, blue] = rgb;
let [h, s, l] = rgbToHsl(
parseInt(red, 10),
parseInt(green, 10),
parseInt(blue, 10)
);
if (s > 10 && s < 10) {
s = 100 - s;
}
const [r, g, b] = hslToRgb(h, s, 100 - l);
decl.value = `${~~r}, ${~~g}, ${~~b}`;
}
}
});
const altQuery = postcss.atRule({
name: 'media',
params: `(prefers-color-scheme: ${config.mode})`
});
altQuery.append(altColors);
defaultColors.after(altQuery);
}
};
}
);