All files / src/themes utils.ts

63.63% Statements 7/11
50% Branches 2/4
100% Functions 1/1
63.63% Lines 7/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23            12x     12x   12x         12x 12x 12x   12x    
/**
 * Convert a hex color to an rgba() string.
 * Supports #RGB, #RRGGBB, and #RRGGBBAA (alpha byte in the hex is ignored;
 * use the `alpha` argument instead).
 */
export function hexToRgba(hex: string, alpha: number): string {
  Iif (!/^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(hex)) {
    throw new Error(`Invalid hex color: ${hex}`);
  }
  const h = hex.slice(1);
  let r: number, g: number, b: number;
  Iif (h.length === 3) {
    r = parseInt(h.charAt(0) + h.charAt(0), 16);
    g = parseInt(h.charAt(1) + h.charAt(1), 16);
    b = parseInt(h.charAt(2) + h.charAt(2), 16);
  } else {
    r = parseInt(h.slice(0, 2), 16);
    g = parseInt(h.slice(2, 4), 16);
    b = parseInt(h.slice(4, 6), 16);
  }
  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}