StringMash.com

RGB to hex converter

rgb() values to a hex colour code, with the working, a swatch and the contrast.

Conversion
16 characters
Updates as you type
Hex#FF8800
RGBrgb(255, 136, 0)
HSLhsl(32, 100%, 50%)
Nearest CSS namedarkorange
Contrast with white2.39:1 (fails)
Contrast with black8.77:1 (AAA)
The swatch and working are below

Show the steps
Aa blackAa white
  1. Write each number from 0 to 255 as two hex digits: divide by 16, and the answer and the remainder are the two digits (10 to 15 are A to F).
  2. Red: 255 ÷ 16 = 15 remainder 15, so FF.
  3. Green: 136 ÷ 16 = 8 remainder 8, so 88.
  4. Blue: 0 ÷ 16 = 0 remainder 0, so 00.
  5. Put them together after a #: #FF8800.
  6. Contrast: 2.39:1 against white and 8.77:1 against black, so black text reads better on it.

Using the converter

Type the red, green and blue values, each from 0 to 255. rgb(255, 136, 0), 255, 136, 0 and 255 136 0 all work, as do percentages and an rgba() opacity. The hex code appears straight away, ready to copy as #FF8800, with the HSL value, the nearest CSS colour name and the contrast with black and white text.

The box takes hex codes, hsl() and colour names too, so you can paste whatever a design tool or a stylesheet gave you. Nothing you type leaves the page.

How RGB becomes hex

Each of the three numbers becomes two hex digits. Divide it by 16. The whole-number answer is the first digit and the remainder the second, with 10 to 15 written as A to F. For 136, 136 ÷ 16 is 8 remainder 8, so 88. For 255 it's 15 remainder 15, FF, and for 0 it's 00. Put the three pairs together after a #, red first, and 255, 136, 0 becomes #FF8800.

Every value needs both digits, so 5 is 05, not 5. Otherwise the pairs would run into each other. An opacity, as in rgba(255, 136, 0, 0.5), is multiplied by 255 and added as a fourth pair, so 0.5 × 255 rounds to 128, which is 80, giving #FF880080.

HSL and CSS names

HSL describes the same colour as hue, saturation and lightness. The hue is an angle round the colour wheel (0 red, 120 green, 240 blue), saturation how strong the colour is, and lightness how far from black to white. It's easier to adjust by hand. To make a darker shade, lower the lightness and keep the rest.

CSS has 148 colour names, from aliceblue to yellowgreen. If the colour is one of them exactly, its name is shown; otherwise the nearest one is, which helps when describing a colour in words. Names, hex, rgb() and hsl() can all be typed into either box.

Contrast with white and black

The contrast ratios show whether text will be readable on the colour. The Web Content Accessibility Guidelines ask for at least 4.5:1 between normal text and its background (level AA), 3:1 for large text, and 7:1 for the stricter AAA level. The ratio runs from 1:1, the same colour, to 21:1, black on white.

The ratio comes from each colour's relative luminance, how bright it looks, which weights green most and blue least because eyes are most sensitive to green. That's why yellow #FFFF00 is fine behind black text but nearly unreadable behind white, while blue #0000FF is the other way round.

Questions

What is rgb(0, 0, 0) in hex?

#000000, black. rgb(255, 255, 255) is #FFFFFF, white.

Why does each value stop at 255?

Each channel is stored in one byte, 8 bits, which holds 256 values, 0 to 255. Two hex digits hold exactly the same range, 00 to FF.

Can I use the short three-digit form?

Only when each pair is a doubled digit. #FF8800 can be written #F80, but #FF8801 can't be shortened.

What about numbers above 255 or with decimals?

Above 255 isn't a valid channel, so the converter says which one is out of range. Decimals and percentages are rounded to the nearest whole number.

Sources

Added and reviewed . What's new