-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathstyleGenerator.ts
More file actions
55 lines (47 loc) · 1.63 KB
/
Copy pathstyleGenerator.ts
File metadata and controls
55 lines (47 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
import { transformObject,deepMerge,createRGBA,parseColorForAliases,darkenRGBA, lightenRGBA, inverseRGBA } from "./utils.js";
import { styles } from "./styles.js";
export class StylesGenerator {
styleConfig: any;
defaultStyles: any;
constructor(styleConfig: any) {
this.styleConfig = styleConfig;
this.defaultStyles = styles();
}
private generatePlainStyles(styleObj:any) {
let plainCustomStyles = {}
if (styleObj) {
Object.keys(styleObj).forEach((k)=>{
plainCustomStyles[k] = transformObject(styleObj[k])
})
}
return plainCustomStyles;
}
private changeAlias(str:any, mergedStyles:any){
const {aliasMatch,opacityMatch,darkenMatch,lightenMatch, inverseMatch} = parseColorForAliases(str);
if (!aliasMatch) {
return str;
} else {
const alias = aliasMatch[1];
let opacity = opacityMatch ? parseFloat(opacityMatch[1]) : 1;
const color = mergedStyles[alias];
if (darkenMatch) {
return darkenRGBA(createRGBA(color, opacity))
}
if (lightenMatch){
return lightenRGBA(createRGBA(color, opacity))
}
if (inverseMatch){
return inverseRGBA(createRGBA(color, opacity))
}
return createRGBA(color, opacity);
}
}
mergeStyles() {
let mergedStyles = deepMerge(this.defaultStyles, this.generatePlainStyles(this.styleConfig));
let colors = mergedStyles.colors;
Object.entries(colors).forEach(([key,value])=>{
colors[key] = this.changeAlias(value,colors)
})
return mergedStyles;
}
}