Advertisement

RGB to HEX Converter

Convert rgb colors to hex format instantly.

#

RGB to HEX Conversion

Converting RGB to HEX is a simple base-10 to base-16 conversion applied to each channel. Each decimal value (0โ€“255) becomes a two-digit hexadecimal pair, and the three pairs are concatenated with a # prefix.

Conversion Formula

When to Use HEX

How do I convert RGB to HEX in JavaScript?

Use .toString(16).padStart(2, '0') on each channel: const toHex = (r, g, b) => '#' + [r, g, b].map(v => v.toString(16).padStart(2, '0')).join(''); For example, toHex(255, 87, 51) returns #ff5733. Use .toUpperCase() if you prefer uppercase hex.

What is the HEX code for black and white?

Black = #000000 (R:0, G:0, B:0 โ€” no light). White = #FFFFFF (R:255, G:255, B:255 โ€” full light on all channels). Middle gray = #808080 (128, 128, 128). Pure red = #FF0000, pure green = #00FF00, pure blue = #0000FF.

Can I use RGB decimal values directly in CSS?

Yes โ€” color: rgb(255, 87, 51) is valid CSS. Modern CSS also supports the space-separated syntax without commas: rgb(255 87 51) and fractional percentages: rgb(100% 34% 20%). The HEX equivalent is purely a formatting preference โ€” both produce identical colors.