39 lines
983 B
Plaintext
39 lines
983 B
Plaintext
/**
|
||
* 简单检查是否是有效的颜色值
|
||
* @param {string} value
|
||
* @returns {boolean}
|
||
*/
|
||
function isValidColor(value : string | null) {
|
||
if (value == null) return false;
|
||
|
||
// 常见颜色格式检查
|
||
return (
|
||
// 颜色关键字(red, blue 等)
|
||
/^[a-z]+$/i.test(value!) ||
|
||
// 十六进制(#fff, #ffffff)
|
||
/^#([0-9a-f]{3}){1,2}$/i.test(value!) ||
|
||
// rgb/rgba
|
||
/^rgba?\([\s\d.,%]+\)$/.test(value!) ||
|
||
// hsl/hsla
|
||
/^hsla?\([\s\d.,%]+\)$/.test(value!)
|
||
);
|
||
}
|
||
|
||
|
||
export function getCssVariableColor(element : UniTextElement | null) : string | null {
|
||
if (element == null) return null
|
||
let value = element!.style.getPropertyValue('color').trim();
|
||
|
||
// 如果没有找到变量值,直接返回 null
|
||
if (value == '') return null;
|
||
if(value.startsWith('var(')) {
|
||
const innerVar = value.match(/var\(([^,)]+)(?:,\s*(.*))?\)/);
|
||
if(innerVar == null) return null
|
||
const [, , innerFallback] = innerVar!;
|
||
return innerFallback
|
||
|
||
} else {
|
||
return value
|
||
}
|
||
|
||
} |