| | |
| | | // 处理主题样式 |
| | | export function handleThemeStyle(theme) { |
| | | document.documentElement.style.setProperty('--el-color-primary', 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(theme, i / 10)}`) |
| | | 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(theme, i / 10)}`) |
| | | 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 = str.replace('#', '') |
| | | let hexs = str.match(/../g) |
| | | 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颜色 |
| | | // rgb颜色转hex颜色 |
| | | export function rgbToHex(r, g, b) { |
| | | let hexs = [r.toString(16), g.toString(16), b.toString(16)] |
| | | const hexs = [r.toString(16), g.toString(16), b.toString(16)] |
| | | for (let i = 0; i < 3; i++) { |
| | | if (hexs[i].length == 1) { |
| | | if (hexs[i].length === 1) { |
| | | hexs[i] = `0${hexs[i]}` |
| | | } |
| | | } |
| | |
| | | |
| | | // 变浅颜色值 |
| | | export function getLightColor(color, level) { |
| | | let rgb = hexToRgb(color) |
| | | const rgb = hexToRgb(color) |
| | | for (let i = 0; i < 3; i++) { |
| | | rgb[i] = Math.floor((255 - rgb[i]) * level + rgb[i]) |
| | | } |
| | |
| | | |
| | | // 变深颜色值 |
| | | export function getDarkColor(color, level) { |
| | | let rgb = hexToRgb(color) |
| | | 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()}` |
| | | } |