1616 :inactive-icon =" Sunny"
1717 style =" margin-right : 10px "
1818 />
19+ <el-switch
20+ v-model =" autoColorCalc"
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 autoColorCalc = ref (true )
351358
352359const isDark = useDark ({
353360 selector: ' html' ,
@@ -358,10 +365,52 @@ 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+
381+ const recomputeColorVars = (cssVar : string , base : string ) => {
382+ const match = cssVar .match (/ ^ --el-color-(\w + )$ / )
383+ if (! match ) return
384+ const type = match [1 ]
385+ const targetType = type === ' warning' ? ' warnning' : type
386+ const group = color .find ((c ) => c .type === targetType )
387+ if (! group ) return
388+ group .data .forEach ((item ) => {
389+ if (item .cssVar === cssVar ) return
390+ const light = item .cssVar .match (/ light-(\d + )$ / )
391+ const dark = item .cssVar .match (/ dark-(\d + )$ / )
392+ let value: string | undefined
393+ if (light ) {
394+ value = mixColor (' #ffffff' , base , Number (light [1 ]) / 10 )
395+ } else if (dark ) {
396+ value = mixColor (' #000000' , base , Number (dark [1 ]) / 10 )
397+ }
398+ if (value ) {
399+ item .value = value
400+ setCssVarValue (item .cssVar , value )
401+ modifyCssVar .value [item .cssVar ] = value
402+ }
403+ })
404+ }
405+
361406const updateCssVar = (props : CssVarInfo ) => {
362407 const value = props .value + props .unit
363408 setCssVarValue (props .cssVar , value )
364409 modifyCssVar .value [props .cssVar ] = value
410+
411+ if (autoColorCalc .value ) {
412+ recomputeColorVars (props .cssVar , props .value )
413+ }
365414}
366415
367416const setCssVarValue = (name : string , value : string ) => {
0 commit comments