Hex and RGB Colors

A hex color is three bytes of red, green and blue written in base 16. Here is how to read one, convert it to rgb() and HSL, and check its contrast.

How a hex color is built

A CSS hex color such as #1E90FF is three numbers written side by side. The pattern is #RRGGBB: two hex digits for red, two for green, two for blue. Each pair is one byte, so each channel runs from 00 (none of that light) to FF (full strength, 255 in decimal). Screens mix light, so all three at full strength give white and all three at zero give black.

Every hex digit stands for a value from 0 to 15, with A to F standing for 10 to 15. If that is unfamiliar, the page on number bases covers hex from the ground up.

Converting hex to RGB, step by step

Split the six digits into three pairs. For each pair, multiply the first digit by 16 and add the second. The results below were computed in Python with int(pair, 16).

Example: #1E90FF

ChannelPairWorkingDecimal
Red1E1 × 16 + 1430
Green909 × 16 + 0144
BlueFF15 × 16 + 15255

So #1E90FF is rgb(30, 144, 255): almost no red, a fair amount of green, and full blue, which reads as a bright sky blue.

Example: #FF8800

ChannelPairWorkingDecimal
RedFF15 × 16 + 15255
Green888 × 16 + 8136
Blue000 × 16 + 00

That gives rgb(255, 136, 0), an orange. Going the other way, convert each decimal value to two hex digits and join them. In Python, "%02X%02X%02X" % (255, 136, 0) returns FF8800. The zero padding matters: a channel of 8 must be written 08, not 8.

Shorthand and alpha

When each pair has two identical digits, CSS lets you write one digit per channel. #F80 expands by doubling each digit to #FF8800, the same orange as above. Shorthand only works for colors whose pairs repeat; #1E90FF has no short form.

An eighth and ninth digit add an alpha channel for opacity: #RRGGBBAA, or #RGBA in short form. 00 is fully transparent and FF is fully opaque. 80 is 128 out of 255, which is about 0.502, so #FF880080 is the orange at roughly half opacity.

rgb() and rgba()

The functional notation writes the same channels in decimal. rgb(255, 136, 0) equals #FF8800. The older rgba() adds a fourth value for alpha from 0 to 1, as in rgba(255, 136, 0, 0.5). Modern CSS also accepts a space-separated form with a slash before alpha, rgb(255 136 0 / 50%). All of these describe colors in the same sRGB space, so pick whichever your team finds easiest to read.

Named colors you can rely on

CSS defines a list of color keywords. These basic ones have fixed values. Note two that surprise people: lime is the pure green #00FF00, while the keyword green is a darker shade, and gray is #808080.

KeywordHexRGB
black#000000rgb(0, 0, 0)
white#FFFFFFrgb(255, 255, 255)
red#FF0000rgb(255, 0, 0)
lime#00FF00rgb(0, 255, 0)
blue#0000FFrgb(0, 0, 255)
yellow#FFFF00rgb(255, 255, 0)
aqua / cyan#00FFFFrgb(0, 255, 255)
fuchsia / magenta#FF00FFrgb(255, 0, 255)
gray#808080rgb(128, 128, 128)

HSL in brief

HSL describes a color by hue (an angle on the color wheel, 0 to 360 degrees), saturation and lightness (both as percentages). It is easier to adjust by hand: to make a color lighter, raise the lightness and leave the rest alone. Python's colorsys.rgb_to_hls converts RGB values scaled to 0 to 1; note it returns hue, lightness, saturation in that order. Rounded, the results are:

  • #FF8800 is hsl(32, 100%, 50%).
  • #1E90FF is hsl(210, 100%, 56%).

Because of rounding, converting a rounded HSL value back to hex can land one step away from the original. Keep the hex value as the source of truth if exact matching matters.

Checking text contrast

The Web Content Accessibility Guidelines (WCAG) define a contrast ratio from two colors' relative luminance. Each channel is scaled to 0 to 1, converted from the sRGB curve to linear light, and weighted 0.2126 for red, 0.7152 for green and 0.0722 for blue. The ratio is the lighter luminance plus 0.05, divided by the darker luminance plus 0.05. For level AA, normal-size body text needs a ratio of at least 4.5:1.

Running that formula in Python gives these ratios against a white background:

  • Black #000000 on white: 21.00:1, the maximum possible.
  • Gray #767676 (luminance 0.1812) on white: 4.54:1, which just clears 4.5:1.
  • #1E90FF on white: 3.24:1, which is too low for body text.

How to read an unfamiliar hex value

  1. Split it into pairs, expanding shorthand first if it has three or four digits.
  2. Look at the first digit of each pair; it carries most of the weight. High red and green with low blue leans yellow or orange.
  3. If all three pairs are equal, as in #808080, the color is a neutral gray.
  4. Check for a fourth pair. If present, it is alpha and the color may look different over different backgrounds.
  5. Before using it for text, compute its contrast ratio against the background instead of trusting your eye.