auto-theme-js 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +0 -0
- package/dist/AutoTheme.d.ts +30 -0
- package/dist/AutoTheme.js +426 -0
- package/package.json +34 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Gustavo Luiz Gregorio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type ColorTypes = "hex" | "hsl" | "rgb" | "oklab" | "oklch";
|
|
2
|
+
type ColorHEX = `#${string}`;
|
|
3
|
+
type ColorRGB = `rgba(${number}, ${number}, ${number}, ${number})`;
|
|
4
|
+
type ColorHSL = `hsla(${number}, ${number}%, ${number}%, ${number})`;
|
|
5
|
+
type ColorOKLAB = `oklab(${number}% ${number} ${number} / ${number})`;
|
|
6
|
+
type ColorOKLCH = `oklch(${number}% ${number} ${number}deg / ${number})`;
|
|
7
|
+
type Color = ColorHEX | ColorRGB | ColorHSL | ColorOKLAB | ColorOKLCH;
|
|
8
|
+
type ThemeProperties = "primary" | "secondary" | "tertiary" | "accent" | "neutral";
|
|
9
|
+
type Shades = "50" | "100" | "200" | "300" | "400" | "500" | "600" | "700" | "800" | "900";
|
|
10
|
+
type ShadeSpaces = Partial<Record<Shades, Shades>>;
|
|
11
|
+
type ShadeProperties = `${ThemeProperties}-${Shades}`;
|
|
12
|
+
type ThemeShades = Partial<Record<ShadeProperties, Color>>;
|
|
13
|
+
type Theme = Record<Shades, ThemeShades> & {
|
|
14
|
+
colorType: ColorTypes;
|
|
15
|
+
baseColor: Color;
|
|
16
|
+
};
|
|
17
|
+
export declare class AutoTheme {
|
|
18
|
+
#private;
|
|
19
|
+
colorType: ColorTypes;
|
|
20
|
+
baseColor: Color;
|
|
21
|
+
primary: ShadeSpaces;
|
|
22
|
+
secondary: ShadeSpaces;
|
|
23
|
+
tertiary: ShadeSpaces;
|
|
24
|
+
accent: ShadeSpaces;
|
|
25
|
+
neutral: ShadeSpaces;
|
|
26
|
+
constructor(color: Color, inputType?: ColorTypes, outputType?: ColorTypes);
|
|
27
|
+
static serialize(autoThemeObject: AutoTheme, escapeChar?: string): string;
|
|
28
|
+
static deserialize(serializedTheme: string, escapeChar?: string): Theme;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
export class AutoTheme {
|
|
2
|
+
colorType;
|
|
3
|
+
baseColor;
|
|
4
|
+
primary = {};
|
|
5
|
+
secondary = {};
|
|
6
|
+
tertiary = {};
|
|
7
|
+
accent = {};
|
|
8
|
+
neutral = {};
|
|
9
|
+
constructor(color, inputType = "hex", outputType = "hex") {
|
|
10
|
+
this.colorType = outputType;
|
|
11
|
+
// Convert baseColor to the selected output type
|
|
12
|
+
const parsedColor = AutoTheme.#parseToOKLCH(color, inputType);
|
|
13
|
+
this.baseColor = AutoTheme.#oklchToColor(parsedColor, outputType);
|
|
14
|
+
this.#addColors(color, inputType, "50", "900");
|
|
15
|
+
}
|
|
16
|
+
#addColors(color, inputType, shadeMin, shadeMax) {
|
|
17
|
+
const keys = [
|
|
18
|
+
"primary",
|
|
19
|
+
"secondary",
|
|
20
|
+
"tertiary",
|
|
21
|
+
"accent",
|
|
22
|
+
"neutral",
|
|
23
|
+
];
|
|
24
|
+
const shades = [
|
|
25
|
+
"50",
|
|
26
|
+
"100",
|
|
27
|
+
"200",
|
|
28
|
+
"300",
|
|
29
|
+
"400",
|
|
30
|
+
"500",
|
|
31
|
+
"600",
|
|
32
|
+
"700",
|
|
33
|
+
"800",
|
|
34
|
+
"900",
|
|
35
|
+
];
|
|
36
|
+
// Parse base color to OKLCH using the input type
|
|
37
|
+
const baseOKLCH = AutoTheme.#parseToOKLCH(color, inputType);
|
|
38
|
+
// Define hue rotations for color harmony
|
|
39
|
+
// Primary: base hue (0°)
|
|
40
|
+
// Secondary: analogous (+30°)
|
|
41
|
+
// Tertiary: analogous (-30°)
|
|
42
|
+
// Accent: complementary (+180°)
|
|
43
|
+
// Neutral: desaturated base
|
|
44
|
+
const hueRotations = {
|
|
45
|
+
primary: 0,
|
|
46
|
+
secondary: 30,
|
|
47
|
+
tertiary: -30,
|
|
48
|
+
accent: 180,
|
|
49
|
+
neutral: 0,
|
|
50
|
+
};
|
|
51
|
+
// Lightness mapping for shades (Tailwind-like distribution)
|
|
52
|
+
// 50 = very light, 500 = base, 900 = very dark
|
|
53
|
+
const lightnessMap = {
|
|
54
|
+
"50": 97,
|
|
55
|
+
"100": 94,
|
|
56
|
+
"200": 86,
|
|
57
|
+
"300": 77,
|
|
58
|
+
"400": 66,
|
|
59
|
+
"500": 55,
|
|
60
|
+
"600": 45,
|
|
61
|
+
"700": 35,
|
|
62
|
+
"800": 25,
|
|
63
|
+
"900": 15,
|
|
64
|
+
};
|
|
65
|
+
// Filter shades within range
|
|
66
|
+
const minIndex = shades.indexOf(shadeMin);
|
|
67
|
+
const maxIndex = shades.indexOf(shadeMax);
|
|
68
|
+
const filteredShades = shades.slice(minIndex, maxIndex + 1);
|
|
69
|
+
for (const key of keys) {
|
|
70
|
+
// Reset the property object
|
|
71
|
+
this[key] = {};
|
|
72
|
+
// Calculate the hue for this theme property
|
|
73
|
+
const hue = (baseOKLCH.h + hueRotations[key] + 360) % 360;
|
|
74
|
+
// For neutral, reduce chroma significantly
|
|
75
|
+
const chroma = key === "neutral" ? baseOKLCH.c * 0.1 : baseOKLCH.c;
|
|
76
|
+
for (const shade of filteredShades) {
|
|
77
|
+
const lightness = lightnessMap[shade];
|
|
78
|
+
// Adjust chroma based on lightness (reduce at extremes for natural look)
|
|
79
|
+
const adjustedChroma = AutoTheme.#adjustChromaForLightness(chroma, lightness);
|
|
80
|
+
const shadeColor = {
|
|
81
|
+
l: lightness,
|
|
82
|
+
c: adjustedChroma,
|
|
83
|
+
h: hue,
|
|
84
|
+
a: baseOKLCH.a,
|
|
85
|
+
};
|
|
86
|
+
const colorValue = AutoTheme.#oklchToColor(shadeColor, this.colorType);
|
|
87
|
+
const shadeKey = `${shade}`;
|
|
88
|
+
AutoTheme.#addColorProperty(this[key], shadeKey, colorValue);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// #region color_conversion
|
|
93
|
+
static #parseToOKLCH(color, type) {
|
|
94
|
+
switch (type) {
|
|
95
|
+
case "hex":
|
|
96
|
+
return AutoTheme.#hexToOKLCH(color);
|
|
97
|
+
case "rgb":
|
|
98
|
+
return AutoTheme.#rgbToOKLCH(color);
|
|
99
|
+
case "hsl":
|
|
100
|
+
return AutoTheme.#hslToOKLCH(color);
|
|
101
|
+
case "oklch":
|
|
102
|
+
return AutoTheme.#parseOKLCH(color);
|
|
103
|
+
case "oklab":
|
|
104
|
+
return AutoTheme.#oklabToOKLCH(color);
|
|
105
|
+
default:
|
|
106
|
+
return AutoTheme.#hexToOKLCH(color);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
static #hexToOKLCH(hex) {
|
|
110
|
+
// Remove # and parse
|
|
111
|
+
const cleanHex = hex.replace("#", "");
|
|
112
|
+
let r, g, b, a = 1;
|
|
113
|
+
if (cleanHex.length === 3) {
|
|
114
|
+
r = parseInt(cleanHex[0] + cleanHex[0], 16) / 255;
|
|
115
|
+
g = parseInt(cleanHex[1] + cleanHex[1], 16) / 255;
|
|
116
|
+
b = parseInt(cleanHex[2] + cleanHex[2], 16) / 255;
|
|
117
|
+
}
|
|
118
|
+
else if (cleanHex.length === 6) {
|
|
119
|
+
r = parseInt(cleanHex.slice(0, 2), 16) / 255;
|
|
120
|
+
g = parseInt(cleanHex.slice(2, 4), 16) / 255;
|
|
121
|
+
b = parseInt(cleanHex.slice(4, 6), 16) / 255;
|
|
122
|
+
}
|
|
123
|
+
else if (cleanHex.length === 8) {
|
|
124
|
+
r = parseInt(cleanHex.slice(0, 2), 16) / 255;
|
|
125
|
+
g = parseInt(cleanHex.slice(2, 4), 16) / 255;
|
|
126
|
+
b = parseInt(cleanHex.slice(4, 6), 16) / 255;
|
|
127
|
+
a = parseInt(cleanHex.slice(6, 8), 16) / 255;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return { l: 50, c: 0, h: 0, a: 1 };
|
|
131
|
+
}
|
|
132
|
+
return AutoTheme.#linearRGBToOKLCH(r, g, b, a);
|
|
133
|
+
}
|
|
134
|
+
static #rgbToOKLCH(rgb) {
|
|
135
|
+
const match = rgb.match(/rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)/);
|
|
136
|
+
if (!match)
|
|
137
|
+
return { l: 50, c: 0, h: 0, a: 1 };
|
|
138
|
+
const r = parseInt(match[1]) / 255;
|
|
139
|
+
const g = parseInt(match[2]) / 255;
|
|
140
|
+
const b = parseInt(match[3]) / 255;
|
|
141
|
+
const a = parseFloat(match[4]);
|
|
142
|
+
return AutoTheme.#linearRGBToOKLCH(r, g, b, a);
|
|
143
|
+
}
|
|
144
|
+
static #hslToOKLCH(hsl) {
|
|
145
|
+
const match = hsl.match(/hsla\((\d+),\s*(\d+)%,\s*(\d+)%,\s*([\d.]+)\)/);
|
|
146
|
+
if (!match)
|
|
147
|
+
return { l: 50, c: 0, h: 0, a: 1 };
|
|
148
|
+
const h = parseInt(match[1]);
|
|
149
|
+
const s = parseInt(match[2]) / 100;
|
|
150
|
+
const l = parseInt(match[3]) / 100;
|
|
151
|
+
const a = parseFloat(match[4]);
|
|
152
|
+
// HSL to RGB
|
|
153
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
154
|
+
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
|
155
|
+
const m = l - c / 2;
|
|
156
|
+
let r = 0, g = 0, b = 0;
|
|
157
|
+
if (h < 60) {
|
|
158
|
+
r = c;
|
|
159
|
+
g = x;
|
|
160
|
+
b = 0;
|
|
161
|
+
}
|
|
162
|
+
else if (h < 120) {
|
|
163
|
+
r = x;
|
|
164
|
+
g = c;
|
|
165
|
+
b = 0;
|
|
166
|
+
}
|
|
167
|
+
else if (h < 180) {
|
|
168
|
+
r = 0;
|
|
169
|
+
g = c;
|
|
170
|
+
b = x;
|
|
171
|
+
}
|
|
172
|
+
else if (h < 240) {
|
|
173
|
+
r = 0;
|
|
174
|
+
g = x;
|
|
175
|
+
b = c;
|
|
176
|
+
}
|
|
177
|
+
else if (h < 300) {
|
|
178
|
+
r = x;
|
|
179
|
+
g = 0;
|
|
180
|
+
b = c;
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
r = c;
|
|
184
|
+
g = 0;
|
|
185
|
+
b = x;
|
|
186
|
+
}
|
|
187
|
+
return AutoTheme.#linearRGBToOKLCH(r + m, g + m, b + m, a);
|
|
188
|
+
}
|
|
189
|
+
static #parseOKLCH(oklch) {
|
|
190
|
+
const match = oklch.match(/oklch\(([\d.]+)%\s+([\d.]+)\s+([\d.]+)deg\s*\/\s*([\d.]+)\)/);
|
|
191
|
+
if (!match)
|
|
192
|
+
return { l: 50, c: 0, h: 0, a: 1 };
|
|
193
|
+
return {
|
|
194
|
+
l: parseFloat(match[1]),
|
|
195
|
+
c: parseFloat(match[2]),
|
|
196
|
+
h: parseFloat(match[3]),
|
|
197
|
+
a: parseFloat(match[4]),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
static #oklabToOKLCH(oklab) {
|
|
201
|
+
const match = oklab.match(/oklab\(([\d.]+)%\s+([-\d.]+)\s+([-\d.]+)\s*\/\s*([\d.]+)\)/);
|
|
202
|
+
if (!match)
|
|
203
|
+
return { l: 50, c: 0, h: 0, a: 1 };
|
|
204
|
+
const l = parseFloat(match[1]);
|
|
205
|
+
const a = parseFloat(match[2]);
|
|
206
|
+
const b = parseFloat(match[3]);
|
|
207
|
+
const alpha = parseFloat(match[4]);
|
|
208
|
+
const c = Math.sqrt(a * a + b * b);
|
|
209
|
+
const h = (Math.atan2(b, a) * 180) / Math.PI;
|
|
210
|
+
return { l, c, h: h < 0 ? h + 360 : h, a: alpha };
|
|
211
|
+
}
|
|
212
|
+
static #linearRGBToOKLCH(r, g, b, a) {
|
|
213
|
+
// sRGB to linear RGB
|
|
214
|
+
const toLinear = (c) => c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
215
|
+
const lr = toLinear(r);
|
|
216
|
+
const lg = toLinear(g);
|
|
217
|
+
const lb = toLinear(b);
|
|
218
|
+
// Linear RGB to OKLab
|
|
219
|
+
const l_ = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb);
|
|
220
|
+
const m_ = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb);
|
|
221
|
+
const s_ = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb);
|
|
222
|
+
const L = 0.2104542553 * l_ + 0.793617785 * m_ - 0.0040720468 * s_;
|
|
223
|
+
const A = 1.9779984951 * l_ - 2.428592205 * m_ + 0.4505937099 * s_;
|
|
224
|
+
const B = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.808675766 * s_;
|
|
225
|
+
// OKLab to OKLCH
|
|
226
|
+
const c = Math.sqrt(A * A + B * B);
|
|
227
|
+
let h = (Math.atan2(B, A) * 180) / Math.PI;
|
|
228
|
+
if (h < 0)
|
|
229
|
+
h += 360;
|
|
230
|
+
return { l: L * 100, c, h, a };
|
|
231
|
+
}
|
|
232
|
+
static #adjustChromaForLightness(baseChroma, lightness) {
|
|
233
|
+
// Reduce chroma at very light and very dark extremes for natural look
|
|
234
|
+
// Maximum chroma at mid-lightness (around 50-60%)
|
|
235
|
+
const factor = 1 - Math.pow(Math.abs(lightness - 55) / 55, 2) * 0.5;
|
|
236
|
+
return baseChroma * Math.max(0.3, factor);
|
|
237
|
+
}
|
|
238
|
+
static #oklchToColor(oklch, outputType) {
|
|
239
|
+
switch (outputType) {
|
|
240
|
+
case "oklch":
|
|
241
|
+
return `oklch(${oklch.l.toFixed(1)}% ${oklch.c.toFixed(3)} ${oklch.h.toFixed(1)}deg / ${oklch.a})`;
|
|
242
|
+
case "hex":
|
|
243
|
+
return AutoTheme.#oklchToHex(oklch);
|
|
244
|
+
case "rgb":
|
|
245
|
+
return AutoTheme.#oklchToRGB(oklch);
|
|
246
|
+
case "hsl":
|
|
247
|
+
return AutoTheme.#oklchToHSL(oklch);
|
|
248
|
+
case "oklab":
|
|
249
|
+
return AutoTheme.#oklchToOKLAB(oklch);
|
|
250
|
+
default:
|
|
251
|
+
return AutoTheme.#oklchToHex(oklch);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
static #oklchToLinearRGB(oklch) {
|
|
255
|
+
const L = oklch.l / 100;
|
|
256
|
+
const hRad = (oklch.h * Math.PI) / 180;
|
|
257
|
+
const A = oklch.c * Math.cos(hRad);
|
|
258
|
+
const B = oklch.c * Math.sin(hRad);
|
|
259
|
+
// OKLab to linear RGB
|
|
260
|
+
const l_ = L + 0.3963377774 * A + 0.2158037573 * B;
|
|
261
|
+
const m_ = L - 0.1055613458 * A - 0.0638541728 * B;
|
|
262
|
+
const s_ = L - 0.0894841775 * A - 1.291485548 * B;
|
|
263
|
+
const l = l_ * l_ * l_;
|
|
264
|
+
const m = m_ * m_ * m_;
|
|
265
|
+
const s = s_ * s_ * s_;
|
|
266
|
+
let r = +4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s;
|
|
267
|
+
let g = -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s;
|
|
268
|
+
let b = -0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s;
|
|
269
|
+
// Linear RGB to sRGB
|
|
270
|
+
const toSRGB = (c) => c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;
|
|
271
|
+
r = Math.max(0, Math.min(1, toSRGB(r)));
|
|
272
|
+
g = Math.max(0, Math.min(1, toSRGB(g)));
|
|
273
|
+
b = Math.max(0, Math.min(1, toSRGB(b)));
|
|
274
|
+
return { r, g, b, a: oklch.a };
|
|
275
|
+
}
|
|
276
|
+
static #oklchToHex(oklch) {
|
|
277
|
+
const { r, g, b, a } = AutoTheme.#oklchToLinearRGB(oklch);
|
|
278
|
+
const toHex = (c) => Math.round(c * 255)
|
|
279
|
+
.toString(16)
|
|
280
|
+
.padStart(2, "0");
|
|
281
|
+
if (a < 1) {
|
|
282
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}${toHex(a)}`;
|
|
283
|
+
}
|
|
284
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
|
|
285
|
+
}
|
|
286
|
+
static #oklchToRGB(oklch) {
|
|
287
|
+
const { r, g, b, a } = AutoTheme.#oklchToLinearRGB(oklch);
|
|
288
|
+
return `rgba(${Math.round(r * 255)}, ${Math.round(g * 255)}, ${Math.round(b * 255)}, ${a})`;
|
|
289
|
+
}
|
|
290
|
+
static #oklchToHSL(oklch) {
|
|
291
|
+
const { r, g, b, a } = AutoTheme.#oklchToLinearRGB(oklch);
|
|
292
|
+
const max = Math.max(r, g, b);
|
|
293
|
+
const min = Math.min(r, g, b);
|
|
294
|
+
const l = (max + min) / 2;
|
|
295
|
+
let h = 0, s = 0;
|
|
296
|
+
if (max !== min) {
|
|
297
|
+
const d = max - min;
|
|
298
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
299
|
+
switch (max) {
|
|
300
|
+
case r:
|
|
301
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
302
|
+
break;
|
|
303
|
+
case g:
|
|
304
|
+
h = ((b - r) / d + 2) / 6;
|
|
305
|
+
break;
|
|
306
|
+
case b:
|
|
307
|
+
h = ((r - g) / d + 4) / 6;
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return `hsla(${Math.round(h * 360)}, ${Math.round(s * 100)}%, ${Math.round(l * 100)}%, ${a})`;
|
|
312
|
+
}
|
|
313
|
+
static #oklchToOKLAB(oklch) {
|
|
314
|
+
const hRad = (oklch.h * Math.PI) / 180;
|
|
315
|
+
const a = oklch.c * Math.cos(hRad);
|
|
316
|
+
const b = oklch.c * Math.sin(hRad);
|
|
317
|
+
return `oklab(${oklch.l.toFixed(1)}% ${a.toFixed(3)} ${b.toFixed(3)} / ${oklch.a})`;
|
|
318
|
+
}
|
|
319
|
+
// #endregion color_conversion
|
|
320
|
+
// ...existing code for serialize, deserialize...
|
|
321
|
+
// #region serialization
|
|
322
|
+
static serialize(autoThemeObject, escapeChar = "|") {
|
|
323
|
+
if (!(autoThemeObject instanceof AutoTheme))
|
|
324
|
+
throw new Error("Object is not a AutoTheme instance");
|
|
325
|
+
let serialized = "";
|
|
326
|
+
// Simple properties
|
|
327
|
+
for (const [key, value] of Object.entries(autoThemeObject)) {
|
|
328
|
+
if (typeof value !== "string" ||
|
|
329
|
+
(typeof value === "string" && value.length <= 1))
|
|
330
|
+
continue;
|
|
331
|
+
serialized += `${key}:${value}${escapeChar}`;
|
|
332
|
+
}
|
|
333
|
+
// Object properties
|
|
334
|
+
for (const [key, value] of Object.entries(autoThemeObject)) {
|
|
335
|
+
if (typeof value !== "object" ||
|
|
336
|
+
(typeof value === "object" &&
|
|
337
|
+
Object.entries(value).length === 0))
|
|
338
|
+
continue;
|
|
339
|
+
let currentPropObject = `${escapeChar}${key}:{`;
|
|
340
|
+
for (const [key2, value2] of Object.entries(value)) {
|
|
341
|
+
currentPropObject += `${key2}:${value2}${escapeChar}`;
|
|
342
|
+
}
|
|
343
|
+
currentPropObject =
|
|
344
|
+
currentPropObject
|
|
345
|
+
.trim()
|
|
346
|
+
.substring(0, currentPropObject.length - 1) + "}";
|
|
347
|
+
serialized += `${currentPropObject}${escapeChar}`;
|
|
348
|
+
}
|
|
349
|
+
if (serialized.charAt(serialized.length - 1) === escapeChar)
|
|
350
|
+
serialized = serialized.substring(0, serialized.length - 1);
|
|
351
|
+
return serialized.trim();
|
|
352
|
+
}
|
|
353
|
+
static deserialize(serializedTheme, escapeChar = "|") {
|
|
354
|
+
if (!serializedTheme)
|
|
355
|
+
throw new Error("Serialized Theme was not passed as a parameter");
|
|
356
|
+
const theme = {};
|
|
357
|
+
const objectsStart = serializedTheme.indexOf(`${escapeChar}${escapeChar}`);
|
|
358
|
+
const themeSimpleString = serializedTheme.substring(0, objectsStart);
|
|
359
|
+
const themeSimpleProps = themeSimpleString.split(escapeChar);
|
|
360
|
+
const themeObjectString = serializedTheme.substring(objectsStart + 2, serializedTheme.length);
|
|
361
|
+
const themeObjectProps = themeObjectString.split(`${escapeChar}${escapeChar}`);
|
|
362
|
+
for (const prop of themeSimpleProps) {
|
|
363
|
+
const [key, value] = prop.split(":");
|
|
364
|
+
AutoTheme.#addColorProperty(theme, key, value);
|
|
365
|
+
}
|
|
366
|
+
for (const prop of themeObjectProps) {
|
|
367
|
+
const separatorIndex = prop.indexOf(":");
|
|
368
|
+
const objKey = prop.substring(0, separatorIndex);
|
|
369
|
+
const objValue = prop.substring(separatorIndex + 2, prop.length - 1);
|
|
370
|
+
// Creates the internal object
|
|
371
|
+
AutoTheme.#addObjectProperty(theme, objKey);
|
|
372
|
+
// Assigns the internal object properties
|
|
373
|
+
const objValueProps = objValue.split(escapeChar);
|
|
374
|
+
for (const objValueProp of objValueProps) {
|
|
375
|
+
const [objValueKey, ObjValueValue] = objValueProp.split(":");
|
|
376
|
+
AutoTheme.#addColorProperty(
|
|
377
|
+
// @ts-ignore
|
|
378
|
+
theme[objKey], objValueKey, ObjValueValue);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return theme;
|
|
382
|
+
}
|
|
383
|
+
// #endregion serialization
|
|
384
|
+
// #region private_methods
|
|
385
|
+
static #addColorGroups(theme) {
|
|
386
|
+
const groups = [
|
|
387
|
+
"analogous",
|
|
388
|
+
"monochromatic",
|
|
389
|
+
"triad",
|
|
390
|
+
"complementary",
|
|
391
|
+
"splitComplementary",
|
|
392
|
+
"square",
|
|
393
|
+
"compound",
|
|
394
|
+
"shades",
|
|
395
|
+
];
|
|
396
|
+
for (const group of groups) {
|
|
397
|
+
Object.defineProperty(theme, group, {
|
|
398
|
+
value: {},
|
|
399
|
+
writable: true,
|
|
400
|
+
enumerable: true,
|
|
401
|
+
configurable: true,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
static #addColorProperty(theme, key, value) {
|
|
406
|
+
Object.defineProperty(theme, key, {
|
|
407
|
+
value: value,
|
|
408
|
+
configurable: true,
|
|
409
|
+
enumerable: true,
|
|
410
|
+
writable: true,
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
static #addObjectProperty(theme, key) {
|
|
414
|
+
Object.defineProperty(theme, key, {
|
|
415
|
+
value: {},
|
|
416
|
+
configurable: true,
|
|
417
|
+
enumerable: true,
|
|
418
|
+
writable: true,
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
static #forPropColorArrays(themeProp, keys, colors) {
|
|
422
|
+
for (let i = 0; i < keys.length; ++i) {
|
|
423
|
+
AutoTheme.#addColorProperty(themeProp, keys[i], colors[i]);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "auto-theme-js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Gustavo L. Gregorio",
|
|
7
|
+
"main": "dist/AutoTheme.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build:lin": "clear; tsc; npx ts-add-js-extension --dir=dist",
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/GustavoLGregorio/auto-theme-js#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/GustavoLGregorio/auto-theme-js/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/GustavoLGregorio/auto-theme-js.git"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/AutoTheme.js",
|
|
24
|
+
"types": "./dist/AutoTheme.d.ts"
|
|
25
|
+
},
|
|
26
|
+
"./types": {
|
|
27
|
+
"types": "./dist/types.d.ts"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"die-statement": "^1.0.3",
|
|
32
|
+
"ts-add-js-extension": "^1.6.6"
|
|
33
|
+
}
|
|
34
|
+
}
|