-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
214 lines (184 loc) · 6.42 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import chroma, { InterpolationMode } from 'chroma-js';
import { DefaultColors } from 'tailwindcss/types/generated/colors.d.js';
import builtInColors from 'tailwindcss/colors.js';
function keys<T extends object>(obj: T): (keyof T)[] {
return Object.keys(obj) as (keyof T)[];
}
function entries<T extends object>(obj: T): [keyof T, T[keyof T]][] {
return Object.entries(obj) as [keyof T, T[keyof T]][];
}
function hasOwn<T extends object>(obj: T, key: keyof T): key is keyof T {
return Object.prototype.hasOwnProperty.call(obj, key);
}
// valid color modes for chroma-js
export const validColorModes = [
'rgb',
'lab',
'lch',
'lrgb',
'hcl',
'num',
'hcg',
'oklch',
'hsi',
'hsl',
'hsv',
'oklab',
] as const;
// types for tailwind-lerp-colors
type Shades = Exclude<Record<string, string> | DefaultColors[keyof DefaultColors], string>;
type Colors = Record<string, Shades | string> | DefaultColors;
type ColorMode = (typeof validColorModes)[number];
type Options = {
includeBase?: boolean;
includeLegacy?: boolean;
lerpEnds?: boolean;
interval?: number;
mode?: ColorMode;
};
type OptionName = keyof Options;
type Option<T extends OptionName> = Options[T];
type SingularOptions = Pick<Options, 'lerpEnds' | 'interval' | 'mode'>;
// default options for tailwind-lerp-colors -> lerpColor
const defaultSingleOptions: Required<SingularOptions> = {
lerpEnds: true,
interval: 25,
mode: 'lrgb',
};
// default options for tailwind-lerp-colors -> lerpColors
const defaultOptions = {
includeBase: true,
includeLegacy: false,
...defaultSingleOptions,
};
const isOptionInvalid = <T extends OptionName>(options: Options, optionName: T, test: (k: Option<T>) => boolean) => {
return options && hasOwn(options, optionName) && !test(options[optionName]);
};
const throwError = (message: string) => {
throw new Error(message);
};
const isValidShade = (shades: Shades | string): shades is Shades => {
if (
// undefined or null
shades == null ||
// check if shades is an object
typeof shades !== 'object' ||
// check if shades is an array
Array.isArray(shades) ||
shades.toString() !== '[object Object]' ||
!keys(shades).some((key) => !isNaN(+key) && +key >= 0 && +key <= 1000)
) {
return false;
}
return true;
};
export const lerpColor = (shades: Shades, options: SingularOptions = {}) => {
// validate lerpEnds
if (isOptionInvalid(options, 'lerpEnds', (v) => typeof v === 'boolean'))
throwError('tailwind-lerp-colors option `lerpEnds` must be a boolean.');
// validate interval
if (isOptionInvalid(options, 'interval', (v) => Number.isInteger(v) && typeof v === 'number' && v > 0))
throwError('tailwind-lerp-colors option `interval` must be a positive integer greater than 0.');
// validate mode
if (isOptionInvalid(options, 'mode', (v) => typeof v === 'string' && validColorModes.includes(v)))
throwError(
`tailwind-lerp-colors option \`mode\` must be one of the following values: ${validColorModes.join(', ')}.`
);
if (!isValidShade(shades))
throwError(
`tailwind-lerp-colors object \`shades\` must be an object with numeric keys.\n\nvalue used: ${JSON.stringify(
shades,
null,
2
)}`
);
const { lerpEnds, interval, mode } = {
...defaultSingleOptions,
...(options ?? {}),
};
const sortByNumericFirstIndex = ([numericKeyA]: [number, string], [numericKeyB]: [number, string]) => {
return numericKeyA - numericKeyB;
};
const namedShades: Record<string, string> = {};
const numericShades = entries(shades)
.flatMap(([key, color]) => {
const numericShade = +key;
if (isNaN(numericShade)) {
namedShades[key] = color;
return [];
}
return [[numericShade, color]] as [[number, string]];
})
.sort(sortByNumericFirstIndex);
if (lerpEnds) {
if (numericShades[0]?.[0] !== 0) numericShades.unshift([0, '#ffffff']);
if (numericShades.slice(-1)[0]?.[0] !== 1000) numericShades.push([1000, '#000000']);
}
const finalShades = [...numericShades];
for (let i = 0; i < numericShades.length - 1; i++) {
const [shade, color] = numericShades[i] ?? [];
const [nextShade, nextColor] = numericShades[i + 1] ?? [];
if (!shade || !color || !nextShade || !nextColor) {
continue;
}
// check to make sure both shades being compared
// are evenly divisible by the set interval
const interpolations = (nextShade - shade) / interval - 1;
if (interpolations <= 0 || !Number.isInteger(interpolations)) continue;
const scale = chroma.scale([color, nextColor]).mode(mode as InterpolationMode);
const getColorAt = (percent: number) => scale(percent).hex();
for (let run = 1; run <= interpolations; run++) {
const percent = run / (interpolations + 1);
finalShades.push([shade + interval * run, getColorAt(percent)]);
}
}
finalShades.sort(sortByNumericFirstIndex);
return {
...Object.fromEntries(finalShades),
...namedShades,
};
};
export const lerpColors = (colorsObj: Colors = {}, options: Options = {}) => {
// validate includeBase
if (isOptionInvalid(options, 'includeBase', (v) => typeof v === 'boolean'))
throwError('tailwind-lerp-colors option `includeBase` must be a boolean.');
// validate includeLegacy
if (isOptionInvalid(options, 'includeLegacy', (v) => typeof v === 'boolean'))
throwError('tailwind-lerp-colors option `includeLegacy` must be a boolean.');
const legacyNames = ['lightBlue', 'warmGray', 'trueGray', 'coolGray', 'blueGray'];
const { includeBase, includeLegacy, lerpEnds, interval, mode } = {
...defaultOptions,
...options,
};
const baseColors: Colors = {};
if (includeBase) {
const builtInColorKeys = keys(builtInColors);
for (const key of builtInColorKeys) {
if (!legacyNames.includes(key) || includeLegacy) {
baseColors[key] = builtInColors[key];
}
}
}
const initialColors = entries({
...baseColors,
...colorsObj,
});
const finalColors: Colors = {};
for (const [name, shades] of initialColors) {
if (!isValidShade(shades)) {
finalColors[name] = shades;
} else {
finalColors[name] = lerpColor(shades, { lerpEnds, interval, mode });
}
}
return finalColors;
};
export type {
Shades as LerpColorsShades,
Colors as LerpColorsColors,
ColorMode as LerpColorsColorMode,
Options as LerpColorsOptions,
OptionName as LerpColorsOptionName,
Option as LerpColorsOption,
SingularOptions as LerpColorsSingularOptions,
};