export interface ParsedColor { r: number; g: number; b: number; a?: number; } /** * 将颜色字符串解析为包含红色、绿色、蓝色和 alpha 分量的对象。 */ export function parseColor(color: string): ParsedColor { const match = color.match( /rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)/ ); if (match) { return { r: parseInt(match[1], 10), g: parseInt(match[2], 10), b: parseInt(match[3], 10), a: match[4] ? parseFloat(match[4]) : 1, }; } const hex = color.replace("#", ""); return { r: parseInt(hex.slice(0, 2), 16), g: parseInt(hex.slice(2, 4), 16), b: parseInt(hex.slice(4, 6), 16), }; }