@@ -22,6 +22,8 @@ export type GeneratePaletteOptions = {
22
22
adjustSaturation ?: boolean ;
23
23
/** Whether to add two extra dark colors usually used for dark mode (generating a palette of 10 instead of 8 colors) */
24
24
addDarkestTints ?: boolean ;
25
+ /** Whether to reverse the color palette to generate dark mode palette (pass 'true' to generate the same palette for both light and dark modes) */
26
+ avoidReverseOnDark ?: boolean ;
25
27
}
26
28
export class Colors {
27
29
[ key : string ] : any ;
@@ -173,6 +175,8 @@ export class Colors {
173
175
return validColors ? undefined : results [ 0 ] ;
174
176
}
175
177
178
+ shouldReverseOnDark = ( avoidReverseOnDark ?: boolean ) => ! avoidReverseOnDark && this . shouldSupportDarkMode && Scheme . getSchemeType ( ) === 'dark' ;
179
+
176
180
getColorTint ( colorValue : string | OpaqueColorValue , tintKey : string | number , options : GetColorTintOptions = { } ) {
177
181
if ( _ . isUndefined ( tintKey ) || isNaN ( tintKey as number ) || _ . isUndefined ( colorValue ) ) {
178
182
// console.error('"Colors.getColorTint" must accept a color and tintKey params');
@@ -190,10 +194,8 @@ export class Colors {
190
194
if ( colorKey ) {
191
195
const colorKeys = [ 1 , 5 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 ] ;
192
196
const keyIndex = _ . indexOf ( colorKeys , Number ( tintKey ) ) ;
193
- const shouldReverseOnDark =
194
- ! options ?. avoidReverseOnDark && this . shouldSupportDarkMode && Scheme . getSchemeType ( ) === 'dark' ;
195
- const key = shouldReverseOnDark ? colorKeys [ colorKeys . length - 1 - keyIndex ] : tintKey ;
196
-
197
+ const key =
198
+ this . shouldReverseOnDark ( options ?. avoidReverseOnDark ) ? colorKeys [ colorKeys . length - 1 - keyIndex ] : tintKey ;
197
199
const requiredColorKey = `${ colorKey . slice ( 0 , - 2 ) } ${ key } ` ;
198
200
const requiredColorKey1 = `${ colorKey . slice ( 0 , - 1 ) } ${ key } ` ;
199
201
const requiredColor = this [ requiredColorKey ] || this [ requiredColorKey1 ] ;
@@ -215,17 +217,14 @@ export class Colors {
215
217
return colorsPalette [ tintLevel - 1 ] ;
216
218
}
217
219
218
- private generatePalette = _ . memoize ( ( color : string , options ?: GeneratePaletteOptions ) : string [ ] => {
219
- const defaultOptions = { adjustLightness : true , adjustSaturation : true , addDarkestTints : false } ;
220
- const _options = { ...defaultOptions , ...options } ;
221
-
220
+ private generatePalette = _ . memoize ( ( color : string , options ?: GeneratePaletteOptions ) : string [ ] => {
222
221
const hsl = Color ( color ) . hsl ( ) ;
223
222
const lightness = Math . round ( hsl . color [ 2 ] ) ;
224
- const lightColorsThreshold = _options . adjustLightness && this . shouldGenerateDarkerPalette ( color ) ? 5 : 0 ;
223
+ const lightColorsThreshold = options ? .adjustLightness && this . shouldGenerateDarkerPalette ( color ) ? 5 : 0 ;
225
224
const ls = [ hsl . color [ 2 ] ] ;
226
225
227
226
let l = lightness - 10 ;
228
- const lightnessLevel = _options . addDarkestTints ? 0 : 20 ;
227
+ const lightnessLevel = options ? .addDarkestTints ? 0 : 20 ;
229
228
while ( l >= lightnessLevel - lightColorsThreshold ) { // darker tints
230
229
ls . unshift ( l ) ;
231
230
l -= 10 ;
@@ -243,15 +242,18 @@ export class Colors {
243
242
tints . push ( tint ) ;
244
243
} ) ;
245
244
246
- const size = _options . addDarkestTints ? 10 : 8 ;
245
+ const size = options ? .addDarkestTints ? 10 : 8 ;
247
246
const sliced = tints . slice ( 0 , size ) ;
248
- const adjusted = _options . adjustSaturation && adjustSaturation ( sliced , color ) ;
247
+ const adjusted = options ? .adjustSaturation && adjustSaturation ( sliced , color ) ;
249
248
return adjusted || sliced ;
250
249
} ) ;
251
250
251
+ defaultOptions = { adjustLightness : true , adjustSaturation : true , addDarkestTints : false , avoidReverseOnDark : false } ;
252
+
252
253
generateColorPalette = _ . memoize ( ( color : string , options ?: GeneratePaletteOptions ) : string [ ] => {
253
- const palette = this . generatePalette ( color , options ) ;
254
- return this . shouldSupportDarkMode && Scheme . getSchemeType ( ) === 'dark' ? _ . reverse ( palette ) : palette ;
254
+ const _options = { ...this . defaultOptions , ...options } ;
255
+ const palette = this . generatePalette ( color , _options ) ;
256
+ return this . shouldReverseOnDark ( _options ?. avoidReverseOnDark ) ? _ . reverse ( palette ) : palette ;
255
257
} ) ;
256
258
257
259
private generateDesignTokens ( primaryColor : string , dark ?: boolean ) {
0 commit comments