11import { TinyColor } from '@ctrl/tinycolor'
22import { generate , presetPalettes , presetDarkPalettes } from '@ant-design/colors'
33
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-
204export const getLightNeutralColor = ( ) => {
215 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' ,
2913 }
3014}
3115
3216export const getDarkNeutralColor = ( ) => {
3317 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' ,
4125 }
4226}
4327
28+ const cacheColors = new Map < string , Record < string , string > > ( )
29+
4430export const getCssVarColor = (
4531 baseColor : string ,
46- opts ? : { appearance : 'light' | 'dark' ; backgroundColor : string } ,
32+ opts : { appearance : 'light' | 'dark' ; backgroundColor : string } ,
4733) => {
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+ }
4939 const color = new TinyColor ( baseColor )
5040 const preset = appearance === 'dark' ? presetDarkPalettes : presetPalettes
5141 const colors =
@@ -55,50 +45,29 @@ export const getCssVarColor = (
5545 appearance === 'dark' ? { theme : appearance , backgroundColor } : undefined ,
5646 )
5747 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 ] ,
7868
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 ( ) ) ,
10370 }
71+ cacheColors . set ( cacheKey , cssVars )
72+ return cssVars
10473}
0 commit comments