-
Notifications
You must be signed in to change notification settings - Fork 5
/
getcolors.js
72 lines (60 loc) · 1.85 KB
/
getcolors.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
// This tool parses themes created by the [shadcn/ui Themes website](https://ui.shadcn.com/themes)
// and will output the closest [Tailwind CSS colors]
// (https://tailwindcss.com/docs/customizing-colors#default-color-palette) for each CSS variable.
const fs = require("fs");
const twColors = require("tailwindcss/colors");
const hsl2hex = require("hsl-to-hex");
const hex2hsl = require("hex-to-hsl");
// convert tailwind default colors to expanded format
let colors = {};
Object.entries(twColors).forEach(([key, value]) => {
if (typeof value === "string") {
if (!value.startsWith("#")) return;
colors[key] = value;
} else {
Object.entries(value).forEach(([k, v]) => {
colors[`${key}-${k}`] = v;
});
}
});
const nearestColor = require("nearest-color").from(colors);
// read theme
const theme = fs.readFileSync(process.argv[2], "utf8");
const light = {};
const dark = {};
let isLight = true; // default to light theme
// parse theme
theme.split("\n").forEach((l) => {
const line = l.trim();
if (!line.length) return;
if (line.startsWith(".dark")) isLight = false;
if (!line.startsWith("--")) return;
const [key, value] = line.split(":");
if (!key || !value) return;
// make sure value is a color
if (!value.match(/[0-9.]+\s[0-9.]+%\s[0-9.]+%;/)) return;
const color = nearestColor(hsl2hex(...parseHsl(value)));
if (isLight) {
light[key] = color.name;
} else {
dark[key] = color.name;
}
});
dump(":root", light);
dump(".dark", dark);
console.log(JSON.stringify({ light, dark }, null, 2));
function dump(theme, colors) {
console.log(`${theme} \{`);
Object.entries(colors).forEach(([key, value]) => {
console.log(` ${key}: ${value};`);
});
console.log("}\n");
}
function parseHsl(value) {
return value
.trim()
.replace("%", "")
.replace(";", "")
.split(" ")
.map((v) => parseFloat(v));
}