Hex to RGB

    0

    0

    colorhexrgb
    Color snippets

    Convert a hex colour to RGB.

    Filename pattern: *.js

    const hexToRGB = (hex) => {
      if (hex.charAt(0) === '#') {
        hex = hex.substring(1);
      }
    
      if (hex.length !== 3 && hex.length !== 6) {
        return false;
      }
    
      if (hex.length === 3) {
        hex = hex.split('').map(c => c.repeat(2)).join('');
      }
    
      const red = parseInt(hex.substring(0, 2), 16);
      const blue = parseInt(hex.substring(4, 6), 16);
      const green = parseInt(hex.substring(2, 4), 16);
    
      return { red, green, blue };
    };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.