Skip to content

Infra/ revese color palettes in dark mode #1893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions generatedTypes/src/style/colors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import tinycolor from 'tinycolor2';
import { Schemes, SchemeType } from './scheme';
export declare class Colors {
[key: string]: any;
private shouldSupportDarkMode;
constructor();
/**
* Load custom set of colors
Expand Down Expand Up @@ -46,6 +47,7 @@ export declare class Colors {
getBackgroundKeysPattern(): RegExp;
isEmpty(color: string): boolean;
getColorTint(color: string, tintKey: string | number): any;
getInvertedTintKey(tintKey: string | number): number;
getColorName(color: string): any;
getTintedColorForDynamicHex(color: string, tintKey: string | number): string;
generateColorPalette: ((color: any) => string[]) & _.MemoizedFunction;
Expand Down Expand Up @@ -114,13 +116,12 @@ declare const colorObject: Colors & {
orange40: string;
orange50: string;
orange60: string;
/**
orange70: string;
orange80: string; /**
* Set color scheme for app
* arguments:
* scheme - color scheme e.g light/dark/default
*/
orange70: string;
orange80: string;
red1: string;
red5: string;
red10: string;
Expand All @@ -142,6 +143,14 @@ declare const colorObject: Colors & {
purple70: string;
purple80: string;
violet1: string;
/**
* Add alpha to hex or rgb color
* arguments:
* p1 - hex color / R part of RGB
* p2 - opacity / G part of RGB
* p3 - B part of RGB
* p4 - opacity
*/
violet5: string;
violet10: string;
violet20: string;
Expand Down Expand Up @@ -174,11 +183,7 @@ declare const colorObject: Colors & {
$backgroundSuccessHeavy: string;
$backgroundSuccess: string;
$backgroundWarningHeavy: string;
$backgroundWarning: string; /**
* Load set of schemes for light/dark mode
* arguments:
* schemes - two sets of map of colors e.g {light: {screen: 'white'}, dark: {screen: 'black'}}
*/
$backgroundWarning: string;
$backgroundMajor: string;
$backgroundDangerHeavy: string;
$backgroundDanger: string;
Expand All @@ -189,6 +194,11 @@ declare const colorObject: Colors & {
$textDisabled: string;
$textDefault: string;
$textNeutralHeavy: string;
/**
* Set color scheme for app
* arguments:
* scheme - color scheme e.g light/dark/default
*/
$textNeutral: string;
$textNeutralLight: string;
$textDefaultLight: string;
Expand Down
31 changes: 27 additions & 4 deletions src/style/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Scheme, {Schemes, SchemeType} from './scheme';

export class Colors {
[key: string]: any;
private shouldSupportDarkMode = false;

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

/**
* Support listening to Appearance changes
* and change the design tokens accordingly
* and change the design tokens accordingly
*/
supportDarkMode() {
const designTokensColors = Scheme.getSchemeType() === 'dark' ? designTokensDM : designTokens;
this.shouldSupportDarkMode = true;
Object.assign(this, designTokensColors);
}

/**
* Add alpha to hex or rgb color
* arguments:
Expand Down Expand Up @@ -132,7 +134,9 @@ export class Colors {
const colorKey = _.findKey(this, (_value, key) => this[key] === color);

if (colorKey) {
const requiredColorKey = `${colorKey.slice(0, -2)}${tintKey}`;
const key =
this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark' ? this.getInvertedTintKey(tintKey) : tintKey;
const requiredColorKey = `${colorKey.slice(0, -2)}${key}`;
const requiredColor = this[requiredColorKey];

if (_.isUndefined(requiredColor)) {
Expand All @@ -143,6 +147,24 @@ export class Colors {
return this.getTintedColorForDynamicHex(color, tintKey);
}

getInvertedTintKey(tintKey: string | number) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can I offer a code improvement (:

You can create an array of our keys ([1, 5, 10, 20, 30, ..., 80])
Then use array.indexOf to detect the given key index, revert the index (as you did before) and return the inverted key..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

let invertedTintKey;
switch (tintKey) {
case 1:
case 80:
invertedTintKey = 81 - Number(tintKey);
break;
case 5:
case 70:
invertedTintKey = 75 - Number(tintKey);
break;
default:
invertedTintKey = 90 - Number(tintKey);
break;
}
return invertedTintKey;
}

getColorName(color: string) {
return ColorName.name(color)[1];
}
Expand Down Expand Up @@ -183,7 +205,8 @@ export class Colors {

const sliced = tints.slice(0, 8);
const adjusted = adjustSaturation(sliced, color);
return adjusted || sliced;
const palette = adjusted || sliced;
return this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark' ? _.reverse(palette) : palette;
});

shouldGenerateDarkerPalette(color: string) {
Expand Down