Skip to content

Commit 3ef9772

Browse files
authored
Colors - generate palette - pass options object (#2734)
* Colors - generate palette - pass options object * darkest tints * fix ts error * pr comments * pr comments 2 * naming fix * renaming option
1 parent 804cc5c commit 3ef9772

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/style/colors.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,14 @@ export type DesignToken = {semantic?: [string]; resource_paths?: [string]; toStr
1515
export type TokensOptions = {primaryColor: string};
1616
export type GetColorTintOptions = {avoidReverseOnDark?: boolean};
1717
export type GetColorByHexOptions = {validColors?: string[]};
18-
18+
export type GeneratePaletteOptions = {
19+
/** Whether to adjust the lightness of very light colors (generating darker palette) */
20+
adjustLightness?: boolean;
21+
/** Whether to adjust the saturation of colors with high lightness and saturation (unifying saturation level throughout palette) */
22+
adjustSaturation?: boolean;
23+
/** Whether to add two extra dark colors usually used for dark mode (generating a palette of 10 instead of 8 colors) */
24+
addDarkestTints?: boolean;
25+
}
1926
export class Colors {
2027
[key: string]: any;
2128
private shouldSupportDarkMode = false;
@@ -208,20 +215,24 @@ export class Colors {
208215
return colorsPalette[tintLevel - 1];
209216
}
210217

211-
private generatePalette = _.memoize((color: string): string[] => {
218+
private generatePalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
219+
const defaultOptions = {adjustLightness: true, adjustSaturation: true, addDarkestTints: false};
220+
const _options = {...defaultOptions, ...options};
221+
212222
const hsl = Color(color).hsl();
213223
const lightness = Math.round(hsl.color[2]);
214-
const lightColorsThreshold = this.shouldGenerateDarkerPalette(color) ? 5 : 0;
215-
224+
const lightColorsThreshold = _options.adjustLightness && this.shouldGenerateDarkerPalette(color) ? 5 : 0;
216225
const ls = [hsl.color[2]];
226+
217227
let l = lightness - 10;
218-
while (l >= 20 - lightColorsThreshold) {
228+
const lightnessLevel = _options.addDarkestTints ? 0 : 20;
229+
while (l >= lightnessLevel - lightColorsThreshold) { // darker tints
219230
ls.unshift(l);
220231
l -= 10;
221232
}
222233

223234
l = lightness + 10;
224-
while (l < 100 - lightColorsThreshold) {
235+
while (l < 100 - lightColorsThreshold) { // lighter tints
225236
ls.push(l);
226237
l += 10;
227238
}
@@ -232,13 +243,14 @@ export class Colors {
232243
tints.push(tint);
233244
});
234245

235-
const sliced = tints.slice(0, 8);
236-
const adjusted = adjustSaturation(sliced, color);
246+
const size = _options.addDarkestTints ? 10 : 8;
247+
const sliced = tints.slice(0, size);
248+
const adjusted = _options.adjustSaturation && adjustSaturation(sliced, color);
237249
return adjusted || sliced;
238250
});
239251

240-
generateColorPalette = _.memoize((color: string): string[] => {
241-
const palette = this.generatePalette(color);
252+
generateColorPalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
253+
const palette = this.generatePalette(color, options);
242254
return this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark' ? _.reverse(palette) : palette;
243255
});
244256

0 commit comments

Comments
 (0)