/** * Color conversion utilities. */ /** * Convert color from various formats to [r, g, b, a]. */ export function parseColor(color: string | number[]): number[] { if (Array.isArray(color)) { return color.length === 3 ? [...color, 255] : color; } // Parse hex color if (color.startsWith('#')) { const hex = color.slice(1); if (hex.length === 3) { return [ parseInt(hex[0] + hex[0], 16), parseInt(hex[1] + hex[1], 16), parseInt(hex[2] + hex[2], 16), 255, ]; } return [ parseInt(hex.slice(0, 2), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(4, 6), 16), hex.length === 8 ? parseInt(hex.slice(6, 8), 16) : 255, ]; } return [51, 136, 255, 255]; // Default blue } /** * Convert hex color to rgba format. */ export function hexToRgba(hex: string, alpha: number = 1): string { const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); if (result) { const r = parseInt(result[1], 16); const g = parseInt(result[2], 16); const b = parseInt(result[3], 16); return `rgba(${r}, ${g}, ${b}, ${alpha})`; } return hex; }