export const DEFAULT_PINK_RGB = '202, 8, 234'; export const DEFAULT_BLUE_RGB = '4, 148, 247'; /** * Convert a 6-digit hex color to an "r, g, b" string. * Returns null for invalid input. */ export const hexToRgb = (hex: string): string | null => { if (!hex) return null; const cleanHex = hex.replace(/^#/, ''); if (!/^[0-9A-Fa-f]{6}$/.test(cleanHex)) return null; const r = parseInt(cleanHex.substring(0, 2), 16); const g = parseInt(cleanHex.substring(2, 4), 16); const b = parseInt(cleanHex.substring(4, 6), 16); return `${r}, ${g}, ${b}`; }; /** * Convert hex (3 or 6 digit) to "r,g,b" string (no spaces). * Used for inline rgba() CSS values. */ export const hexToRGB = (h: string): string => { let r: number | string = 0; let g: number | string = 0; let b: number | string = 0; if (h.length === 4) { r = `0x${h[1]}${h[1]}`; g = `0x${h[2]}${h[2]}`; b = `0x${h[3]}${h[3]}`; } else if (h.length === 7) { r = `0x${h[1]}${h[2]}`; g = `0x${h[3]}${h[4]}`; b = `0x${h[5]}${h[6]}`; } return `${+r},${+g},${+b}`; }; /** * Convert hex to rgba() CSS string. * Supports shorthand (#03F) and full (#0033FF) hex values. */ export const hexToRgba = (hex: string, alpha = 1): string => { const expandedHex = hex.length === 4 ? '#' + hex[1] + hex[1] + hex[2] + hex[2] + hex[3] + hex[3] : hex; const r = parseInt(expandedHex.substring(1, 3), 16); const g = parseInt(expandedHex.substring(3, 5), 16); const b = parseInt(expandedHex.substring(5, 7), 16); return `rgb(${r} ${g} ${b} / ${alpha})`; }; /** * Convert comma-separated RGB string to hex (e.g. "255,0,128" -> "#ff0080"). */ export const RGBToHex = (rgbColor: string): string => { const [r, g, b] = rgbColor.split(',').map(c => parseInt(c, 10)); return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); }; /** * Convert CSS rgb/rgba string to hex (e.g. "rgb(255 0 128 / 1)" -> "#ff0080"). */ export const rgbaToHex = (rgba: string): string => { const parts: RegExpMatchArray | null = rgba.match(/(\d+) \s*(\d+) \s*(\d+)/); if (!parts) { return '#FFFFFF'; } const r = parseInt(parts[1]).toString(16).padStart(2, '0'); const g = parseInt(parts[2]).toString(16).padStart(2, '0'); const b = parseInt(parts[3]).toString(16).padStart(2, '0'); return `#${r}${g}${b}`; };