DEV Community

Surhid Amatya
Surhid Amatya

Posted on

Colors in Programming, Part 1: RGB and Hex

Sooner or later you run into colors written like this:

#FF0000
#336699
#FFFFFF
#000000
Enter fullscreen mode Exit fullscreen mode

They look random. They aren't. Once you know two things, RGB and hexadecimal, you can read most of them on sight.

This is part 1 of 3. Part 2 covers alpha (transparency) and part 3 covers what happens when a transparent color sits on top of another one. Here I only want to answer one question: how does a computer store a color?

Three numbers

Most screens work with the RGB model: red, green and blue. Each channel gets a value from 0 to 255, so a color is just three numbers:

RGB(255, 0, 0)
Enter fullscreen mode Exit fullscreen mode

That's red at full strength, no green, no blue. RGB(0, 255, 0) is pure green and RGB(0, 0, 255) is pure blue.

Why 255?

Because a channel is usually stored in 8 bits. Eight bits give you 2⁸ = 256 values, and since counting starts at zero, they run from 0 to 255:

00000000 = 0
11111111 = 255
Enter fullscreen mode Exit fullscreen mode

Three channels of 256 levels each is 256 × 256 × 256 = 16,777,216 possible colors. That's where "24-bit color" comes from: 8 + 8 + 8 = 24 bits.

Why hex?

You could write RGB(255, 0, 0) everywhere, and people do. Hex is shorter, and it lines up neatly with the 8-bit values.

Hexadecimal is base 16. It uses the digits 0 to 9, then letters for the next six:

A = 10   B = 11   C = 12
D = 13   E = 14   F = 15
Enter fullscreen mode Exit fullscreen mode

Two hex digits cover 00 to FF, which is 0 to 255. That's exactly one channel. So a color is three pairs, one per channel:

#RRGGBB
Enter fullscreen mode Exit fullscreen mode

Reading some codes

Split #FF0000 into pairs and label them:

FF  00  00
R   G   B
Enter fullscreen mode Exit fullscreen mode

Red is FF (255), green is 0, blue is 0. Pure red.

#00FF00 is 00 FF 00, so pure green. #0000FF is 00 00 FF, so pure blue.

White is #FFFFFF, everything at maximum. Black is #000000, everything at zero. Mixing channels gives the rest: #FFFF00 is red plus green with no blue, which is yellow. #FF00FF is magenta and #00FFFF is cyan.

Decoding one you've never seen

Take #336699. Split it into 33 66 99 and convert each pair to decimal:

33 hex = 51
66 hex = 102
99 hex = 153
Enter fullscreen mode Exit fullscreen mode

So it's RGB(51, 102, 153), a muted blue. You don't have to remember that. You can work it out, and that's the skill worth having.

Converting hex by hand

Each pair is two digits in base 16, so the left digit counts sixteens:

3A = 3 × 16 + 10 = 58
FF = 15 × 16 + 15 = 255
Enter fullscreen mode Exit fullscreen mode

This is also why FF means "maximum". F is the biggest single digit (15), and FF is the biggest two-digit number (255). Every channel runs from 00 (off) to FF (full).

The same idea everywhere

RGB isn't tied to one language. You'll meet it in CSS, Android, iOS, Flutter, React Native, game engines and image libraries. The syntax changes:

color: rgb(255, 0, 0);
color: #FF0000;
Enter fullscreen mode Exit fullscreen mode

Flutter wants an integer like 0xFFFF0000. Underneath it's the same three numbers.

A warning about eight digits

Six-digit colors are RRGGBB pretty much everywhere. Eight-digit ones are a mess. Some platforms put the extra pair first (AARRGGBB), others put it last (RRGGBBAA). Part 2 is about that extra pair.

What to take away

Don't memorize hex codes. Remember the shape, #RRGGBB, with each pair running from 00 to FF. Then #FF0000 is red, #00FF00 green, #0000FF blue, #FFFFFF white and #000000 black, and anything else you can split and convert.

Top comments (0)