Skip to content

Commit 6183f55

Browse files
authored
Infra/ revese color palettes in dark mode (#1893)
* revese color palettes in dark mode * add getInvertedTintKey method * update inverting logic
1 parent 856bc83 commit 6183f55

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

generatedTypes/src/style/colors.d.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import tinycolor from 'tinycolor2';
33
import { Schemes, SchemeType } from './scheme';
44
export declare class Colors {
55
[key: string]: any;
6+
private shouldSupportDarkMode;
67
constructor();
78
/**
89
* Load custom set of colors
@@ -46,6 +47,7 @@ export declare class Colors {
4647
getBackgroundKeysPattern(): RegExp;
4748
isEmpty(color: string): boolean;
4849
getColorTint(color: string, tintKey: string | number): any;
50+
getInvertedTintKey(tintKey: string | number): number;
4951
getColorName(color: string): any;
5052
getTintedColorForDynamicHex(color: string, tintKey: string | number): string;
5153
generateColorPalette: ((color: any) => string[]) & _.MemoizedFunction;
@@ -114,13 +116,12 @@ declare const colorObject: Colors & {
114116
orange40: string;
115117
orange50: string;
116118
orange60: string;
117-
/**
119+
orange70: string;
120+
orange80: string; /**
118121
* Set color scheme for app
119122
* arguments:
120123
* scheme - color scheme e.g light/dark/default
121124
*/
122-
orange70: string;
123-
orange80: string;
124125
red1: string;
125126
red5: string;
126127
red10: string;
@@ -142,6 +143,14 @@ declare const colorObject: Colors & {
142143
purple70: string;
143144
purple80: string;
144145
violet1: string;
146+
/**
147+
* Add alpha to hex or rgb color
148+
* arguments:
149+
* p1 - hex color / R part of RGB
150+
* p2 - opacity / G part of RGB
151+
* p3 - B part of RGB
152+
* p4 - opacity
153+
*/
145154
violet5: string;
146155
violet10: string;
147156
violet20: string;
@@ -174,11 +183,7 @@ declare const colorObject: Colors & {
174183
$backgroundSuccessHeavy: string;
175184
$backgroundSuccess: string;
176185
$backgroundWarningHeavy: string;
177-
$backgroundWarning: string; /**
178-
* Load set of schemes for light/dark mode
179-
* arguments:
180-
* schemes - two sets of map of colors e.g {light: {screen: 'white'}, dark: {screen: 'black'}}
181-
*/
186+
$backgroundWarning: string;
182187
$backgroundMajor: string;
183188
$backgroundDangerHeavy: string;
184189
$backgroundDanger: string;
@@ -190,6 +195,11 @@ declare const colorObject: Colors & {
190195
$textDisabled: string;
191196
$textDefault: string;
192197
$textNeutralHeavy: string;
198+
/**
199+
* Set color scheme for app
200+
* arguments:
201+
* scheme - color scheme e.g light/dark/default
202+
*/
193203
$textNeutral: string;
194204
$textNeutralLight: string;
195205
$textDefaultLight: string;

src/style/colors.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Scheme, {Schemes, SchemeType} from './scheme';
1111

1212
export class Colors {
1313
[key: string]: any;
14+
private shouldSupportDarkMode = false;
1415

1516
constructor() {
1617
const colors = Object.assign(colorsPalette, designTokens, themeColors);
@@ -58,13 +59,14 @@ export class Colors {
5859

5960
/**
6061
* Support listening to Appearance changes
61-
* and change the design tokens accordingly
62+
* and change the design tokens accordingly
6263
*/
6364
supportDarkMode() {
6465
const designTokensColors = Scheme.getSchemeType() === 'dark' ? designTokensDM : designTokens;
66+
this.shouldSupportDarkMode = true;
6567
Object.assign(this, designTokensColors);
6668
}
67-
69+
6870
/**
6971
* Add alpha to hex or rgb color
7072
* arguments:
@@ -132,7 +134,13 @@ export class Colors {
132134
const colorKey = _.findKey(this, (_value, key) => this[key] === color);
133135

134136
if (colorKey) {
135-
const requiredColorKey = `${colorKey.slice(0, -2)}${tintKey}`;
137+
const colorKeys = [1, 5, 10, 20, 30, 40, 50, 60, 70, 80];
138+
const keyIndex = _.indexOf(colorKeys, Number(tintKey));
139+
const key =
140+
this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark'
141+
? colorKeys[colorKeys.length - 1 - keyIndex]
142+
: tintKey;
143+
const requiredColorKey = `${colorKey.slice(0, -2)}${key}`;
136144
const requiredColor = this[requiredColorKey];
137145

138146
if (_.isUndefined(requiredColor)) {
@@ -183,7 +191,8 @@ export class Colors {
183191

184192
const sliced = tints.slice(0, 8);
185193
const adjusted = adjustSaturation(sliced, color);
186-
return adjusted || sliced;
194+
const palette = adjusted || sliced;
195+
return this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark' ? _.reverse(palette) : palette;
187196
});
188197

189198
shouldGenerateDarkerPalette(color: string) {

0 commit comments

Comments
 (0)