mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-12 09:18:14 +08:00
🐛 修复颜色解析错误
* PostID: 46557602
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* @file utils/toolFunc.ts
|
||||
* @description 一些工具函数
|
||||
* @since Beta v0.3.8
|
||||
* @since Beta v0.3.9
|
||||
*/
|
||||
|
||||
import { os, path } from "@tauri-apps/api";
|
||||
@@ -163,18 +163,40 @@ export function getRandomString(length: number, type: string = "all"): string {
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 将颜色转为 hex
|
||||
* @since Beta v0.3.9
|
||||
* @param {string} color - 颜色
|
||||
* @returns {string} hex
|
||||
*/
|
||||
function color2Hex(color: string): string {
|
||||
if (color.startsWith("#")) return color;
|
||||
if (color.startsWith("rgb")) {
|
||||
// 正则获取 rgb(0, 0, 0) 或 rgba(0, 0, 0, 0) 或 rgb(0 0 0/0)
|
||||
const reg = /rgba?\((.*?)\)/;
|
||||
const match = reg.exec(color);
|
||||
if (match === null) return "#000000";
|
||||
const rgb = match[1];
|
||||
const rgbArr = rgb.split(/[ ,/]/);
|
||||
if (rgbArr.length < 3) return "#000000";
|
||||
const r = parseInt(rgbArr[0]);
|
||||
const g = parseInt(rgbArr[1]);
|
||||
const b = parseInt(rgbArr[2]);
|
||||
return colorConvert.rgb.hex([r, g, b]);
|
||||
}
|
||||
return colorConvert.keyword.hex(<KEYWORD>color);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 判断颜色是否相似
|
||||
* @since Beta v0.3.7
|
||||
* @since Beta v0.3.9
|
||||
* @param {string} colorBg - 背景颜色
|
||||
* @param {string} colorText - 文本颜色
|
||||
* @returns {boolean} 是否相似
|
||||
*/
|
||||
export function isColorSimilar(colorBg: string, colorText: string): boolean {
|
||||
const hexBg = colorBg.startsWith("#") ? colorBg : colorConvert.keyword.hex(<KEYWORD>colorBg);
|
||||
const hexText = colorText.startsWith("#")
|
||||
? colorText
|
||||
: colorConvert.keyword.hex(<KEYWORD>colorText);
|
||||
const hexBg = color2Hex(colorBg);
|
||||
const hexText = color2Hex(colorText);
|
||||
return score(hexText, hexBg) === "Fail";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user