Hexadecimal Color Notation on the Web

Oct 13
21:00

2004

Stephen Bucaro

Stephen Bucaro

  • Share this article on Facebook
  • Share this article on Twitter
  • Share this article on Linkedin

... is granted for the below article to ... ... use for ezine, ... ... as free bonus or part of

mediaimage

----------------------------------------------------------
Permission is granted for the below article to forward,
reprint,Hexadecimal Color Notation on the Web Articles distribute, use for ezine, newsletter, website,
offer as free bonus or part of a product for sale as long
as no changes are made and the byline, copyright, and the
resource box below is included.
----------------------------------------------------------

Hexadecimal Color Notation on the Web

By Stephen Bucaro

When designing elements for your webpage, you will often
be called upon to specify a color. For example, the code
for a span shown below specifies that the color of the text
within the span will be yellow.

<span style="color:yellow;">Text</span>

Colors can be specified according to their names, for
example "yellow", "green", or "blue". In many cases, these
simple color names will work. But what if you want to
specify a more sophisticated color like "cornflowerblue"?
A particular browser may not recognize a particular color
name. It's more reliable to specify colors with an
"RGB triplet".

An RGB triplet specifies a color based upon the amounts of
red, green, and blue, on a scale from 0 to 255, required
to create the color. For example, to create the color
cornflowerblue you need red=100, green=149, and blue=237.
We could then specify the color of the text within a span
using the rgb function as shown below.

<span style="color:rgb(100,149,237);">Text</span>

This will work fine with style notation, but what if you
want to use straight html. Html doesn't recognize the rgb
function. In that case, you can specify color using
"hexadecimal" notation. Whereas the decimal numbering
system uses the characters 0 through 9 to get 10 values,
the hexadecimal numbering system uses the characters 0
through f to get 16 values. (After 9 the characters a, b,
c, d, e and f are used, as shown below.)

Decimal Hexidecimal Equivilants

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

On first appearance, this looks pretty simple but you need
two hexadecimal characters to represent all decimal values
from 0 to 255. When you increment decimal 9 by 1, you
change the 9 to 0 and put 1 in the ten's place. When you
increment hexidecimal F by one, you change the F to 0 and
put 1 in the "sixteens" place. Sometimes it's not easy to
convert between decimal and hexadecimal in your head.

RGB Triplet for Cornflowerblue

color dec hex
red 100 64
green 149 95
blue 237 ED

We could then specify the color of text within a span using
the hexadecimal notation as shown below.

<span style="color:#6495ed;">Text</span>

Note that when we indicate the use of hexadecimal notation
by placing a pound (#) sign in front of the number, and we
don't use commas to separate the color components.

If it's not easy to convert between decimal and hexadecimal
in your head, then how do you do it? You can use a
calculator that has a decimal to hexadecimal coversion
function, or you can learn to think in hexadecimal. For
example, what's the next number after CE? That would be CF.
what's the next number after CF? That would be D0. Which
hexadecimal number is higher 99 or B2? B2 would be higher
than 99. It gets easier with experience.

Here's Java Script code for a simple decimal to hexidecimal
color converter

function convert(decvalue)
{
var num = parseInt(decvalue);
if(num >= 0 && num < 256)
{
var hexnum = num.toString(16);
alert("#" + hexnum);
}
else {alert("Error!");}
}

It's important to use hexadecimal notation to specify
colors when you use DHTML with visual effects, because to
create a dynamic color change you have to increment or add
a value to a color. The vast amount of color specification
on webpages is in hexadecimal notation, so it would be
wise to become familiar with it.

----------------------------------------------------------
Resource Box:
Copyright(C) Bucaro TecHelp. To learn how to maintain
your computer and use it more effectively to design a Web
site and make money on the Web visit bucarotechelp.com
To subscribe to Bucaro TecHelp Newsletter visit
http://bucarotechelp.com/search/000800.asp
----------------------------------------------------------