liyong
16 小时以前 93b8ceac34e2fbd5c57fe5ab4f5bac32c85408aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 处理主题样式
export function handleThemeStyle(theme) {
    const primary = normalizeHex(theme)
    const [r, g, b] = hexToRgb(primary)
    const light2 = getLightColor(primary, 0.2)
    const light3 = getLightColor(primary, 0.3)
    const light5 = getLightColor(primary, 0.5)
 
    document.documentElement.style.setProperty('--el-color-primary', primary)
    document.documentElement.style.setProperty('--el-color-primary-rgb', `${r}, ${g}, ${b}`)
    for (let i = 1; i <= 9; i++) {
        document.documentElement.style.setProperty(`--el-color-primary-light-${i}`, `${getLightColor(primary, i / 10)}`)
    }
    for (let i = 1; i <= 9; i++) {
        document.documentElement.style.setProperty(`--el-color-primary-dark-${i}`, `${getDarkColor(primary, i / 10)}`)
    }
 
    // 系统主题联动到侧边栏选中态与高亮
    document.documentElement.style.setProperty('--menu-active-bg', `linear-gradient(135deg, ${primary} 0%, ${light3} 100%)`)
    document.documentElement.style.setProperty('--menu-active-glow', `0 10px 24px rgba(${r}, ${g}, ${b}, 0.32)`)
    document.documentElement.style.setProperty('--menu-hover', `rgba(${r}, ${g}, ${b}, 0.2)`)
    document.documentElement.style.setProperty('--accent-primary', primary)
    document.documentElement.style.setProperty('--accent-light', light2)
    document.documentElement.style.setProperty('--accent-lighter', light5)
}
 
// hex颜色转rgb颜色
export function hexToRgb(str) {
    str = normalizeHex(str).replace('#', '')
    const hexs = str.match(/../g) || ['40', '9e', 'ff']
    for (let i = 0; i < 3; i++) {
        hexs[i] = parseInt(hexs[i], 16)
    }
    return hexs
}
 
// rgb颜色转hex颜色
export function rgbToHex(r, g, b) {
    const hexs = [r.toString(16), g.toString(16), b.toString(16)]
    for (let i = 0; i < 3; i++) {
        if (hexs[i].length === 1) {
            hexs[i] = `0${hexs[i]}`
        }
    }
    return `#${hexs.join('')}`
}
 
// 变浅颜色值
export function getLightColor(color, level) {
    const rgb = hexToRgb(color)
    for (let i = 0; i < 3; i++) {
        rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i])
    }
    return rgbToHex(rgb[0], rgb[1], rgb[2])
}
 
// 变深颜色值
export function getDarkColor(color, level) {
    const rgb = hexToRgb(color)
    for (let i = 0; i < 3; i++) {
        rgb[i] = Math.floor(rgb[i] * (1 - level))
    }
    return rgbToHex(rgb[0], rgb[1], rgb[2])
}
 
function normalizeHex(color) {
    if (!color || typeof color !== 'string') return '#409eff'
    let value = color.trim().replace('#', '')
    if (value.length === 3) {
        value = value.split('').map((s) => s + s).join('')
    }
    if (!/^[0-9a-fA-F]{6}$/.test(value)) return '#409eff'
    return `#${value.toLowerCase()}`
}