hex-cmyk

Converter

RGB to HEX Converter

Three numbers in, one six-character string out. The rules that trip people up are padding and shorthand, and both are covered below.

C
M
Y
K
SUM
HEX
RGB
HSL
CMYK

Need it the other way round?HEX to RGB converter

These values are mathematical approximations. Ink, paper, press calibration and colour profile all change the printed result — always approve colour from a physical proof. Full disclaimer.

How the conversion works #

// for each channel digit1 = floor(value / 16) digit2 = value mod 16   // 10–15 are written A–F // pad any single digit with a leading zero hex = "#" + pad(R) + pad(G) + pad(B)

A worked example #

Crimson, rgb(220, 20, 60).

  1. 220 ÷ 16 = 13 remainder 12. Thirteen is D, twelve is C, so red is DC.
  2. 20 ÷ 16 = 1 remainder 4. Green is 14.
  3. 60 ÷ 16 = 3 remainder 12. Blue is 3C.
  4. Joined together: #DC143C.
The padding trap

If green had been 4 rather than 20, the pair would be 04 — not 4. Drop that zero and the whole string shifts left, turning a valid colour into a different one or into nonsense. Every conversion function needs an explicit pad step, and it is worth writing a test for exactly this case.

Common RGB to hex values #

ColourRGBHEXCMYK
Black0, 0, 0#0000000, 0, 0, 100
White255, 255, 255#FFFFFF0, 0, 0, 0
Red255, 0, 0#FF00000, 100, 100, 0
Lime0, 255, 0#00FF00100, 0, 100, 0
Blue0, 0, 255#0000FF100, 100, 0, 0
Yellow255, 255, 0#FFFF000, 0, 100, 0
Cyan0, 255, 255#00FFFF100, 0, 0, 0
Magenta255, 0, 255#FF00FF0, 100, 0, 0
Dodger blue30, 144, 255#1E90FF88, 44, 0, 0
Navy0, 0, 128#000080100, 100, 0, 50
Teal0, 128, 128#008080100, 0, 0, 50
Forest green34, 139, 34#228B2276, 0, 76, 45
Olive128, 128, 0#8080000, 0, 100, 50
Orange255, 165, 0#FFA5000, 35, 100, 0
Crimson220, 20, 60#DC143C0, 91, 73, 14
Maroon128, 0, 0#8000000, 100, 100, 50
Purple128, 0, 128#8000800, 100, 0, 50
Hot pink255, 105, 180#FF69B40, 59, 29, 0
Silver192, 192, 192#C0C0C00, 0, 0, 25
Charcoal54, 69, 79#36454F32, 13, 0, 69

Questions #

Why does my channel value produce a single hex digit?

Any value below 16 converts to one digit, and it must be padded with a leading zero to keep the pairs aligned. 10 becomes 0A, not A. Getting this wrong is the most common bug when people write their own conversion function.

Can every RGB colour be shortened to three digits?

No. Only when each pair is a doubled digit. 255, 0, 170 gives #FF00AA which shortens to #F0A, but 30, 144, 255 gives #1E90FF which cannot be shortened without changing the colour.

Should I write hex in upper or lower case?

Either renders identically. Lower case is the more common convention in CSS codebases; upper case reads more clearly in design documentation. The only thing that matters is consistency within a project.

What if my values are outside 0 to 255?

They should be clamped to the range before converting. A value of 300 is not a brighter red, it is invalid, and different tools will handle it differently. This converter clamps rather than wrapping.