-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
188 lines (160 loc) · 4.27 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pSBC from "shade-blend-color";
interface IHexToRgb {
(hex: string): string;
}
interface IGetRgb {
(hexValue: string): number;
}
interface IGetSRgb {
(hexValue: string): number;
}
interface IGetLuminance {
(hexColor: string): number;
}
interface IGetContrast {
(foreground: string, background: string): number;
}
interface IGetTextColor {
(bgColor: string, lightTextColor: string, darkTextColor: string): string;
}
interface IParams {
color: string;
lightTextColor: string;
darkTextColor: string;
}
interface IGenerateVariantColors {
(params: IParams): {
base: { color: string; text: string };
light: { color: string; text: string };
dark: { color: string; text: string };
};
}
interface IColorComplete {
DEFAULT: string;
text: string;
light: { DEFAULT: string; text: string };
dark: { DEFAULT: string; text: string };
}
const getRgb: IGetRgb = (hexValue) =>
parseInt(hexValue, 16) || Number(hexValue);
const getSRgb: IGetSRgb = (hexValue) => {
const rgb = getRgb(hexValue) / 255;
return rgb <= 0.03928 ? rgb / 12.92 : ((rgb + 0.055) / 1.055) ** 2.4;
};
const getLuminance: IGetLuminance = (hexColor) =>
0.2126 * getSRgb(hexColor.substr(1, 2)) +
0.7152 * getSRgb(hexColor.substr(3, 2)) +
0.0722 * getSRgb(hexColor.substr(-2));
const getContrast: IGetContrast = (foreground, background) => {
const L1 = getLuminance(foreground);
const L2 = getLuminance(background);
return (Math.max(L1, L2) + 0.05) / (Math.min(L1, L2) + 0.05);
};
const getTextColor: IGetTextColor = (
bgColor,
lightTextColor,
darkTextColor
) => {
const whiteContrast = getContrast(bgColor, lightTextColor);
const blackContrast = getContrast(bgColor, darkTextColor);
return whiteContrast > blackContrast ? lightTextColor : darkTextColor;
};
const hexToRgb: IHexToRgb = (hex) => {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return `rgb(${r},${g},${b})`;
};
const generateVariantColors: IGenerateVariantColors = ({
color,
lightTextColor,
darkTextColor,
}) => {
const lightColor = pSBC(0.2, color);
const darkColor = pSBC(-0.5, color);
if (!lightColor || !darkColor) {
throw new Error("pSBC failed to generate a color for light or dark.");
}
return {
base: {
color,
text: getTextColor(color, lightTextColor, darkTextColor),
},
light: {
color: lightColor,
text: getTextColor(lightColor, lightTextColor, darkTextColor),
},
dark: {
color: darkColor,
text: getTextColor(darkColor, lightTextColor, darkTextColor),
},
};
};
const generateColor = ({
color,
lightTextColor = "#FFFFFF",
darkTextColor = "#000000",
}: {
color: string;
lightTextColor: string;
darkTextColor: string;
}) => {
if (!/^#([A-Fa-f0-9]{6})$/.test(color)) {
throw new Error(
"Color must be a 6-character hex. Ex: #ffffff, Not: " + String(color)
);
}
if (!/^#([A-Fa-f0-9]{6})$/.test(lightTextColor)) {
throw new Error(
"lightTextColor must be a 6-character hex. Ex: #ffffff, Not: " +
String(lightTextColor)
);
}
if (!/^#([A-Fa-f0-9]{6})$/.test(darkTextColor)) {
throw new Error(
"darkTextColor must be a 6-character hex. Ex: #ffffff, Not: " +
String(darkTextColor)
);
}
const { base, light, dark } = generateVariantColors({
color,
lightTextColor,
darkTextColor,
});
return {
DEFAULT: base.color,
text: base.text,
light: {
DEFAULT: light.color,
text: light.text,
},
dark: {
DEFAULT: dark.color,
text: dark.text,
},
} satisfies RecursiveKeyValuePair
};
interface RecursiveKeyValuePair<K extends keyof any = string, V = string> {
[key: string]: V | RecursiveKeyValuePair<K, V>;
}
const generatePalette = <T extends { [colorName: string]: string }>({
colors,
lightTextColor = "#ffffff",
darkTextColor = "#000000",
}: {
colors: T;
lightTextColor?: string;
darkTextColor?: string;
}) => {
type IPalette = { [colorName in keyof T]: RecursiveKeyValuePair };
const palette: IPalette = {} as any;
for (const key in colors) {
palette[key] = generateColor({
color: colors[key],
darkTextColor,
lightTextColor,
})
}
return palette;
};
export { generatePalette, generateColor, hexToRgb };