@@ -21,14 +21,14 @@ export type GeneratePaletteOptions = {
21
21
adjustLightness ?: boolean ;
22
22
/** Whether to adjust the saturation of colors with high lightness and saturation (unifying saturation level throughout palette) */
23
23
adjustSaturation ?: boolean ;
24
- /** Array of saturation adjustments to apply on the color's tints array (from darkest to lightest).
24
+ /** Array of saturation adjustments to apply on the color's tints array (from darkest to lightest).
25
25
* The 'adjustSaturation' option must be true */
26
26
saturationLevels ?: number [ ] ;
27
27
/** Whether to add two extra dark colors usually used for dark mode (generating a palette of 10 instead of 8 colors) */
28
28
addDarkestTints ?: boolean ; // TODO: rename 'fullPalette'
29
29
/** Whether to reverse the color palette to generate dark mode palette (pass 'true' to generate the same palette for both light and dark modes) */
30
30
avoidReverseOnDark ?: boolean ;
31
- }
31
+ } ;
32
32
export class Colors {
33
33
[ key : string ] : any ;
34
34
shouldSupportDarkMode = false ;
@@ -180,9 +180,12 @@ export class Colors {
180
180
return validColors ? undefined : results [ 0 ] ;
181
181
}
182
182
183
- shouldReverseOnDark = ( avoidReverseOnDark ?: boolean ) => ! avoidReverseOnDark && this . shouldSupportDarkMode && Scheme . isDarkMode ( ) ;
184
-
185
- getColorTint ( colorValue : string | OpaqueColorValue , tintKey : string | number , options : GetColorTintOptions = { } ) {
183
+ shouldReverseOnDark = ( avoidReverseOnDark ?: boolean ) =>
184
+ ! avoidReverseOnDark && this . shouldSupportDarkMode && Scheme . isDarkMode ( ) ;
185
+
186
+ getColorTint ( colorValue : string | OpaqueColorValue | undefined ,
187
+ tintKey : string | number ,
188
+ options : GetColorTintOptions = { } ) {
186
189
if ( _ . isUndefined ( tintKey ) || isNaN ( tintKey as number ) || _ . isUndefined ( colorValue ) ) {
187
190
// console.error('"Colors.getColorTint" must accept a color and tintKey params');
188
191
return colorValue ;
@@ -199,8 +202,9 @@ export class Colors {
199
202
if ( colorKey ) {
200
203
const colorKeys = [ 1 , 5 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 ] ;
201
204
const keyIndex = _ . indexOf ( colorKeys , Number ( tintKey ) ) ;
202
- const key =
203
- this . shouldReverseOnDark ( options ?. avoidReverseOnDark ) ? colorKeys [ colorKeys . length - 1 - keyIndex ] : tintKey ;
205
+ const key = this . shouldReverseOnDark ( options ?. avoidReverseOnDark )
206
+ ? colorKeys [ colorKeys . length - 1 - keyIndex ]
207
+ : tintKey ;
204
208
const requiredColorKey = `${ colorKey . slice ( 0 , - 2 ) } ${ key } ` ;
205
209
const requiredColorKey1 = `${ colorKey . slice ( 0 , - 1 ) } ${ key } ` ;
206
210
const requiredColor = this [ requiredColorKey ] || this [ requiredColorKey1 ] ;
@@ -222,24 +226,26 @@ export class Colors {
222
226
return colorsPalette [ tintLevel - 1 ] ;
223
227
}
224
228
225
- private generatePalette = _ . memoize ( ( color : string , options ?: GeneratePaletteOptions ) : string [ ] => {
229
+ private generatePalette = _ . memoize ( ( color : string , options ?: GeneratePaletteOptions ) : string [ ] => {
226
230
const hsl = Color ( color ) . hsl ( ) ;
227
231
const colorLightness = hsl . color [ 2 ] ;
228
232
const lightness = Math . round ( colorLightness ) ;
229
233
const isWhite = lightness === 100 ;
230
234
const lightnessLevel = options ?. addDarkestTints ? ( isWhite ? 5 : 0 ) : 20 ;
231
235
const lightColorsThreshold = options ?. adjustLightness && this . shouldGenerateDarkerPalette ( color ) ? 5 : 0 ;
232
- const step = /* options?.addDarkestTints ? 9 : */ 10 ;
236
+ const step = /* options?.addDarkestTints ? 9 : */ 10 ;
233
237
const ls = [ colorLightness ] ;
234
-
238
+
235
239
let l = lightness - step ;
236
- while ( l >= lightnessLevel - lightColorsThreshold ) { // darker tints
240
+ while ( l >= lightnessLevel - lightColorsThreshold ) {
241
+ // darker tints
237
242
ls . unshift ( l ) ;
238
243
l -= step ;
239
244
}
240
245
241
246
l = lightness + step ;
242
- while ( l < 100 - lightColorsThreshold ) { // lighter tints
247
+ while ( l < 100 - lightColorsThreshold ) {
248
+ // lighter tints
243
249
ls . push ( l ) ;
244
250
l += step ;
245
251
}
@@ -249,12 +255,12 @@ export class Colors {
249
255
const tint = generateColorTint ( color , e ) ;
250
256
tints . push ( tint ) ;
251
257
} ) ;
252
-
258
+
253
259
const size = options ?. addDarkestTints ? 10 : 8 ;
254
260
const start = options ?. addDarkestTints && colorLightness > 10 ? - size : 0 ;
255
261
const end = options ?. addDarkestTints && colorLightness > 10 ? undefined : size ;
256
262
const sliced = tints . slice ( start , end ) ;
257
-
263
+
258
264
const adjusted = options ?. adjustSaturation && adjustSaturation ( sliced , color , options ?. saturationLevels ) ;
259
265
return adjusted || sliced ;
260
266
} , generatePaletteCacheResolver ) ;
@@ -413,7 +419,7 @@ function threeDigitHexToSix(value: string) {
413
419
414
420
function generatePaletteCacheResolver ( color : string , options ?: GeneratePaletteOptions ) {
415
421
return `${ color } ${ options ? '_' + JSON . stringify ( options ) : '' } ` ;
416
- }
422
+ }
417
423
418
424
const TypedColors = Colors as ExtendTypeWith < typeof Colors , typeof colorsPalette & typeof DesignTokens > ;
419
425
const colorObject = new TypedColors ( ) ;
0 commit comments