Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

How HEX Colors Work

4.88/5 (9 votes)
9 Feb 2015CPOL2 min read 21.4K  
How HEX colors work

Have you ever wondered what all those numbers and letters in a hex color means? What color is #AA3939 or #888888 and why is that combination giving the color that it gives? Well, it’s not magic, and it’s not random either. It’s actually  a pretty simple system. The only thing you have to really learn is how the hexadecimal number system works.

Number System

Hex is actually a number system, much like our normal decimal system. The difference is the base the system is based upon. Our normal number system is base 10. Hexadecimal is base 16. What that really means is what each position in a number is worth.

Each positions number worth is calculated with this formula:

   number \times base^{(position-1)}

Base 10 (Our Normal Decimal System)

Example:

   123

We always start from the right and go left. So the first position is worth:

   3 \times 10^0 = 3.

The second position is:

   2 \times 10^1 = 20

The third position is then worth

   1 \times 10^2 = 100

Then you add them all up together:

   3+20+100=123.

Easy as that.

123

Base 16 (Hex)

Example:

   B2C

 

You might have noticed that hex has letters and number in them. That is because they go from 0-15, and it would very difficult to make sense out of which number goes into which position. So we have 0-9 and then A=10, B=11 all the way up to F=15.
Again, we start from right to left. First number is now worth:

   12 \times 16^0 = 12

The second number is:

   2 \times 16^1 = 32

The third number is:

   11 \times 16^2 = 2816

The total number shown in our decimal system is then calculated by adding the numbers together:

   12+32+2816=2860.

b2c

Making Sense of the Numbers

A hex color is made up of three parts that represent the amount of Red, Green and Blue that are mixed together. These are split up using the first and second number for red, third and fourth number for green and fifth and sixth number for blue.

hexcolor

Knowing this, you can now specify the amount of red, green and blue in a color. Since there are two hex numbers for each color, you can have 0 amount of a color (00), all the way up to 255 (FF). Using this system, we can specify a large number of different colors, using the amount of red, green and blue (255*255*255=16 581 375 – to be exact).

Examples:

  • White is made when mixing the maximum number of red, green and blue together, so it will be #FFFFFF.
  • Black is when there are no amount of any of the colors #000000
  • Shades of grey can be done like this: #111111 (very dark grey – almost black). #CCCCCC (very light grey)

How 3 Digit HEX Works

Sometimes, people like to use a shorthand HEX for some colors with only 3 digits. The way this works is that each digit is representing their color (red, green, blue), but when you write #F00, this is actually the same as #FF0000. So, it's a shortcut for when you need some special colors like red, green, blue, black, white and grey scales.

Examples:

  • Black: #000 (#000000)
  • White: #FFF (#FFFFFF)
  • Grey: #888 (#888888)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)