type RGB = { r: number; g: number; b: number; }; export default (hex: string): RGB | null => { const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; const longHex = hex.replace(shorthandRegex, (m: string, r: string, g: string, b: string) => r + r + g + g + b + b); const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(longHex); return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16), } : null; };