forked from liteflow-labs/starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.ts
46 lines (41 loc) · 1.19 KB
/
colors.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
function interpolateColor(color1: string, color2: string, ratio: number) {
const r1 = parseInt(color1.substring(1, 3), 16)
const g1 = parseInt(color1.substring(3, 5), 16)
const b1 = parseInt(color1.substring(5, 7), 16)
const r2 = parseInt(color2.substring(1, 3), 16)
const g2 = parseInt(color2.substring(3, 5), 16)
const b2 = parseInt(color2.substring(5, 7), 16)
const r = Math.round(r1 + (r2 - r1) * ratio)
.toString(16)
.padStart(2, '0')
const g = Math.round(g1 + (g2 - g1) * ratio)
.toString(16)
.padStart(2, '0')
const b = Math.round(b1 + (b2 - b1) * ratio)
.toString(16)
.padStart(2, '0')
return `#${r}${g}${b}`.toUpperCase()
}
export function generatePalette(color: string, extra = 0.05) {
const palette: { [key: number]: string } = {
...[50, 100, 200, 300, 400].reduce(
(prev, x) => ({
...prev,
[x]: interpolateColor(color, '#FFFFFF', 1 - x / 500 + extra),
}),
{},
),
500: color,
...[600, 700, 800, 900, 950].reduce(
(prev, x) => ({
...prev,
[x]: interpolateColor(color, '#000000', x / 500 - 1 - extra),
}),
{},
),
}
return {
...palette,
black: palette[950],
}
}