Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC

How HEX Colors Work

Rate me:
Please Sign up or sign in to vote.
4.88/5 (9 votes)
9 Feb 2015CPOL2 min read 19.8K   3   11
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)


Written By
Software Developer
Norway Norway
My name is Gjermund Bjaanes. I am 25 years old, born and raised in Norway. I am a developer, geek and enthusiast in general. I’m passionate about programming and software engineering in general. Currently I focus on web development and mobile development on my free time.

I am a software engineer and developer by trade and by passion.

I have always had a very enthusiastic interest for computers, dated to when I was little. It became an obsession for programming, IT, gadgets and new technology. I love being a developer, and that is why I do it as much as I can!

Other than that; In my spare time I like to code, read, cook, hang out with friends and to work out.

Comments and Discussions

 
QuestionWhere to get all color Hex codes and Their respective names ? Pin
Deepanshu Seth20-Jul-22 0:24
Deepanshu Seth20-Jul-22 0:24 
QuestionMy vote of 5 Pin
VE211-Feb-15 4:02
VE211-Feb-15 4:02 
QuestionInteresting Subject Pin
Ken of Kendoria10-Feb-15 8:22
professionalKen of Kendoria10-Feb-15 8:22 
AnswerRe: Interesting Subject Pin
Gjermund Bjaanes10-Feb-15 20:24
professionalGjermund Bjaanes10-Feb-15 20:24 
QuestionSorry, but my vote is 0 Pin
Leonid Shikhmatov10-Feb-15 4:31
Leonid Shikhmatov10-Feb-15 4:31 
AnswerRe: Sorry, but my vote is 0 Pin
Frank T. Clark10-Feb-15 10:29
professionalFrank T. Clark10-Feb-15 10:29 
AnswerRe: Sorry, but my vote is 0 Pin
jediYL10-Feb-15 19:54
professionaljediYL10-Feb-15 19:54 
AnswerRe: Sorry, but my vote is 0 Pin
Gjermund Bjaanes10-Feb-15 20:27
professionalGjermund Bjaanes10-Feb-15 20:27 
GeneralMy vote of 5 Pin
Volynsky Alex9-Feb-15 13:29
professionalVolynsky Alex9-Feb-15 13:29 
GeneralRe: My vote of 5 Pin
Gjermund Bjaanes9-Feb-15 19:53
professionalGjermund Bjaanes9-Feb-15 19:53 
GeneralRe: My vote of 5 Pin
Volynsky Alex10-Feb-15 9:40
professionalVolynsky Alex10-Feb-15 9:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.