non-frames link to top of chapter
HyperText Markup Language represents each color by a distinct triad of two-digit hexadecimal numbers, i.e. "11FF99". So in order to understand HTML color-codes, you first have to understand the hexadecimal numbering system.
Hexadecimal is a numerical system with a base of 16. This means there are sixteen characters instead of ten: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. That's like counting from 0 to 15, but replacing 10-15 with A-F. If you counted past F, you would roll over to the next digit, so that decimal sixteen (16d) would equal hexidecimal 10 (10h). Then you'd count 11, 12, 13, 14, all the way up to 1F, and then roll over to 20 (32 in decimal)... and so on.
In HTML, three hex numbers of two digits each (00-FF) define the brightness of each of your primary colors: red, green, and blue. Remember, you're not mixing pigment. When you paint, certainly the primary colors are yellow, red, and blue, but when you blend light on a computer screen things work a little differently. You have to make yellow now. You make it with red and green.
The background of this page is "006633". That's no red at all, a mid-low amount of green, and a teensy bit of blue to keep the green from being too bright. The text color has all the red and green you can stuff in it, and somewhat less blue -- "FFFF99" -- resulting in a light yellow. A full amount of all three colors, "FFFFFF", will give you white; a lack of any color, "000000", will result in black.
There are several ways of arriving at a desired color. If you have a lot of patience and/or familiarity with the red-green-blue system, you can just play around with the numbers. You can also visit a page like this one for starter ideas.
But if you need to take a background color into a program like Adobe Photoshop or some such so that you can create an image to match it, or if you want to make the background of your page match the image, you may need to convert between hexidecimal and decimal representations of that color. For such cases, I offer the following formulae.
This is probably more than you'll ever need to know. Being unable to find one ready-made, I sort of reinvented to wheel and came up with universal formulae for converting between any two base systems. They can be used for hexadecimal to decimal, decimal to binary, binary to octal - whatever. You can use either formula in any case; it depends on which base you find easiest to calculate in. The suggestions below assume that most of us prefer the decimal system, but I dunno, maybe you think in octal.
If the following is too geeky for you, just page down. I've got shortcuts for those of you who don't want to do the math.
Conversion by Multiplication
Use TARGET base for calculations. (Useful for converting base 16 to base 10.)Formula:
Ct = DnBn + Dn-1Bn-1 + Dn-2Bn-2 + ... + D0B0
where Ct = "converted-to", or TARGET number;
Cf = "convert-from" or GIVEN number;
B = base of Cf;
Dn = digit of Cf in the Bn place,
so that digits of Cf are Dn, Dn-1, Dn-2, ... D0.
First, I probably need to untangle all those "Dn" and "Bn" things for you.
We'll look at the decimal system first. You're probably used to talking about "ones" digits, "tens" digits, and "hundreds" digits, right? If I gave you the number 42957, you could tell me that there's a 9 in the hundreds place, a 7 in the ones place, and so on. But what if I gave you the hexidecimal number 42E5F? It would be inaccurate to say that E was in the hundreds place, because that digit does not represent hundreds; it represents blocks of 256.
This becomes easier if you think about digit places in terms of exponents. In the decimal or base 10 system, when we talk about a digit being in the hundreds place, we're really saying that the digit stands for blocks of 102. The ones place is better thought of as the 100 place (any number raised to the exponent of zero equals 1).
In the hexidecimal or base 16 system, the "ones" place is really 160. Instead of a "tens" place, you'll have digits in the "sixteens" place, or 161. Then you'd have the 162 place, which counts blocks of two hundred and fifty-six.
THEREFORE, each digit of a number in the base "B" system can be described by an exponent of B.
Now we're ready to use the formula.
Given that Cf = 7E3 h (hexidecimal, base 16),
what is Ct in decimal (base 10)?Ct = DnBn + Dn-1Bn-1 + Dn-2Bn-2 + ... + D0B0
Ct = D2B2 + D1B1 + D0B0
Cf is a three-digit number; notice that "n" will always be one less than the amount of digits in Cf.Ct = (7 x 162) + (E x 161) + (3 x 160)
Ct = (7 x 256) + (14 x 16) + (3 x 1)
Ct = 2019
(Formula assumes that you can handle single digits, and can for instance figure out that Eh = 14d without too much trouble.)
SUMMARY: Since each digit counts blocks of Bn, 7E3 can be rephrased as 7x162 + 14x161 + 3x160. And so we have the Conversion by Multiplication formula.
Still with me? Let's try the second formula. It's a little
less intuitive, but if you re-read it a few times you'll find
its the exact opposite of the first.
Conversion by Division Digits of Ct will be Dn, Dn-1, Dn-2, ... Do, Cf / Bn = Dn + Rn where B = base of Ct ("convert-to" or TARGET number);
Using GIVEN base for calculations. (Handy for converting from decimal
to hexidecimal.)
arrived at thus:
Rn / Bn-1 = Dn-1 + Rn-1
Rn-1 / Bn-2 = Dn-2 + Rn-2
...
R / B0 = D0 + 0
Bn = highest power of B that will divide into Cf ("convert-from" or GIVEN number)
R = remainder
and Dn = digit of Ct in Bn place.
So if you had a decimal number of 999, and you wanted to convert it to hexadecimal using this formula, here's what you'd do.
The highest power of 16 that can divide into 999 is 162,
or 256. So right from the beginning you know that your solution
will have three digits: 162, 161, and 160.
You also know your first calculation will look like this:
999 / 256 = 3 + R231 (a remainder of 231)
Now you know your first hexadecimal digit will be 3 -- in other words, there are three blocks of 162 in the decimal number 999.
Next you'd divide your remainder by 161:
231 / 16 = 14 + R7
There are fourteen blocks of 161 in the decimal number 231, so your second digit will be E, the hexadecimal rendering of 14d.
Lastly, you would divide that remainder by 160, but since 160 = 1, and anything divided by 1 results in itself, you might as well not bother. Your final, "ones" place digit will be 7.
Ct = 3E7
If the foregoing gobbledygook just doesn't seem worth it, you can try something else:
That page will also shove formulae at your throat, but you don't have to eat the stuff or even slog through it. Right at the top is a mindbogglingly simply javascript form. Plug your number in the appropriate slot, click on another blank, and watch its counterparts in several other base systems pop up.
For those of you with Windows 95 or later, your calculator accessory will do the same thing. Make sure "Scientific" View is selected. Click the radio button for the system you're converting FROM, enter the GIVEN number in the blank, then click the radio button for the system you're converting TO.
Another script from the JavaScript Planet collection, even more pertinent to the issue, can be found here:
This program specifically calculates the hexadecimal code of the color that results when you enter the red, green, and blue components in decimal numbers from 0-255. A pop-up window will actually show you what the color looks like.
Whatever method you choose, have fun!
© 1997, 1998 Nicole J. LeBoeuf