const HEX_REGEX = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/; /** * Validates a hex color string. Accepts 3-digit and 6-digit hex values * with a leading `#`. */ export function isValidHex(value: string): boolean { return HEX_REGEX.test(value.trim()); } /** * Returns an error message if the hex value is invalid, or undefined if valid. */ export function validateHex(value: string): string | undefined { if (!value.trim()) { return undefined; } if (!value.startsWith('#')) { return 'Color must start with #'; } if (!HEX_REGEX.test(value.trim())) { return 'Invalid hex format (e.g. #1A2B3C)'; } return undefined; }