DEV Community

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

Posted on

8 3

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

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay