import formatHex from '~/format-hex'; import { MESSAGES } from '~/modules/constants'; import { extractAlphaFromHex } from '~/modules/hex-utils'; import { invariant } from '~/modules/invariant'; import { addAlpha } from '~/modules/utils'; import { isHex } from '~/modules/validators'; import { RGB } from '~/types'; /** * Convert HEX to RGB. * * @param input - The hex color string. * @returns The RGB color object. */ export default function hex2rgb(input: string): RGB { invariant(isHex(input), MESSAGES.inputHex); const hex = formatHex(input).slice(1); const alpha = extractAlphaFromHex(input); return addAlpha( { r: parseInt(hex.charAt(0) + hex.charAt(1), 16), g: parseInt(hex.charAt(2) + hex.charAt(3), 16), b: parseInt(hex.charAt(4) + hex.charAt(5), 16), }, alpha, ); }