generated from next-book/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc-accent.mjs
56 lines (47 loc) · 1.31 KB
/
calc-accent.mjs
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
import fs from "fs";
import { lch, rgb } from "d3-color";
try {
const params = JSON.parse(fs.readFileSync("./epub/params.json", "utf8"));
const { edition } = params?.params?.metadata;
if (edition) {
const color = lch(rgb("#a16e1b")); //50.45, 52.5, 75.43);
const base = hashCode("beletrie") - hashCode(edition);
color.h += base;
fs.writeFileSync(
"./assets/style/_accent.scss",
writeCss(color.formatRgb(), color.brighter(1.2).formatRgb())
);
}
} catch {}
function hashCode(string) {
let hash = 0,
i,
chr;
if (string.length === 0) return hash;
for (i = 0; i < string.length; i++) {
chr = string.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
function writeCss(color, brighter) {
const comment = `// generated by "npm run calc-accent"`;
const code = `body {
--title-color: ${color};
--accent-color: ${color};
@media (prefers-color-scheme: dark) {
--title-color: ${color};
--accent-color: ${brighter};
}
&.nb-color-scheme-light {
--title-color: ${color};
--accent-color: ${color};
}
&.nb-color-scheme-dark {
--title-color: ${color};
--accent-color: ${brighter};
}
}`;
return `${comment}\n\n${code}`;
}