Skip to content

Override toString on our design token color #1960

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 2 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 24 additions & 15 deletions src/style/colors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'lodash';
//@ts-ignore
import Color from 'color';
import {OpaqueColorValue} from 'react-native';
import tinycolor from 'tinycolor2';
import {colorsPalette, themeColors} from './colorsPalette';
import DesignTokens from './designTokens';
Expand Down Expand Up @@ -92,6 +93,11 @@ export class Colors {
let green;
let blue;

// Handle design token PlatformColor object
if (typeof p1 === 'object') {
p1 = colorStringValue(p1);
}

if (arguments.length === 2 && typeof p1 === 'string') {
hex = p1;
opacity = p2;
Expand Down Expand Up @@ -129,23 +135,19 @@ export class Colors {
}
}

/**
* Return the original color string value by its colorKey based on the current scheme(light/dark)
*/
getColorValue(colorKey: string) {
return Scheme.getScheme(true)[colorKey] ?? this[colorKey];
}

getColorName(color: string) {
getColorName(colorValue: string) {
const color = colorStringValue(colorValue);
return ColorName.name(color)[1];
}

getColorTint(color: string, tintKey: string | number) {
if (_.isUndefined(tintKey) || isNaN(tintKey as number) || _.isUndefined(color)) {
getColorTint(colorValue: string | OpaqueColorValue, tintKey: string | number) {
if (_.isUndefined(tintKey) || isNaN(tintKey as number) || _.isUndefined(colorValue)) {
// console.error('"Colors.getColorTint" must accept a color and tintKey params');
return color;
return colorValue;
}

const color = colorStringValue(colorValue);

if (color === 'transparent') {
return color;
}
Expand All @@ -170,7 +172,7 @@ export class Colors {
return this.getTintedColorForDynamicHex(color, tintKey);
}

getTintedColorForDynamicHex(color: string, tintKey: string | number) {
private getTintedColorForDynamicHex(color: string, tintKey: string | number) {
// Handles dynamic colors (non uilib colors)
let tintLevel = Math.floor(Number(tintKey) / 10);
tintLevel = Math.max(1, tintLevel);
Expand Down Expand Up @@ -210,13 +212,14 @@ export class Colors {
return this.shouldSupportDarkMode && Scheme.getSchemeType() === 'dark' ? _.reverse(palette) : palette;
});

shouldGenerateDarkerPalette(color: string) {
private shouldGenerateDarkerPalette(color: string) {
const hsl = Color(color).hsl();
const hue = hsl.color[0];
return _.inRange(hue, 51, 184);
}

isDark(color: string) {
isDark(colorValue: string | OpaqueColorValue) {
const color = colorStringValue(colorValue);
const lum = tinycolor(color).getLuminance();
return lum < 0.55;
}
Expand All @@ -232,11 +235,17 @@ export class Colors {
isTransparent(color?: string) {
return color && _.toUpper(color) === _.toUpper('transparent');
}
areEqual(colorA: string, colorB: string) {
areEqual(colorAValue: string | OpaqueColorValue, colorBValue: string | OpaqueColorValue) {
const colorA = colorStringValue(colorAValue);
const colorB = colorStringValue(colorBValue);
return _.toLower(colorA) === _.toLower(colorB);
}
}

function colorStringValue(color: string | object) {
return color.toString();
}

function adjustSaturation(colors: string[], color: string) {
let array;
const lightnessLevel = 80;
Expand Down
23 changes: 10 additions & 13 deletions src/style/scheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export type SchemeChangeListener = (schemeType?: 'light' | 'dark') => void;
class Scheme {
private currentScheme: SchemeType = 'default';
private schemes: Schemes = {light: {}, dark: {}};
private schemesJS: Schemes = {light: {}, dark: {}};
private changeListeners: SchemeChangeListener[] = [];
private usePlatformColors = false;

Expand Down Expand Up @@ -64,40 +63,38 @@ class Scheme {
throw new Error(`There is a mismatch in scheme keys: ${missingKeys.join(', ')}`);
}

merge(this.schemesJS, schemes);

const platformColorsSchemes: Schemes = cloneDeep(schemes);

forEach(schemes, (scheme, schemeKey) => {
forEach(scheme, (colorValue, colorKey) => {
// @ts-expect-error
Object.defineProperty(platformColorsSchemes[schemeKey], colorKey, {
get: () => {
let color: any = colorValue;
if (this.usePlatformColors) {
if (Constants.isAndroid) {
// Remove the $ prefix cause it's not allowed in Android and add the @color prefix
return PlatformColor(`@color/${colorKey.replace(/^[$]/, '')}`);
color = PlatformColor(`@color/${colorKey.replace(/^[$]/, '')}`);
} else {
return PlatformColor(colorKey);
color = PlatformColor(colorKey);
}
} else {
return colorValue;
// Get the original hex string value by calling toString()
color.toString = () => schemes[this.getSchemeType()][colorKey];
}
return color;
}
});
});
});

merge(this.schemes, platformColorsSchemes);
}

/**
* Retrieve scheme by current scheme type
*/
getScheme(useJS = false) {
if (useJS) {
return this.schemesJS[this.getSchemeType()];
} else {
return this.schemes[this.getSchemeType()];
}
getScheme() {
return this.schemes[this.getSchemeType()];
}

/**
Expand Down