DEV Community

Cover image for Convert RGB to Hex color codes in JavaScript
coder4life
coder4life

Posted on

Convert RGB to Hex color codes in JavaScript

Sometimes you have the RGB color code but you just really want the Hex code, right? Well converting RGB to Hex is so easy in JavaScript!

const rgb2hex = (r, g, b) => {
    return '#' +
        (
            (1 << 24) +
            (r << 16) +
            (g << 8) +
            b
        )
        .toString(16).slice(1);
};

console.log(rgb2hex(0, 0, 0));
Enter fullscreen mode Exit fullscreen mode

Latest comments (0)