Files
qiming/qiming-mobile/uni_modules/lime-button/components/l-button/utils.uts

39 lines
983 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 简单检查是否是有效的颜色值
* @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
}
}