1
1
import _ from 'lodash' ;
2
2
//@ts -ignore
3
3
import Color from 'color' ;
4
+ import { OpaqueColorValue } from 'react-native' ;
4
5
import tinycolor from 'tinycolor2' ;
5
6
import { colorsPalette , themeColors } from './colorsPalette' ;
6
7
import DesignTokens from './designTokens' ;
@@ -92,6 +93,11 @@ export class Colors {
92
93
let green ;
93
94
let blue ;
94
95
96
+ // Handle design token PlatformColor object
97
+ if ( typeof p1 === 'object' ) {
98
+ p1 = colorStringValue ( p1 ) ;
99
+ }
100
+
95
101
if ( arguments . length === 2 && typeof p1 === 'string' ) {
96
102
hex = p1 ;
97
103
opacity = p2 ;
@@ -129,23 +135,19 @@ export class Colors {
129
135
}
130
136
}
131
137
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 ) ;
140
140
return ColorName . name ( color ) [ 1 ] ;
141
141
}
142
142
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 ) ) {
145
145
// console.error('"Colors.getColorTint" must accept a color and tintKey params');
146
- return color ;
146
+ return colorValue ;
147
147
}
148
148
149
+ const color = colorStringValue ( colorValue ) ;
150
+
149
151
if ( color === 'transparent' ) {
150
152
return color ;
151
153
}
@@ -170,7 +172,7 @@ export class Colors {
170
172
return this . getTintedColorForDynamicHex ( color , tintKey ) ;
171
173
}
172
174
173
- getTintedColorForDynamicHex ( color : string , tintKey : string | number ) {
175
+ private getTintedColorForDynamicHex ( color : string , tintKey : string | number ) {
174
176
// Handles dynamic colors (non uilib colors)
175
177
let tintLevel = Math . floor ( Number ( tintKey ) / 10 ) ;
176
178
tintLevel = Math . max ( 1 , tintLevel ) ;
@@ -210,13 +212,14 @@ export class Colors {
210
212
return this . shouldSupportDarkMode && Scheme . getSchemeType ( ) === 'dark' ? _ . reverse ( palette ) : palette ;
211
213
} ) ;
212
214
213
- shouldGenerateDarkerPalette ( color : string ) {
215
+ private shouldGenerateDarkerPalette ( color : string ) {
214
216
const hsl = Color ( color ) . hsl ( ) ;
215
217
const hue = hsl . color [ 0 ] ;
216
218
return _ . inRange ( hue , 51 , 184 ) ;
217
219
}
218
220
219
- isDark ( color : string ) {
221
+ isDark ( colorValue : string | OpaqueColorValue ) {
222
+ const color = colorStringValue ( colorValue ) ;
220
223
const lum = tinycolor ( color ) . getLuminance ( ) ;
221
224
return lum < 0.55 ;
222
225
}
@@ -232,11 +235,17 @@ export class Colors {
232
235
isTransparent ( color ?: string ) {
233
236
return color && _ . toUpper ( color ) === _ . toUpper ( 'transparent' ) ;
234
237
}
235
- areEqual ( colorA : string , colorB : string ) {
238
+ areEqual ( colorAValue : string | OpaqueColorValue , colorBValue : string | OpaqueColorValue ) {
239
+ const colorA = colorStringValue ( colorAValue ) ;
240
+ const colorB = colorStringValue ( colorBValue ) ;
236
241
return _ . toLower ( colorA ) === _ . toLower ( colorB ) ;
237
242
}
238
243
}
239
244
245
+ function colorStringValue ( color : string | object ) {
246
+ return color . toString ( ) ;
247
+ }
248
+
240
249
function adjustSaturation ( colors : string [ ] , color : string ) {
241
250
let array ;
242
251
const lightnessLevel = 80 ;
0 commit comments