@@ -15,7 +15,14 @@ export type DesignToken = {semantic?: [string]; resource_paths?: [string]; toStr
15
15
export type TokensOptions = { primaryColor : string } ;
16
16
export type GetColorTintOptions = { avoidReverseOnDark ?: boolean } ;
17
17
export type GetColorByHexOptions = { validColors ?: string [ ] } ;
18
-
18
+ export type GeneratePaletteOptions = {
19
+ /** Whether to adjust the lightness of very light colors (generating darker palette) */
20
+ adjustLightness ?: boolean ;
21
+ /** Whether to adjust the saturation of colors with high lightness and saturation (unifying saturation level throughout palette) */
22
+ adjustSaturation ?: boolean ;
23
+ /** Whether to add two extra dark colors usually used for dark mode (generating a palette of 10 instead of 8 colors) */
24
+ addDarkestTints ?: boolean ;
25
+ }
19
26
export class Colors {
20
27
[ key : string ] : any ;
21
28
private shouldSupportDarkMode = false ;
@@ -208,20 +215,24 @@ export class Colors {
208
215
return colorsPalette [ tintLevel - 1 ] ;
209
216
}
210
217
211
- private generatePalette = _ . memoize ( ( color : string ) : string [ ] => {
218
+ private generatePalette = _ . memoize ( ( color : string , options ?: GeneratePaletteOptions ) : string [ ] => {
219
+ const defaultOptions = { adjustLightness : true , adjustSaturation : true , addDarkestTints : false } ;
220
+ const _options = { ...defaultOptions , ...options } ;
221
+
212
222
const hsl = Color ( color ) . hsl ( ) ;
213
223
const lightness = Math . round ( hsl . color [ 2 ] ) ;
214
- const lightColorsThreshold = this . shouldGenerateDarkerPalette ( color ) ? 5 : 0 ;
215
-
224
+ const lightColorsThreshold = _options . adjustLightness && this . shouldGenerateDarkerPalette ( color ) ? 5 : 0 ;
216
225
const ls = [ hsl . color [ 2 ] ] ;
226
+
217
227
let l = lightness - 10 ;
218
- while ( l >= 20 - lightColorsThreshold ) {
228
+ const lightnessLevel = _options . addDarkestTints ? 0 : 20 ;
229
+ while ( l >= lightnessLevel - lightColorsThreshold ) { // darker tints
219
230
ls . unshift ( l ) ;
220
231
l -= 10 ;
221
232
}
222
233
223
234
l = lightness + 10 ;
224
- while ( l < 100 - lightColorsThreshold ) {
235
+ while ( l < 100 - lightColorsThreshold ) { // lighter tints
225
236
ls . push ( l ) ;
226
237
l += 10 ;
227
238
}
@@ -232,13 +243,14 @@ export class Colors {
232
243
tints . push ( tint ) ;
233
244
} ) ;
234
245
235
- const sliced = tints . slice ( 0 , 8 ) ;
236
- const adjusted = adjustSaturation ( sliced , color ) ;
246
+ const size = _options . addDarkestTints ? 10 : 8 ;
247
+ const sliced = tints . slice ( 0 , size ) ;
248
+ const adjusted = _options . adjustSaturation && adjustSaturation ( sliced , color ) ;
237
249
return adjusted || sliced ;
238
250
} ) ;
239
251
240
- generateColorPalette = _ . memoize ( ( color : string ) : string [ ] => {
241
- const palette = this . generatePalette ( color ) ;
252
+ generateColorPalette = _ . memoize ( ( color : string , options ?: GeneratePaletteOptions ) : string [ ] => {
253
+ const palette = this . generatePalette ( color , options ) ;
242
254
return this . shouldSupportDarkMode && Scheme . getSchemeType ( ) === 'dark' ? _ . reverse ( palette ) : palette ;
243
255
} ) ;
244
256
0 commit comments