Menu

Colors in Programming, Part 1: RGB and Hex
📰

Colors in Programming, Part 1: RGB and Hex

DEV Community: css·Surhid Amatya·8 days ago
#SOiM7H4n
#devtocss#devto#dev#rrggbb#enter
Long article (684 words) — no AI summary yet.
Reading 0:00
15s threshold

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.