/ src / lib / utils / hex-to-rgb.ts
hex-to-rgb.ts
 1  /**
 2   * Converts a hex color string to an RGB object.
 3   * @param hex The hex color string.
 4   * @returns The RGB object.
 5   */
 6  export default function hexToRgb(hex: string) {
 7    const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
 8    return result
 9      ? {
10          r: parseInt(result[1], 16),
11          g: parseInt(result[2], 16),
12          b: parseInt(result[3], 16),
13        }
14      : null;
15  }