How to convert RGB to HEX

RGB uses three decimal channels from 0 to 255. HEX stores the same channels as three two-digit base-16 pairs joined after a number sign.

Open the RGB to HEX converter

Convert each RGB channel

For every channel, divide by 16. The whole-number quotient is the first HEX digit and the remainder is the second. Values 10 through 15 become A through F.

RGB(255, 128, 0) → FF | 80 | 00 → #FF8000

  • 255: 15 remainder 15, so FF
  • 128: 8 remainder 0, so 80
  • 0: 0 remainder 0, so 00

Always pad a one-digit result with a leading zero. Decimal 5 becomes 05, not just 5.

Useful channel reference

DecimalHEX
000
5133
12880
15399
255FF

Uppercase and lowercase HEX letters are equivalent. Choose one convention and use it consistently in your project.

Add opacity as an alpha pair

Multiply opacity from 0 to 1 by 255, round to the nearest whole byte, then convert that byte to HEX and append it:

alpha byte = round(opacity × 255)

At opacity 0.5, the byte is round(127.5) = 128, which is 80. RGB(51, 102, 153) at 0.5 opacity becomes #33669980. The stored alpha is about 0.502 because byte values are discrete.

Validate the inputs

Each RGB channel must be a whole number from 0 through 255. Clamp or reject out-of-range data instead of silently producing invalid HEX. Opacity must stay between 0 and 1.

Short three-digit HEX is available only when both digits in every pair match. #336699 can become #369; #326699 cannot.

Convert with ToolPixels

The free RGB to HEX tool validates red, green, and blue as whole numbers, accepts optional opacity, and returns six or eight-digit HEX.

Convert RGB to HEX