-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.ts
136 lines (122 loc) · 3.16 KB
/
generate.ts
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
131
132
133
134
135
136
import {
BackgroundColor,
Color,
ContrastColor,
CssColor,
Theme as LeonardoTheme,
} from "@adobe/leonardo-contrast-colors";
import {
BackgroundColors,
CustomColors,
KeyColors,
Polarity,
Theme,
} from "./palettes.ts";
// @deno-types="npm:@types/chroma-js@2"
import Chroma from "chroma";
import { objectEntries } from "./lib.ts";
import chroma from "chroma";
export const convertPaletteToColors = (
palette: KeyColors<Chroma.Color>,
extraKeys: Record<
keyof CustomColors<Chroma.Color>,
keyof KeyColors<Chroma.Color>
>,
) => {
const asColor = (name: string, color: Chroma.Color): Color => {
return new Color({
name,
// @ts-expect-error this works in chroma-js
colorKeys: [color.hex()],
ratios: [1.2, 2.26, 3.24, 4.57, 6.55, 8.87, 11.84, 15.29],
colorspace: "OKLCH",
smooth: true,
output: "OKLCH",
});
};
const asBackground = (name: string, color: Chroma.Color): BackgroundColor => {
return new BackgroundColor({
name,
// @ts-expect-error this works in chroma-js
colorKeys: [color.hex()],
ratios: [1.2, 2.06, 2.95, 4.27, 6.05, 8.37, 11.34, 14.89],
colorspace: "OKLCH",
smooth: true,
output: "OKLCH",
});
};
const colors = objectEntries(palette).map(([name, oklch]) =>
asColor(name, oklch)
);
for (const [extraKey, value] of objectEntries(extraKeys)) {
colors.push(asColor(extraKey, palette[value]));
}
const backgroundColor = asBackground("base", palette.base);
return {
colors,
backgroundColor,
};
};
export const colorsToTheme = (
{
name,
baseShade,
brightShade,
polarity,
}: { name: string; baseShade: Shade; brightShade: Shade; polarity: Polarity },
{
colors,
backgroundColor,
}: { colors: Color[]; backgroundColor: BackgroundColor },
{
lightness,
contrast,
saturation,
}: { lightness: number; contrast: number; saturation: number },
generateBackground: (
background: Chroma.Color,
) => BackgroundColors<Chroma.Color>,
): Theme => {
const theme = new LeonardoTheme({
colors,
backgroundColor,
lightness,
contrast,
saturation,
output: "LCH",
});
const { background: bgStr } = theme.contrastColorPairs;
const [l, c, h] = bgStr
.replace("lch(", "")
.replace(")", "")
.split(",")
.map((val) => parseFloat(val));
const background = chroma.lch(l, c, h);
const backgrounds = generateBackground(background);
return {
name,
theme,
polarity,
backgrounds: backgrounds,
baseShade,
brightShade,
};
};
export type Shade = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800;
export type ColorShades = Record<Shade, CssColor>;
export type ColorObject = Record<
| keyof CustomColors<Chroma.Color>
| keyof KeyColors<Chroma.Color>,
ColorShades
>;
export const colorListToObj = (colors: ContrastColor[]): ColorObject =>
colors.reduce((acc, { name, values }) => {
acc[name as keyof ColorObject] = values.reduce(
(acc, { name: shadeName, value }) => {
acc[Number(shadeName.replace(name, "")) as Shade] = value;
return acc;
},
{} as ColorShades,
);
return acc;
}, {} as ColorObject);