FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 01, 2002
How Color Works

Ryan Detert

WebReview.com: How Color Works

Accurate Color on the Web

To understand how your computer creates colors for display, we need to first understand a little about the hexadecimal numbering system. Then we'll cover how a system uses hexadecimal values to call a triplet of primary colors to tune a pixel just right to display one of 16 million colors.

Hexadecimal Numbers

The hexadecimal system is a base 16 number system. We're used to a base 10 number system. What does this mean? Well, it means that you have six extra digits to deal with. We write every numbering system basically the same way, using a series of single digits to reach the base number (for example, 10 or 16), where we add a second digit.

In the base 10 numbering system, decimal, we use the digits from 0 to 9 to write all our numbers. In base 16, hexadecimal, we use the digits of 0 through F, where A=10 decimal, B=11 decimal, C=12 decimal, D=13 decimal, E=14 decimal, and F=15 decimal.

So while we count like this in decimal:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20...

We count this way in hexadecimal:

0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14...

Reading larger hexadecimal numbers is done the same way that you read in decimal, although you may not realize it. In the decimal system that we use, everything is a power of 10, even though we don't really think of it in this way.

For example, the number 16345 in decimal is really:

1 x 104 + 6 x 103 + 3 x 102 + 4 x 101 + 5 x 100 = 16345

So, in hexadecimal, this number would be 3FD9 (and remember that F=15 decimal and D=13 decimal), which may also be expressed as:

3 x 163 + F x 162 + D x 161 + 9 x 160 = 3FD9

Now that you have the general idea of hexadecimal, you may ask why we bother to have a different system. Computer gurus long ago decided to make hexadecimal the standard for computer languages, even though the computer itself operates in a base 2 system (0,1). If you have ever done any lower level programming you will realize that hexadecimal is easier to read because it also allows us to write huge decimal numbers using fewer digits. I will not go into how to convert between bases because it is beyond the scope of this tutorial and all that you really need is a calculator for converting and an understanding of what hexadecimal is.

RGB (Red, Green and Blue) Triplets

Monitors display everything via pixels, which are tiny dots on the screen that are composed of a red, green, and blue part. In fact, these colors are the also the primary colors of our color spectrum. All colors that we see are composed of a mixture of wavelengths of red, green, and blue light. In order to color in a pixel, we can adjust the intensities of the red, green, and blue parts of the pixel to form different colors. For example, if all of these parts are turned off the pixel is black. If all of them are on, the pixel is white. And if only the blue part of the pixel is turned on the pixel will be blue.

HTML, like many computer languages, relies on RGB (red, green, blue) triblets to assign color values to a pixel. An RGB triplet, for our purposes, will be 3 bytes, which is 1 byte for each component of the RGB triplet. Since there are 8 bits in a byte, an RGB triplet is also 24-bit color.

The range of a single byte is from between 0-255 in decimal, which is equivalent to 0-ff in hexadecimal. In HTML you may have seen something like this:

<font color="#FF12AC">

This color value is actually an RGB triplet that is being read in hexadecimal by the computer. It's six digits long, two digits for each byte. In this HTML RGB triplet,

  • the first two digits (ff) represent the intensity of the red component of the pixel, which is at full strength because a byte cannot be greater than FF.
  • The next two digits (12) represent the intensity of the green component of the pixel, it is fairly low.
  • The last two digits (ac) represent the intensity of the blue part of the pixel, and it's fairly high.

The end result is a pink pixel.

In most programming languages, you must indicate that a string of numbers is hexadecimal. In JavaScript, for example, you have to precede hexadecimal numbers with 0x to avoid confusion with decimal values. To write the previous RGB triplet using JavaScript, we would use 0xFF12AC. For example, in JavaScript we can make the background color of a page pink by using:

document.bgColor = 0xFF12AC;

By using an RGB triplet, we are able to use 256 shades of red, green, and blue to make our colors, this works out to be somewhere around 16.4 million colors!

Web Palette

That said, our range for anything that will display in a browser is a bit smaller. Netscape Navigator and Microsoft Internet Explorer rely on a palette of 216 colors, which is all the Microsoft and Macintosh platforms have in common.

For more details on this browser-safe palette, see Bob Schmitt's web color primer from the Nov. 18, 1997, issue of Web Review.


Tools for Color Management
How Color Works

RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:
    MEDIA CENTER  more
    NetSeminar
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
    Modernize your Development by Moving Build and Code Quality Upstream
    Moderated by Jon Erickson, Editor-in-Chief of Dr. Dobb's, this interactive panel discussion brings industry experts Anders Wallgren, CTO of Electric Cloud and Gwyn Fisher, CTO of Klocwork together for a candid discussion of the cost savings, productivity and quality benefits that can be achieved by stabilizing builds and code quality as early in the development cycle as possible.

    The reality of today's development environment - geographically distributed teams, the use of Agile development practices, increasing application complexity, etc. - is straining the viability of the traditional coding, build and release process. To stay ahead of the curve, development teams are modernizing their approach to dealing with these issues, and as a result are achieving new levels of development productivity. Register for the webcast.
    Date: Wednesday, July 15, 2009
    Time: 11 am PT/2 pm ET
                                   
    INFO-LINK

    Resource Links: