forked from acmcsufoss/acmcsuf.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.js
More file actions
77 lines (66 loc) · 1.63 KB
/
Copy pathcolors.js
File metadata and controls
77 lines (66 loc) · 1.63 KB
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
75
76
77
import { parse } from 'css-tree';
export function parseGlobalCSS(source) {
const ast = parse(source);
return fromAST(ast);
}
function fromAST(ast) {
const colors = [];
for (const rule of ast.stylesheet?.rules ?? []) {
if (!('selectors' in rule)) {
continue;
}
if (!rule.selectors?.find((s) => s.endsWith(':root'))) {
continue;
}
for (const decl of rule?.declarations ?? []) {
const color = fromDecl(decl);
if (color) {
colors.push(color);
}
}
}
return colors.sort((a, b) => a.id.localeCompare(b.id));
}
function fromDecl({ property, value }) {
if (property === undefined || value === undefined) {
return null;
}
switch (true) {
case /^\d{1,3}, \d{1,3}, \d{1,3}$/.test(value): {
return {
id: property,
value: value,
color: `rgb(${value})`,
};
}
case /^#[0-9a-fA-F]{3,6}$/.test(value):
case value.startsWith('rgb') && value.endsWith(')'):
case value.startsWith('hsl') && value.endsWith(')'):
case value.startsWith('linear-gradient') && value.endsWith(')'): {
return {
id: property,
value: value,
};
}
case value.startsWith('var') && value.endsWith(')'): {
const aliasOf = value.slice(4, -1).trim();
if (value.endsWith('-rgb)')) {
const color = value.replace(/-rgb\)$/, ')');
return {
id: property,
value: value,
color: color,
aliasOf: aliasOf,
};
}
return {
id: property,
value: value,
aliasOf: aliasOf,
};
}
default: {
return null;
}
}
}