Skip to content

Commit be708d8

Browse files
authored
Fix typing of Colors.getColorTint api (#2924)
1 parent d1bf364 commit be708d8

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/style/colors.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ export type GeneratePaletteOptions = {
2121
adjustLightness?: boolean;
2222
/** Whether to adjust the saturation of colors with high lightness and saturation (unifying saturation level throughout palette) */
2323
adjustSaturation?: boolean;
24-
/** Array of saturation adjustments to apply on the color's tints array (from darkest to lightest).
24+
/** Array of saturation adjustments to apply on the color's tints array (from darkest to lightest).
2525
* The 'adjustSaturation' option must be true */
2626
saturationLevels?: number[];
2727
/** Whether to add two extra dark colors usually used for dark mode (generating a palette of 10 instead of 8 colors) */
2828
addDarkestTints?: boolean; // TODO: rename 'fullPalette'
2929
/** Whether to reverse the color palette to generate dark mode palette (pass 'true' to generate the same palette for both light and dark modes) */
3030
avoidReverseOnDark?: boolean;
31-
}
31+
};
3232
export class Colors {
3333
[key: string]: any;
3434
shouldSupportDarkMode = false;
@@ -180,9 +180,12 @@ export class Colors {
180180
return validColors ? undefined : results[0];
181181
}
182182

183-
shouldReverseOnDark = (avoidReverseOnDark?: boolean) => !avoidReverseOnDark && this.shouldSupportDarkMode && Scheme.isDarkMode();
184-
185-
getColorTint(colorValue: string | OpaqueColorValue, tintKey: string | number, options: GetColorTintOptions = {}) {
183+
shouldReverseOnDark = (avoidReverseOnDark?: boolean) =>
184+
!avoidReverseOnDark && this.shouldSupportDarkMode && Scheme.isDarkMode();
185+
186+
getColorTint(colorValue: string | OpaqueColorValue | undefined,
187+
tintKey: string | number,
188+
options: GetColorTintOptions = {}) {
186189
if (_.isUndefined(tintKey) || isNaN(tintKey as number) || _.isUndefined(colorValue)) {
187190
// console.error('"Colors.getColorTint" must accept a color and tintKey params');
188191
return colorValue;
@@ -199,8 +202,9 @@ export class Colors {
199202
if (colorKey) {
200203
const colorKeys = [1, 5, 10, 20, 30, 40, 50, 60, 70, 80];
201204
const keyIndex = _.indexOf(colorKeys, Number(tintKey));
202-
const key =
203-
this.shouldReverseOnDark(options?.avoidReverseOnDark) ? colorKeys[colorKeys.length - 1 - keyIndex] : tintKey;
205+
const key = this.shouldReverseOnDark(options?.avoidReverseOnDark)
206+
? colorKeys[colorKeys.length - 1 - keyIndex]
207+
: tintKey;
204208
const requiredColorKey = `${colorKey.slice(0, -2)}${key}`;
205209
const requiredColorKey1 = `${colorKey.slice(0, -1)}${key}`;
206210
const requiredColor = this[requiredColorKey] || this[requiredColorKey1];
@@ -222,24 +226,26 @@ export class Colors {
222226
return colorsPalette[tintLevel - 1];
223227
}
224228

225-
private generatePalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
229+
private generatePalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
226230
const hsl = Color(color).hsl();
227231
const colorLightness = hsl.color[2];
228232
const lightness = Math.round(colorLightness);
229233
const isWhite = lightness === 100;
230234
const lightnessLevel = options?.addDarkestTints ? (isWhite ? 5 : 0) : 20;
231235
const lightColorsThreshold = options?.adjustLightness && this.shouldGenerateDarkerPalette(color) ? 5 : 0;
232-
const step = /* options?.addDarkestTints ? 9 : */10;
236+
const step = /* options?.addDarkestTints ? 9 : */ 10;
233237
const ls = [colorLightness];
234-
238+
235239
let l = lightness - step;
236-
while (l >= lightnessLevel - lightColorsThreshold) { // darker tints
240+
while (l >= lightnessLevel - lightColorsThreshold) {
241+
// darker tints
237242
ls.unshift(l);
238243
l -= step;
239244
}
240245

241246
l = lightness + step;
242-
while (l < 100 - lightColorsThreshold) { // lighter tints
247+
while (l < 100 - lightColorsThreshold) {
248+
// lighter tints
243249
ls.push(l);
244250
l += step;
245251
}
@@ -249,12 +255,12 @@ export class Colors {
249255
const tint = generateColorTint(color, e);
250256
tints.push(tint);
251257
});
252-
258+
253259
const size = options?.addDarkestTints ? 10 : 8;
254260
const start = options?.addDarkestTints && colorLightness > 10 ? -size : 0;
255261
const end = options?.addDarkestTints && colorLightness > 10 ? undefined : size;
256262
const sliced = tints.slice(start, end);
257-
263+
258264
const adjusted = options?.adjustSaturation && adjustSaturation(sliced, color, options?.saturationLevels);
259265
return adjusted || sliced;
260266
}, generatePaletteCacheResolver);
@@ -413,7 +419,7 @@ function threeDigitHexToSix(value: string) {
413419

414420
function generatePaletteCacheResolver(color: string, options?: GeneratePaletteOptions) {
415421
return `${color}${options ? '_' + JSON.stringify(options) : ''}`;
416-
}
422+
}
417423

418424
const TypedColors = Colors as ExtendTypeWith<typeof Colors, typeof colorsPalette & typeof DesignTokens>;
419425
const colorObject = new TypedColors();

0 commit comments

Comments
 (0)