All files / Rindu/utils hexToHsl.ts

100% Statements 34/34
100% Branches 14/14
100% Functions 1/1
100% Lines 33/33

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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56        9x       7x 7x 6x 6x 6x 6x 6x 6x 6x 6x     6x 6x 1x   5x 5x 5x   3x 3x   1x 1x   1x 1x   5x 3x       6x 6x 6x   6x 6x   6x 1x     5x    
export function hexToHsl<T extends boolean>(
  hex: string,
  valuesOnly?: T
): (T extends true ? number[] : string) | null;
export function hexToHsl(
  hex: string,
  valuesOnly?: boolean
): number[] | string | null {
  const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  if (!result) return null;
  let r = parseInt(result[1], 16);
  let g = parseInt(result[2], 16);
  let b = parseInt(result[3], 16);
  r /= 255;
  g /= 255;
  b /= 255;
  const max = Math.max(r, g, b),
    min = Math.min(r, g, b);
  let h,
    s,
    l = (max + min) / 2;
  if (max == min) {
    h = s = 0; // achromatic
  } else {
    const d = max - min;
    s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
    switch (max) {
      case r:
        h = (g - b) / d + (g < b ? 6 : 0);
        break;
      case g:
        h = (b - r) / d + 2;
        break;
      case b:
        h = (r - g) / d + 4;
        break;
    }
    if (h) {
      h /= 6;
    }
  }
 
  h = h ? Math.round(h * 360) : 0;
  s = Math.round(s * 100);
  l = Math.round(l * 100);
 
  const hslArray = [h, s, l];
  const hslString = `hsl(${h}, ${s}%, ${l}%)`;
 
  if (valuesOnly) {
    return hslArray;
  }
 
  return hslString;
}