Skip to content

Commit 428af31

Browse files
authored
Override toString on our design token color to expose its original hex value (#1960)
1 parent a217272 commit 428af31

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

src/style/colors.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import _ from 'lodash';
22
//@ts-ignore
33
import Color from 'color';
4+
import {OpaqueColorValue} from 'react-native';
45
import tinycolor from 'tinycolor2';
56
import {colorsPalette, themeColors} from './colorsPalette';
67
import DesignTokens from './designTokens';
@@ -92,6 +93,11 @@ export class Colors {
9293
let green;
9394
let blue;
9495

96+
// Handle design token PlatformColor object
97+
if (typeof p1 === 'object') {
98+
p1 = colorStringValue(p1);
99+
}
100+
95101
if (arguments.length === 2 && typeof p1 === 'string') {
96102
hex = p1;
97103
opacity = p2;
@@ -129,23 +135,19 @@ export class Colors {
129135
}
130136
}
131137

132-
/**
133-
* Return the original color string value by its colorKey based on the current scheme(light/dark)
134-
*/
135-
getColorValue(colorKey: string) {
136-
return Scheme.getScheme(true)[colorKey] ?? this[colorKey];
137-
}
138-
139-
getColorName(color: string) {
138+
getColorName(colorValue: string) {
139+
const color = colorStringValue(colorValue);
140140
return ColorName.name(color)[1];
141141
}
142142

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

149+
const color = colorStringValue(colorValue);
150+
149151
if (color === 'transparent') {
150152
return color;
151153
}
@@ -170,7 +172,7 @@ export class Colors {
170172
return this.getTintedColorForDynamicHex(color, tintKey);
171173
}
172174

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

213-
shouldGenerateDarkerPalette(color: string) {
215+
private shouldGenerateDarkerPalette(color: string) {
214216
const hsl = Color(color).hsl();
215217
const hue = hsl.color[0];
216218
return _.inRange(hue, 51, 184);
217219
}
218220

219-
isDark(color: string) {
221+
isDark(colorValue: string | OpaqueColorValue) {
222+
const color = colorStringValue(colorValue);
220223
const lum = tinycolor(color).getLuminance();
221224
return lum < 0.55;
222225
}
@@ -232,11 +235,17 @@ export class Colors {
232235
isTransparent(color?: string) {
233236
return color && _.toUpper(color) === _.toUpper('transparent');
234237
}
235-
areEqual(colorA: string, colorB: string) {
238+
areEqual(colorAValue: string | OpaqueColorValue, colorBValue: string | OpaqueColorValue) {
239+
const colorA = colorStringValue(colorAValue);
240+
const colorB = colorStringValue(colorBValue);
236241
return _.toLower(colorA) === _.toLower(colorB);
237242
}
238243
}
239244

245+
function colorStringValue(color: string | object) {
246+
return color.toString();
247+
}
248+
240249
function adjustSaturation(colors: string[], color: string) {
241250
let array;
242251
const lightnessLevel = 80;

src/style/scheme.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type SchemeChangeListener = (schemeType?: 'light' | 'dark') => void;
99
class Scheme {
1010
private currentScheme: SchemeType = 'default';
1111
private schemes: Schemes = {light: {}, dark: {}};
12-
private schemesJS: Schemes = {light: {}, dark: {}};
1312
private changeListeners: SchemeChangeListener[] = [];
1413
private usePlatformColors = false;
1514

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

67-
merge(this.schemesJS, schemes);
68-
6966
const platformColorsSchemes: Schemes = cloneDeep(schemes);
67+
7068
forEach(schemes, (scheme, schemeKey) => {
7169
forEach(scheme, (colorValue, colorKey) => {
7270
// @ts-expect-error
7371
Object.defineProperty(platformColorsSchemes[schemeKey], colorKey, {
7472
get: () => {
73+
let color: any = colorValue;
7574
if (this.usePlatformColors) {
7675
if (Constants.isAndroid) {
7776
// Remove the $ prefix cause it's not allowed in Android and add the @color prefix
78-
return PlatformColor(`@color/${colorKey.replace(/^[$]/, '')}`);
77+
color = PlatformColor(`@color/${colorKey.replace(/^[$]/, '')}`);
7978
} else {
80-
return PlatformColor(colorKey);
79+
color = PlatformColor(colorKey);
8180
}
82-
} else {
83-
return colorValue;
81+
// Get the original hex string value by calling toString()
82+
color.toString = () => schemes[this.getSchemeType()][colorKey];
8483
}
84+
return color;
8585
}
8686
});
8787
});
8888
});
89+
8990
merge(this.schemes, platformColorsSchemes);
9091
}
9192

9293
/**
9394
* Retrieve scheme by current scheme type
9495
*/
95-
getScheme(useJS = false) {
96-
if (useJS) {
97-
return this.schemesJS[this.getSchemeType()];
98-
} else {
99-
return this.schemes[this.getSchemeType()];
100-
}
96+
getScheme() {
97+
return this.schemes[this.getSchemeType()];
10198
}
10299

103100
/**

0 commit comments

Comments
 (0)