1616 :inactive-icon =" Sunny"
1717 style =" margin-right : 10px "
1818 />
19+ <el-switch
20+ v-model =" autoPrimaryLight"
21+ active-text =" 自动色值"
22+ inactive-text =" 手动"
23+ style =" margin-right : 10px "
24+ />
1925 <el-button @click =" saveObjectAsCss('cssVar.css')" >导出</el-button >
2026 <!-- <el-button>编辑</el-button> -->
2127 <el-button type =" primary" >保存</el-button >
@@ -348,6 +354,7 @@ import Github from '@/components/icons/Github.vue'
348354const activeName = ref (' first' )
349355const activeNames = ref ([' 1' ])
350356const modifyCssVar: Ref <Record <string , string >> = ref ({})
357+ const autoPrimaryLight = ref (true )
351358
352359const isDark = useDark ({
353360 selector: ' html' ,
@@ -358,10 +365,38 @@ const isDark = useDark({
358365 storage: localStorage ,
359366})
360367
368+ const mixColor = (color1 : string , color2 : string , weight : number ) => {
369+ const w = Math .max (Math .min (weight , 1 ), 0 )
370+ const toHex = (color : string ) => color .replace (' #' , ' ' )
371+ const c1 = toHex (color1 )
372+ const c2 = toHex (color2 )
373+ const r = Math .round (parseInt (c1 .slice (0 , 2 ), 16 ) * w + parseInt (c2 .slice (0 , 2 ), 16 ) * (1 - w ))
374+ const g = Math .round (parseInt (c1 .slice (2 , 4 ), 16 ) * w + parseInt (c2 .slice (2 , 4 ), 16 ) * (1 - w ))
375+ const b = Math .round (parseInt (c1 .slice (4 , 6 ), 16 ) * w + parseInt (c2 .slice (4 , 6 ), 16 ) * (1 - w ))
376+ return ` #${r .toString (16 ).padStart (2 , ' 0' )}${g
377+ .toString (16 )
378+ .padStart (2 , ' 0' )}${b .toString (16 ).padStart (2 , ' 0' )} `
379+ }
380+
361381const updateCssVar = (props : CssVarInfo ) => {
362382 const value = props .value + props .unit
363383 setCssVarValue (props .cssVar , value )
364384 modifyCssVar .value [props .cssVar ] = value
385+
386+ if (props .cssVar === ' --el-color-primary' && autoPrimaryLight .value ) {
387+ const primaryGroup = color .find ((c ) => c .type === ' primary' )?.data ?? []
388+ const computeLight = (light : number ) => {
389+ const cssVar = ` --el-color-primary-light-${light } `
390+ const target = primaryGroup .find ((d ) => d .cssVar === cssVar )
391+ if (target ) {
392+ const mix = mixColor (' #ffffff' , props .value , light / 10 )
393+ target .value = mix
394+ setCssVarValue (cssVar , mix )
395+ modifyCssVar .value [cssVar ] = mix
396+ }
397+ }
398+ ;[3 , 5 , 7 , 8 , 9 ].forEach (computeLight )
399+ }
365400}
366401
367402const setCssVarValue = (name : string , value : string ) => {
0 commit comments