Skip to content

Commit 4fe73e5

Browse files
authored
Colors - generateColorPalette - avoid reversing on dark mode (#2757)
* Colors - generateColorPalette - add the option to avoid reversing on dark mode * adding tests * fix file
1 parent a60209b commit 4fe73e5

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/style/__tests__/colors.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ describe('style/Colors', () => {
104104
expect(uut.getColorTint('#F1BE0B', 3)).toEqual('#8D7006');
105105
expect(uut.getColorTint('#F1BE0B', 95)).toEqual('#FFFEFA');
106106
});
107+
});
107108

109+
describe('generateColorPalette', () => {
108110
it('should memoize calls for generateColorPalette', () => {
109111
uut.getColorTint('#3F88C5', 20);
110112
uut.getColorTint('#3F88C5', 50);
@@ -126,6 +128,16 @@ describe('style/Colors', () => {
126128
});
127129
});
128130

131+
it('should generateColorPalette with avoidReverseOnDark option false not reverse on light mode', () => {
132+
const palette = uut.generateColorPalette('#3F88C5', {avoidReverseOnDark: false});
133+
expect(palette).toEqual(['#193852', '#255379', '#316EA1', '#3F88C5', '#66A0D1', '#8DB9DD', '#B5D1E9', '#DCE9F4']);
134+
});
135+
136+
it('should generateColorPalette with avoidReverseOnDark option true not reverse on light mode', () => {
137+
const palette = uut.generateColorPalette('#3F88C5', {avoidReverseOnDark: true});
138+
expect(palette).toEqual(['#193852', '#255379', '#316EA1', '#3F88C5', '#66A0D1', '#8DB9DD', '#B5D1E9', '#DCE9F4']);
139+
});
140+
129141
describe('generateDesignTokens(...)', () => {
130142
it('should generate design tokens from dark color for light theme', () => {
131143
const primaryColor = '#860D86';

src/style/colors.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ export type GeneratePaletteOptions = {
2222
adjustSaturation?: boolean;
2323
/** Whether to add two extra dark colors usually used for dark mode (generating a palette of 10 instead of 8 colors) */
2424
addDarkestTints?: boolean;
25+
/** Whether to reverse the color palette to generate dark mode palette (pass 'true' to generate the same palette for both light and dark modes) */
26+
avoidReverseOnDark?: boolean;
2527
}
2628
export class Colors {
2729
[key: string]: any;
@@ -173,6 +175,8 @@ export class Colors {
173175
return validColors ? undefined : results[0];
174176
}
175177

178+
shouldReverseOnDark = (avoidReverseOnDark?: boolean) => !avoidReverseOnDark && this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark';
179+
176180
getColorTint(colorValue: string | OpaqueColorValue, tintKey: string | number, options: GetColorTintOptions = {}) {
177181
if (_.isUndefined(tintKey) || isNaN(tintKey as number) || _.isUndefined(colorValue)) {
178182
// console.error('"Colors.getColorTint" must accept a color and tintKey params');
@@ -190,10 +194,8 @@ export class Colors {
190194
if (colorKey) {
191195
const colorKeys = [1, 5, 10, 20, 30, 40, 50, 60, 70, 80];
192196
const keyIndex = _.indexOf(colorKeys, Number(tintKey));
193-
const shouldReverseOnDark =
194-
!options?.avoidReverseOnDark && this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark';
195-
const key = shouldReverseOnDark ? colorKeys[colorKeys.length - 1 - keyIndex] : tintKey;
196-
197+
const key =
198+
this.shouldReverseOnDark(options?.avoidReverseOnDark) ? colorKeys[colorKeys.length - 1 - keyIndex] : tintKey;
197199
const requiredColorKey = `${colorKey.slice(0, -2)}${key}`;
198200
const requiredColorKey1 = `${colorKey.slice(0, -1)}${key}`;
199201
const requiredColor = this[requiredColorKey] || this[requiredColorKey1];
@@ -215,17 +217,14 @@ export class Colors {
215217
return colorsPalette[tintLevel - 1];
216218
}
217219

218-
private generatePalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
219-
const defaultOptions = {adjustLightness: true, adjustSaturation: true, addDarkestTints: false};
220-
const _options = {...defaultOptions, ...options};
221-
220+
private generatePalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
222221
const hsl = Color(color).hsl();
223222
const lightness = Math.round(hsl.color[2]);
224-
const lightColorsThreshold = _options.adjustLightness && this.shouldGenerateDarkerPalette(color) ? 5 : 0;
223+
const lightColorsThreshold = options?.adjustLightness && this.shouldGenerateDarkerPalette(color) ? 5 : 0;
225224
const ls = [hsl.color[2]];
226225

227226
let l = lightness - 10;
228-
const lightnessLevel = _options.addDarkestTints ? 0 : 20;
227+
const lightnessLevel = options?.addDarkestTints ? 0 : 20;
229228
while (l >= lightnessLevel - lightColorsThreshold) { // darker tints
230229
ls.unshift(l);
231230
l -= 10;
@@ -243,15 +242,18 @@ export class Colors {
243242
tints.push(tint);
244243
});
245244

246-
const size = _options.addDarkestTints ? 10 : 8;
245+
const size = options?.addDarkestTints ? 10 : 8;
247246
const sliced = tints.slice(0, size);
248-
const adjusted = _options.adjustSaturation && adjustSaturation(sliced, color);
247+
const adjusted = options?.adjustSaturation && adjustSaturation(sliced, color);
249248
return adjusted || sliced;
250249
});
251250

251+
defaultOptions = {adjustLightness: true, adjustSaturation: true, addDarkestTints: false, avoidReverseOnDark: false};
252+
252253
generateColorPalette = _.memoize((color: string, options?: GeneratePaletteOptions): string[] => {
253-
const palette = this.generatePalette(color, options);
254-
return this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark' ? _.reverse(palette) : palette;
254+
const _options = {...this.defaultOptions, ...options};
255+
const palette = this.generatePalette(color, _options);
256+
return this.shouldReverseOnDark(_options?.avoidReverseOnDark) ? _.reverse(palette) : palette;
255257
});
256258

257259
private generateDesignTokens(primaryColor: string, dark?: boolean) {

0 commit comments

Comments
 (0)