Skip to content

Commit 2dac1e6

Browse files
authored
Merge pull request #3 from DeepFundAI/codex/add-automatic-light-color-calculation-38yt8c
feat(theme): auto compute color variants
2 parents 2b0ddcd + a7abd69 commit 2dac1e6

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/views/ThemeEditor.vue

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
style="margin-right: 10px"
1818
/>
1919
<el-switch
20-
v-model="autoPrimaryLight"
21-
active-text="自动色值"
22-
inactive-text="手动"
20+
v-model="autoColorCalc"
21+
active-text="自动计算"
22+
inactive-text="手动设置"
2323
style="margin-right: 10px"
2424
/>
2525
<el-button @click="saveObjectAsCss('cssVar.css')">导出</el-button>
@@ -354,7 +354,7 @@ import Github from '@/components/icons/Github.vue'
354354
const activeName = ref('first')
355355
const activeNames = ref(['1'])
356356
const modifyCssVar: Ref<Record<string, string>> = ref({})
357-
const autoPrimaryLight = ref(true)
357+
const autoColorCalc = ref(true)
358358
359359
const isDark = useDark({
360360
selector: 'html',
@@ -378,24 +378,39 @@ const mixColor = (color1: string, color2: string, weight: number) => {
378378
.padStart(2, '0')}${b.toString(16).padStart(2, '0')}`
379379
}
380380
381+
382+
const recomputeColorVars = (cssVar: string, base: string) => {
383+
const match = cssVar.match(/^--el-color-(\w+)$/)
384+
if (!match) return
385+
const type = match[1]
386+
const targetType = type === 'warning' ? 'warnning' : type
387+
const group = color.find((c) => c.type === targetType)
388+
if (!group) return
389+
group.data.forEach((item) => {
390+
if (item.cssVar === cssVar) return
391+
const light = item.cssVar.match(/light-(\d+)$/)
392+
const dark = item.cssVar.match(/dark-(\d+)$/)
393+
let value: string | undefined
394+
if (light) {
395+
value = mixColor('#ffffff', base, Number(light[1]) / 10)
396+
} else if (dark) {
397+
value = mixColor('#000000', base, Number(dark[1]) / 10)
398+
}
399+
if (value) {
400+
item.value = value
401+
setCssVarValue(item.cssVar, value)
402+
modifyCssVar.value[item.cssVar] = value
403+
}
404+
})
405+
}
406+
381407
const updateCssVar = (props: CssVarInfo) => {
382408
const value = props.value + props.unit
383409
setCssVarValue(props.cssVar, value)
384410
modifyCssVar.value[props.cssVar] = value
385411
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)
412+
if (autoColorCalc.value) {
413+
recomputeColorVars(props.cssVar, props.value)
399414
}
400415
}
401416

0 commit comments

Comments
 (0)