generated from phillipcurl/nuxt-netlify-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build-theme.js
74 lines (66 loc) · 1.93 KB
/
build-theme.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
68
69
70
71
72
73
74
const Color = require('color');
const cssGen = require('css-generator');
const fs = require('fs');
const themeConfig = require('./assets/content/config/theme.json');
const options = {
indentation: ' ' // 2 spaces
};
const css = cssGen.create(options);
function getContrastingColor(colorObj) {
const constrastingColor = colorObj.isLight()
? Color('#111111')
: Color('white');
return constrastingColor.string();
}
function getValidatedColor({
colorToChange,
colorToValidate = Color('white'),
minimumContrastRatio = 5,
mixingColor,
mixingAmount,
tries = 0,
maxTries = 8
}) {
const newColor = colorToChange.mix(mixingColor, mixingAmount);
if (
newColor.contrast(colorToValidate) < minimumContrastRatio &&
tries < maxTries
) {
return getValidatedColor({
colorToChange: newColor,
mixingColor,
mixingAmount: 0.1,
tries: ++tries
});
}
return newColor;
}
const primaryColor = Color(themeConfig.brand_color);
const primaryColorLight = getValidatedColor({
colorToChange: primaryColor,
mixingColor: Color('white'),
colorToValidate: Color('#111111'),
mixingAmount: 0.5
});
const primaryColorDark = getValidatedColor({
colorToChange: primaryColor,
mixingColor: Color('#111111'),
mixingAmount: 0.2
});
css.addRule(':root', {
'--color-brand': primaryColor.string(),
'--color-brand_contrast': getContrastingColor(primaryColor),
'--color-brand-light': primaryColorLight.string(),
'--color-brand-light_contrast': getContrastingColor(primaryColorLight),
'--color-brand-dark': primaryColorDark.string(),
'--color-brand-dark_contrast': getContrastingColor(primaryColorDark)
});
console.log('primary color: ', primaryColor.string());
console.log('light color: ', primaryColorLight.string());
console.log('dark color: ', primaryColorDark.string());
console.log('CSS: ', css.getOutput());
var stream = fs.createWriteStream('./assets/css/theme.css');
stream.once('open', function(fd) {
stream.write(css.getOutput());
stream.end();
});