From aad7cafe034a6c1aadb869ee6683697f7c549860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BF=9E=E5=AE=9D=E5=B1=B1?= <1253070437@qq.com> Date: Sat, 8 Jun 2024 02:08:09 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E8=B0=83=E6=95=B4=E3=80=91=E8=B0=83?= =?UTF-8?q?=E6=95=B4ColorPicker=E6=B3=A8=E9=87=8A=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/ColorPicker/index.vue | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/snowy-admin-web/src/components/ColorPicker/index.vue b/snowy-admin-web/src/components/ColorPicker/index.vue index 0e8f197c..8327d64d 100644 --- a/snowy-admin-web/src/components/ColorPicker/index.vue +++ b/snowy-admin-web/src/components/ColorPicker/index.vue @@ -10,20 +10,16 @@ import tool from '@/utils/tool' const emit = defineEmits(['update:value']) - const props = defineProps({ value: { type: String, default: '#1677FF' } }) - const id = tool.snowyUuid() - const forceResize = () => { window.dispatchEvent(new Event('resize')) } - const update = (val) => { showTxt(val) emit('update:value', val) @@ -37,18 +33,12 @@ if (currentColor) { currentColor.textContent = val } - // 改变文字颜色 currentColor.style.color = changeTextColor(currentColor.style.backgroundColor) } - - /** - * 改变文字颜色 - * @param backgroundColor 背景色 - */ + // 改变文字颜色 backgroundColor背景色 const changeTextColor = (backgroundColor) => { const backgroundHexColor = backgroundColor.length > 7 ? convertRGBToHex(backgroundColor) : backgroundColor - let hex = backgroundHexColor // 如果当前传入的参数以 # 开头,去除当前的 if (hex.startsWith('#')) { @@ -61,22 +51,16 @@ if (hex.length !== 6) { throw new Error('Invalid background color.' + backgroundHexColor) } - const r = parseInt(hex.slice(0, 2), 16) const g = parseInt(hex.slice(2, 4), 16) const b = parseInt(hex.slice(4, 6), 16) - if ([r, g, b].some((x) => Number.isNaN(x))) { throw new Error('Invalid background color.' + backgroundHexColor) } - return r * 0.299 + g * 0.587 + b * 0.114 > 186 ? '#000' : '#fff' } const toHex = (x) => ('0' + parseInt(x).toString(16)).slice(-2) - /** - * RGB 转换为 HEX - * @param rgb RGB 颜色 - */ + // RGB 转换为 HEX const convertRGBToHex = (rgb) => { const bg = rgb.match(/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/) // 返回空字符串,在后面判断长度为 6 时候会报错。不在此处进行操作 @@ -90,11 +74,9 @@