All files / utils/misc floatColor.ts

95.45% Statements 21/22
100% Branches 0/0
100% Functions 1/1
95.45% Lines 21/22

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 431x   1x 6x             6x 6x 6x 6x     3x     1x 1x 1x   2x 2x 2x     3x     3x 3x 3x     6x     6x 6x    
const floatColorCache: { [key: string]: number } = {};
 
export default function floatColor(input: string): number {
  let val = input;
 
  // Is the color already computed?
  if (floatColorCache[val]) {
    return floatColorCache[val];
  }
 
  const original = val;
  let r = 0;
  let g = 0;
  let b = 0;
 
  if (val[0] === "#") {
    val = val.slice(1);
 
    if (val.length === 3) {
      r = parseInt(val.charAt(0) + val.charAt(0), 16);
      g = parseInt(val.charAt(1) + val.charAt(1), 16);
      b = parseInt(val.charAt(2) + val.charAt(2), 16);
    } else {
      r = parseInt(val.charAt(0) + val.charAt(1), 16);
      g = parseInt(val.charAt(2) + val.charAt(3), 16);
      b = parseInt(val.charAt(4) + val.charAt(5), 16);
    }
  } else if (val.match(/^ *rgba? *\(/)) {
    const matches = val.match(
      /^ *rgba? *\( *([0-9]*) *, *([0-9]*) *, *([0-9]*) *(,.*)?\) *$/
    );
    r = +matches[1];
    g = +matches[2];
    b = +matches[3];
  }
 
  const color = r * 256 * 256 + g * 256 + b;
 
  // Caching the color
  floatColorCache[original] = color;
  return color;
}