1
1
import { TinyColor } from '@ctrl/tinycolor'
2
2
import { generate , presetPalettes , presetDarkPalettes } from '@ant-design/colors'
3
3
4
- export const getAlphaColor = ( baseColor : string , alpha : number ) =>
5
- new TinyColor ( baseColor ) . setAlpha ( alpha ) . toRgbString ( )
6
-
7
- export const getSolidColor = ( baseColor : string , brightness : number ) => {
8
- const instance = new TinyColor ( baseColor )
9
- return instance . darken ( brightness ) . toHexString ( )
10
- }
11
-
12
- export const getTintColor = ( baseColor : string , tintNumber : number ) => {
13
- return new TinyColor ( baseColor ) . tint ( tintNumber ) . toString ( )
14
- }
15
-
16
- export const getShadeColor = ( baseColor : string , shadeNumber : number ) => {
17
- return new TinyColor ( baseColor ) . shade ( shadeNumber ) . toString ( )
18
- }
19
-
20
4
export const getLightNeutralColor = ( ) => {
21
5
return {
22
- '--neutral- color' : '#000000e0' ,
23
- '--neutral-secondary' : '#000000a6' ,
24
- '--neutral-disabled' : '#00000040' ,
25
- '--neutral-disabled-bg' : '#0000000a' ,
26
- '--neutral-border' : '#d9d9d9' ,
27
- '--neutral-separator' : '#0505050f' ,
28
- '--neutral-bg' : '#f5f5f5' ,
6
+ '--color-neutral ' : '#000000e0' ,
7
+ '--color- neutral-secondary' : '#000000a6' ,
8
+ '--color- neutral-disabled' : '#00000040' ,
9
+ '--color- neutral-disabled-bg' : '#0000000a' ,
10
+ '--color- neutral-border' : '#d9d9d9' ,
11
+ '--color- neutral-separator' : '#0505050f' ,
12
+ '--color- neutral-bg' : '#f5f5f5' ,
29
13
}
30
14
}
31
15
32
16
export const getDarkNeutralColor = ( ) => {
33
17
return {
34
- '--neutral- color' : '#FFFFFFD9' ,
35
- '--neutral-secondary' : '#FFFFFFA6' ,
36
- '--neutral-disabled' : '#FFFFFF40' ,
37
- '--neutral-disabled-bg' : 'rgba(255, 255, 255, 0.08)' ,
38
- '--neutral-border' : '#424242' ,
39
- '--neutral-separator' : '#FDFDFD1F' ,
40
- '--neutral-bg' : '#000000' ,
18
+ '--color-neutral ' : '#FFFFFFD9' ,
19
+ '--color- neutral-secondary' : '#FFFFFFA6' ,
20
+ '--color- neutral-disabled' : '#FFFFFF40' ,
21
+ '--color- neutral-disabled-bg' : 'rgba(255, 255, 255, 0.08)' ,
22
+ '--color- neutral-border' : '#424242' ,
23
+ '--color- neutral-separator' : '#FDFDFD1F' ,
24
+ '--color- neutral-bg' : '#000000' ,
41
25
}
42
26
}
43
27
28
+ const cacheColors = new Map < string , Record < string , string > > ( )
29
+
44
30
export const getCssVarColor = (
45
31
baseColor : string ,
46
- opts ? : { appearance : 'light' | 'dark' ; backgroundColor : string } ,
32
+ opts : { appearance : 'light' | 'dark' ; backgroundColor : string } ,
47
33
) => {
48
- const { appearance = 'light' , backgroundColor = '#141414' } = opts || { }
34
+ const { appearance = 'light' , backgroundColor = '#141414' } = opts
35
+ const cacheKey = `${ baseColor } -${ appearance } -${ backgroundColor } `
36
+ if ( cacheColors . has ( cacheKey ) ) {
37
+ return cacheColors . get ( cacheKey )
38
+ }
49
39
const color = new TinyColor ( baseColor )
50
40
const preset = appearance === 'dark' ? presetDarkPalettes : presetPalettes
51
41
const colors =
@@ -55,50 +45,29 @@ export const getCssVarColor = (
55
45
appearance === 'dark' ? { theme : appearance , backgroundColor } : undefined ,
56
46
)
57
47
const accentColor = colors [ 5 ]
58
- return {
59
- '--accent- color-1' : colors [ 0 ] ,
60
- '--accent- color-2' : colors [ 1 ] ,
61
- '--accent- color-3' : colors [ 2 ] ,
62
- '--accent- color-4' : colors [ 3 ] ,
63
- '--accent- color-5' : colors [ 4 ] ,
64
- '--accent- color-6' : colors [ 5 ] ,
65
- '--accent- color-7' : colors [ 6 ] ,
66
- '--accent- color-8' : colors [ 7 ] ,
67
- '--accent- color-9' : colors [ 8 ] ,
68
- '--accent- color-10' : colors [ 9 ] ,
69
- '--accent- color' : accentColor ,
70
- '--accent- color-hover' : colors [ 4 ] ,
71
- '--accent- color-active' : colors [ 5 ] ,
72
- '--accent- color-content' : '#ffffff' ,
73
- ... ( appearance === 'dark' ? getDarkNeutralColor ( ) : getLightNeutralColor ( ) ) ,
74
- '--bg- color' : baseColor ,
75
- '--bg- color-hover ' : getTintColor ( baseColor , 10 ) ,
76
- '--bg- color-active ' : getTintColor ( baseColor , 20 ) ,
77
- '--bg- color-content ' : '#ffffff' ,
48
+ const cssVars = {
49
+ '--color-accent -1' : colors [ 0 ] ,
50
+ '--color-accent -2' : colors [ 1 ] ,
51
+ '--color-accent -3' : colors [ 2 ] ,
52
+ '--color-accent -4' : colors [ 3 ] ,
53
+ '--color-accent -5' : colors [ 4 ] ,
54
+ '--color-accent -6' : colors [ 5 ] ,
55
+ '--color-accent -7' : colors [ 6 ] ,
56
+ '--color-accent -8' : colors [ 7 ] ,
57
+ '--color-accent -9' : colors [ 8 ] ,
58
+ '--color-accent -10' : colors [ 9 ] ,
59
+ '--color-accent ' : accentColor ,
60
+ '--color-accent -hover' : colors [ 4 ] ,
61
+ '--color-accent- active' : colors [ 6 ] ,
62
+ '--color-accent -content' : '#ffffff' ,
63
+
64
+ '--color-error ' : preset . red [ 4 ] ,
65
+ '--color-warning ' : preset . yellow [ 4 ] ,
66
+ '--color-success ' : preset . green [ 4 ] ,
67
+ '--color-info ' : preset . blue [ 4 ] ,
78
68
79
- '--border-color' : baseColor ,
80
- '--border-color-hover' : getTintColor ( baseColor , 10 ) ,
81
- '--border-color-active' : getTintColor ( baseColor , 20 ) ,
82
- '--border-color-tint-10' : getTintColor ( baseColor , 10 ) ,
83
- '--border-color-tint-20' : getTintColor ( baseColor , 20 ) ,
84
- '--border-color-tint-30' : getTintColor ( baseColor , 30 ) ,
85
- '--border-color-tint-40' : getTintColor ( baseColor , 40 ) ,
86
- '--border-color-tint-50' : getTintColor ( baseColor , 50 ) ,
87
- '--border-color-tint-60' : getTintColor ( baseColor , 60 ) ,
88
- '--border-color-tint-70' : getTintColor ( baseColor , 70 ) ,
89
- '--border-color-tint-80' : getTintColor ( baseColor , 80 ) ,
90
- '--border-color-tint-90' : getTintColor ( baseColor , 90 ) ,
91
- '--bg-color-tint-10' : getTintColor ( baseColor , 10 ) ,
92
- '--bg-color-tint-20' : getTintColor ( baseColor , 20 ) ,
93
- '--bg-color-tint-30' : getTintColor ( baseColor , 30 ) ,
94
- '--bg-color-tint-40' : getTintColor ( baseColor , 40 ) ,
95
- '--bg-color-tint-50' : getTintColor ( baseColor , 50 ) ,
96
- '--bg-color-tint-60' : getTintColor ( baseColor , 60 ) ,
97
- '--bg-color-tint-70' : getTintColor ( baseColor , 70 ) ,
98
- '--bg-color-tint-80' : getTintColor ( baseColor , 80 ) ,
99
- '--bg-color-tint-90' : getTintColor ( baseColor , 90 ) ,
100
- '--text-color' : baseColor ,
101
- '--text-color-hover' : getTintColor ( baseColor , 10 ) ,
102
- '--text-color-active' : getTintColor ( baseColor , 20 ) ,
69
+ ...( appearance === 'dark' ? getDarkNeutralColor ( ) : getLightNeutralColor ( ) ) ,
103
70
}
71
+ cacheColors . set ( cacheKey , cssVars )
72
+ return cssVars
104
73
}
0 commit comments