Colour Formats

RGB to hex

#3366cc
Hex

Nearest named colour: royalblue

Every format
Contrast
On white 5.37 Passes AA
On black 3.91 Fails AA
Local · one byte per channel, two digits each
ebf0fac2d1f099b3e67094db4775d12e5cb824478f1933660f1f3d

Turns an RGB triple into the hex string CSS and every design tool expect: each channel written as two base-16 digits, so 51, 102 and 204 becomes #3366cc. The HSL and OKLCH forms of the same colour sit alongside it, with the contrast the colour gives against white and black text.

How to use the rgb to hex

1 Set the colour: the picker, the hue, saturation and lightness sliders, or a hex code or CSS colour name typed in.
2 Read the rgb() line for the three channel values, and the hex line for the same colour in base 16.
3 Copy whichever of the four formats you need.
4 Converting three channel numbers you already have is one division each, worked through in the questions below: 51, 102, 204 is #3366cc.

The conversion happens one channel at a time and loses nothing on the way. Each of the three numbers runs 0 to 255, which is exactly the range two hexadecimal digits hold, so 51, 102, 204 is 33, 66, cc, and back again with no rounding anywhere. By hand it is a division: 204 over 16 is 12 remainder 12, and 12 is written c, so 204 is cc. Each channel stops at 255 because it is one byte, and a byte has no room for 256.

What varies is the shape of the RGB you arrived with, and three shapes turn up constantly. CSS takes both the legacy comma form, rgb(51, 102, 204), and the modern space-separated one, rgb(51 102 204), with alpha after a slash as rgb(51 102 204 / 50%). They mean the same thing, and the space form is the one to write now, because every newer colour function in CSS uses that shape. Percentages are legal too: rgb(20% 40% 80%) is a valid way to write almost the same colour, since a percentage maps onto the same 0 to 255 range rather than replacing it.

The third shape is the one that produces a wrong answer silently. Figma, SwiftUI, OpenGL, Unity and most graphics libraries hand you a channel as a float from 0 to 1 rather than an integer from 0 to 255. A value of 0.2 is not channel 2, it is 51, and pasting a float triple into a converter that expects bytes gives a colour indistinguishable from black. Multiply each by 255 and round to nearest before converting. Rounding to nearest rather than truncating matters more than it looks: truncation is how a colour drifts a step darker on every round trip through a tool chain.

Alpha is a fourth channel and it goes on the end of the hex string as a fifth and sixth digit: 80 is roughly 50 per cent, ff is fully opaque. Every current browser reads that eight-digit form. HSL and OKLCH are the two other lines shown here, and they are genuine transformations rather than a rewriting, so they can round by a step on the way back. HSL is the easy one to reason about and the one that misleads: its lightness is a position in the colour model, not a measure of how light the colour looks, so 50 per cent lightness is a vivid yellow and a deep blue at the same number. OKLCH is built so that figure means what you expect.

What people use it for

  • Moving a colour out of a design tool that reports RGB into CSS
  • Turning a printed brand-guideline RGB triple into a hex code
  • Reading a colour from a picker that only gives channel values
  • Checking a hand-typed hex code converts back to the right channels

Questions

Divide each channel by 16. The quotient is the first digit and the remainder is the second, with 10 to 15 written a to f. 204 gives 12 remainder 12, so cc; 51 gives 3 remainder 3, so 33.

MDN, CSS color values
Was this tool any good?
Internal signal only · I use it to find the tools worth rebuilding