Skip to content

Commit 1613370

Browse files
committed
fix: #64 修复切换主题后hover颜色未变,以及主题色和辅助色的rgb变量未变化的问题
1 parent 6289bbd commit 1613370

File tree

2 files changed

+84
-4
lines changed

2 files changed

+84
-4
lines changed

src/store/modules/app.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { defineStore } from 'pinia'
2-
import { store } from '../index'
3-
import { humpToUnderline, setCssVar } from '@/utils'
4-
import { ElMessage } from 'element-plus'
51
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
62
import { ElementPlusSize } from '@/types/elementPlus'
73
import { LayoutType } from '@/types/layout'
84
import { ThemeTypes } from '@/types/theme'
5+
import { humpToUnderline, setCssVar } from '@/utils'
6+
import { getCssColorVariable, hexToRGB, mix } from '@/utils/color'
7+
import { ElMessage } from 'element-plus'
8+
import { defineStore } from 'pinia'
9+
import { store } from '../index'
910

1011
const { wsCache } = useCache()
1112

@@ -183,6 +184,40 @@ export const useAppStore = defineStore('app', {
183184
}
184185
},
185186
actions: {
187+
setPrimaryLight() {
188+
if (this.theme.elColorPrimary) {
189+
const elColorPrimary = this.theme.elColorPrimary
190+
const color = this.isDark ? '#000000' : '#ffffff'
191+
const lightList = [3, 5, 7, 8, 9]
192+
lightList.forEach((v) => {
193+
setCssVar(`--el-color-primary-light-${v}`, mix(color, elColorPrimary, v / 10))
194+
})
195+
setCssVar(`--el-color-primary-dark-2`, mix(color, elColorPrimary, 0.2))
196+
197+
this.setAllColorRgbVars()
198+
}
199+
},
200+
201+
// 处理element自带的主题色和辅助色的-rgb切换主题变化,如:--el-color-primary-rgb
202+
setAllColorRgbVars() {
203+
// 需要处理的颜色类型列表
204+
const colorTypes = ['primary', 'success', 'warning', 'danger', 'error', 'info']
205+
206+
colorTypes.forEach((type) => {
207+
// 获取当前颜色值
208+
const colorValue = getCssColorVariable(`--el-color-${type}`)
209+
if (colorValue) {
210+
// 转换为rgba并提取RGB部分
211+
const rgbaString = hexToRGB(colorValue, 1)
212+
const rgbValues = rgbaString.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/i)
213+
if (rgbValues) {
214+
const [, r, g, b] = rgbValues
215+
// 设置对应的RGB变量
216+
setCssVar(`--el-color-${type}-rgb`, `${r}, ${g}, ${b}`)
217+
}
218+
}
219+
})
220+
},
186221
setBreadcrumb(breadcrumb: boolean) {
187222
this.breadcrumb = breadcrumb
188223
},
@@ -256,6 +291,7 @@ export const useAppStore = defineStore('app', {
256291
document.documentElement.classList.remove('dark')
257292
}
258293
wsCache.set(CACHE_KEY.IS_DARK, this.isDark)
294+
this.setPrimaryLight()
259295
},
260296
setCurrentSize(currentSize: ElementPlusSize) {
261297
this.currentSize = currentSize
@@ -272,6 +308,7 @@ export const useAppStore = defineStore('app', {
272308
for (const key in this.theme) {
273309
setCssVar(`--${humpToUnderline(key)}`, this.theme[key])
274310
}
311+
this.setPrimaryLight()
275312
},
276313
setFooter(footer: boolean) {
277314
this.footer = footer

src/utils/color.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,46 @@ export const PREDEFINE_COLORS = [
172172
'#1f73c3',
173173
'#711f57'
174174
]
175+
176+
177+
/**
178+
* Mixes two colors.
179+
*
180+
* @param {string} color1 - The first color, should be a 6-digit hexadecimal color code starting with `#`.
181+
* @param {string} color2 - The second color, should be a 6-digit hexadecimal color code starting with `#`.
182+
* @param {number} [weight=0.5] - The weight of color1 in the mix, should be a number between 0 and 1, where 0 represents 100% of color2, and 1 represents 100% of color1.
183+
* @returns {string} The mixed color, a 6-digit hexadecimal color code starting with `#`.
184+
*/
185+
export const mix = (color1: string, color2: string, weight: number = 0.5): string => {
186+
let color = '#'
187+
for (let i = 0; i <= 2; i++) {
188+
const c1 = parseInt(color1.substring(1 + i * 2, 3 + i * 2), 16)
189+
const c2 = parseInt(color2.substring(1 + i * 2, 3 + i * 2), 16)
190+
const c = Math.round(c1 * weight + c2 * (1 - weight))
191+
color += c.toString(16).padStart(2, '0')
192+
}
193+
return color
194+
}
195+
196+
/**
197+
* getCssColorVariable
198+
* @description 获取css变量的颜色值
199+
* @param colorVariable css变量名
200+
* @param opacity 透明度
201+
* @returns {string} 颜色值
202+
* @example getCssColorVariable('--el-color-primary', 0.5)
203+
* @example getCssColorVariable('--el-color-primary')
204+
* @example getCssColorVariable()
205+
*/
206+
export const getCssColorVariable = (
207+
colorVariable: string = '--el-color-primary',
208+
opacity?: number
209+
) => {
210+
const colorValue = getComputedStyle(document.documentElement)
211+
.getPropertyValue(colorVariable)
212+
.trim()
213+
if (colorValue) {
214+
return opacity ? hexToRGB(colorValue, opacity) : colorValue
215+
}
216+
return ''
217+
}

0 commit comments

Comments
 (0)