wip
This commit is contained in:
parent
1fa4d0d3f8
commit
6a82e94c54
15 changed files with 287 additions and 31 deletions
|
|
@ -1,89 +0,0 @@
|
|||
import * as tinycolor from 'tinycolor2';
|
||||
const lightTheme = require('../../../theme/light');
|
||||
const darkTheme = require('../../../theme/dark');
|
||||
|
||||
type Theme = {
|
||||
meta: {
|
||||
id: string;
|
||||
name: string;
|
||||
inherit: string;
|
||||
vars: any;
|
||||
};
|
||||
} & {
|
||||
[key: string]: string;
|
||||
};
|
||||
|
||||
export default function(theme: Theme) {
|
||||
if (theme.meta.inherit) {
|
||||
const inherit = [lightTheme, darkTheme].find(x => x.meta.id == theme.meta.inherit);
|
||||
theme = Object.assign({}, inherit, theme);
|
||||
}
|
||||
|
||||
const props = compile(theme);
|
||||
|
||||
Object.entries(props).forEach(([k, v]) => {
|
||||
if (k == 'meta') return;
|
||||
document.documentElement.style.setProperty(`--${k}`, v.toString());
|
||||
});
|
||||
|
||||
localStorage.setItem('theme', JSON.stringify(props));
|
||||
}
|
||||
|
||||
function compile(theme: Theme): { [key: string]: string } {
|
||||
function getColor(code: string): tinycolor.Instance {
|
||||
// ref
|
||||
if (code[0] == '@') {
|
||||
return getColor(theme[code.substr(1)]);
|
||||
}
|
||||
if (code[0] == '$') {
|
||||
return getColor(theme.meta.vars[code.substr(1)]);
|
||||
}
|
||||
|
||||
// func
|
||||
if (code[0] == ':') {
|
||||
const parts = code.split('<');
|
||||
const func = parts.shift().substr(1);
|
||||
const arg = parseFloat(parts.shift());
|
||||
const color = getColor(parts.join('<'));
|
||||
|
||||
switch (func) {
|
||||
case 'darken': return color.darken(arg);
|
||||
case 'lighten': return color.lighten(arg);
|
||||
case 'alpha': return color.setAlpha(arg);
|
||||
}
|
||||
}
|
||||
|
||||
return tinycolor(code);
|
||||
}
|
||||
|
||||
const props = {};
|
||||
|
||||
Object.entries(theme).forEach(([k, v]) => {
|
||||
if (k == 'meta') return;
|
||||
const c = getColor(v);
|
||||
props[k] = genValue(c);
|
||||
});
|
||||
|
||||
const primary = getColor(props['primary']);
|
||||
|
||||
for (let i = 1; i < 10; i++) {
|
||||
const color = primary.clone().setAlpha(i / 10);
|
||||
props['primaryAlpha0' + i] = genValue(color);
|
||||
}
|
||||
|
||||
for (let i = 1; i < 100; i++) {
|
||||
const color = primary.clone().lighten(i);
|
||||
props['primaryLighten' + i] = genValue(color);
|
||||
}
|
||||
|
||||
for (let i = 1; i < 100; i++) {
|
||||
const color = primary.clone().darken(i);
|
||||
props['primaryDarken' + i] = genValue(color);
|
||||
}
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
function genValue(c: tinycolor.Instance): string {
|
||||
return c.toRgbString();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue