const HEX_PATTERN = /^#?([0-9a-f]{6})$/i; export const isHexColor = (value: string) => HEX_PATTERN.test(value.trim()); // "3D6B4F" / "#3d6b4f" → "#3d6b4f"; anything else → null export function normalizeHex(value: string): string | null { const match = HEX_PATTERN.exec(value.trim()); return match ? `#${match[1]!.toLowerCase()}` : null; } // Relative luminance (WCAG); below ~0.4 a colour reads as dark and needs light content on it export function isDarkColor(hex: string): boolean { const normalized = normalizeHex(hex); if (!normalized) return false; const [r, g, b] = [1, 3, 5].map((start) => { const channel = parseInt(normalized.slice(start, start + 2), 16) / 255; return channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4; }) as [number, number, number]; return 0.2126 * r + 0.7152 * g + 0.0722 * b < 0.4; }