forked from snejugal/attheme-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattheme.node.js
130 lines (104 loc) · 3 KB
/
attheme.node.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use strict";
class Attheme {
constructor(theme = "", defaultValues) {
if (!theme) {
theme = "";
}
theme = Attheme._parseText(theme);
if (defaultValues) {
let defaults;
if (typeof defaultValues == "object") {
defaults = defaultValues;
} else {
throw new Error("`defaultValues` is not an object");
}
for (let variable in defaults) {
this[variable] = defaults[variable];
}
}
for (let variable in theme) {
this[variable] = theme[variable];
}
if (theme[Attheme.IMAGE_KEY]) {
this[Attheme.IMAGE_KEY] = theme[Attheme.IMAGE_KEY];
}
}
asText(shorthand) {
return Attheme.asText(this, shorthand);
}
static asText(theme, shorthand) {
const b16 = (n) => n.toString(16).padStart(2, "0");
const b10 = (n) => parseInt(n, 16);
let text = "";
for (let variable in theme) {
const red = b16(theme[variable].red);
const green = b16(theme[variable].green);
const blue = b16(theme[variable].blue);
const alpha = b16(theme[variable].alpha);
const hex = `#${(alpha == "ff") ? "" : alpha}${red}${green}${blue}`;
const int = (b10(`${alpha}${red}${green}${blue}`) << 0).toString();
let value;
if (!shorthand || shorthand == "auto") {
if (hex.length >= int.length) {
value = int;
} else {
value = hex;
}
} else if (shorthand == "hex") {
value = hex;
} else if (shorthand == "int") {
value = int;
} else {
throw new Error("The `shorthand` option value is invalid");
}
text += `${variable}=${value}\n`;
}
if (theme[Attheme.IMAGE_KEY]) {
text += `WPS\n${theme[Attheme.IMAGE_KEY]}\nWPE\n`;
}
return text;
}
static _parseText(theme = "") {
if (typeof theme != "string") {
throw new Error("Attheme.parseText requires a string");
return;
}
const b16 = (n) => n.toString(16).padStart(2, "0"),
b10 = (n) => parseInt(n, 16);
const lines = theme.split("\n");
let themeObject = {};
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
if (~line.indexOf("//")) {
line = line.split("//")[0];
}
line = line.trim();
if (line == "WPS") {
themeObject[Attheme.IMAGE_KEY] = lines.slice(i + 1, -2).join("\n");
break;
}
if (!line || !/=/.test(line)) {
continue;
}
const [variable, value] = line.split("=");
let color = null;
if (!value.startsWith("#")) {
color = b16(parseInt(value) >>> 0).padStart(8, "0");
} else {
color = value.slice(1).padStart(8, "f");
}
color = {
red: b10(color.slice(2, 4)),
green: b10(color.slice(4, 6)),
blue: b10(color.slice(6, 8)),
alpha: b10(color.slice(0, 2))
};
themeObject[variable] = color;
}
return themeObject;
}
static get IMAGE_KEY() {
return Symbol.for("image");
}
};
module.exports = Attheme;