RGB to hex

JavaScript, String, Math · Nov 3, 2020

Converts the values of RGB components to a hexadecimal color code.

const RGBToHex = (r, g, b) =>
  ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
RGBToHex(255, 165, 1); // 'ffa501'

More like this

  • JavaScript Math

    A snippet collection of math helpers and algorithms implemented in JavaScript.

    Collection · 95 snippets

  • Hex to RGB

    Converts a color code to an rgb() or rgba() string if alpha value is provided.

    JavaScript, String · Oct 19, 2020

  • RGB to object

    Converts an rgb() color string to an object with the values of each color.

    JavaScript, String · Oct 22, 2020

  • RGB to array

    Converts an rgb() color string to an array of values.

    JavaScript, String · Jun 13, 2021