RGB to Hex

    0

    0

    hexrgbcolor
    Color snippets

    Convert RGB colour to hex.

    Filename pattern: *.js

    const rgbToHex = (red, green, blue, preferShorthand = false) => {
      let colours = [red, green, blue].map(value => value.toString(16));
      let canBeCondensed = colours.every(value => value.charAt(0) === value.charAt(1));
    
      if (canBeCondensed && preferShorthand) {
        colours = colours.map(v => v.charAt(0));
      }
    
      return '#' + colours.join('');
    };
    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.