This document has been superseded by HTML5. See the rules for parsing a legacy color value definition and the rendering section.
Unless otherwise stated, all elements defined or mentioned in this specification are in the http://www.w3.org/1999/xhtml
namespace, and all attributes defined or mentioned in this specification have no namespace (they are in the per-element partition).
RFC2119
Comparing two strings in a ASCII case-insensitive manner means comparing them exactly, codepoint for codepoint, except that the characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) and the corresponding characters in the range U+0061 .. U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) are considered to also match.
The following attributes are color attributes:
alink
, bgcolor
, link
, text
, and vlink
on the body
element.
color
on the font
element.
bordercolor
on the frame
element.
bordercolor
on the frameset
element.
color
on the hr
element.
bgcolor
on the marquee
element.
bgcolor
and bordercolor
on the table
element.
bgcolor
on the tbody
element.
bgcolor
on the td
element.
bgcolor
on the tfoot
element.
bgcolor
on the th
element.
bgcolor
on the thead
element.
bgcolor
on the tr
element.
When a user agent is to extract the color of a color attribute, it must use the rules for parsing color strings, using the attribute's value as the string being parsed. If the algorithm returned an error, the user agent must act as if the attribute was absent. Otherwise, the user agent must process the string returned by the algorithm as a <color> as defined in the CSS Color Module Level 3 specification. [CSS3COLOR]
The rules for parsing color strings are given in the following algorithm. When invoked, the steps must be followed in the order given, aborting at the first step that returns a value. This algorithm will either return a string or an error.
Browsers act on UTF-16 code units here — not Unicode codepoints. Though HTML5 should probably address this throughout.
Let input be the string being parsed.
If input is the empty string, return an error.
If input matches one of the color keywords (in an ASCII case-insensitive manner), then return input.
If input's length is 4 and the first character is a U+0023 NUMBER SIGN ("#") and the remaining characters are in the ranges U+0030 DIGIT ZERO .. U+0039 DIGIT NINE, U+0061 LATIN SMALL LETTER A .. U+0066 LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER A .. U+0046 LATIN CAPITAL LETTER F, then return input.
If input's length is greater than 128, truncate input after the 128'th character.
If the first character in input is a U+0023 NUMBER SIGN ("#"), remove it from input.
Replace every character in input that is not in the ranges U+0030 DIGIT ZERO .. U+0039 DIGIT NINE, U+0061 LATIN SMALL LETTER A .. U+0066 LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER A .. U+0046 LATIN CAPITAL LETTER F with a U+0030 DIGIT ZERO character.
If input is the empty string, append a U+0030 DIGIT ZERO character.
While input's length is not a multiple of 3, append a U+0030 DIGIT ZERO character to input.
Let segments be an array with the length 3.
Split input into three strings with equal length and append them to segments.
If the first item's length in segments is greater than 8, then truncate each item in segments so that the trailing 8 characters remain.
If the first item's length in segments is 1, prepend a U+0030 DIGIT ZERO character to each item in segments. Otherwise, follow these substeps:
While the first item's length in segments is greater than 2 and the first character in each item in segments is a U+0030 DIGIT ZERO character, then loop through each item in segments and remove the first character.
If the first item's length in segments is greater than 2, truncate each item in segments after the second character.
The color keywords used by the algorithm above are those in the following list:
activeborder
activecaption
aliceblue
antiquewhite
appworkspace
aqua
aquamarine
azure
background
beige
bisque
black
blanchedalmond
blue
blueviolet
brown
burlywood
buttonface
buttonhighlight
buttonshadow
buttontext
cadetblue
captiontext
chartreuse
chocolate
coral
cornflowerblue
cornsilk
crimson
cyan
darkblue
darkcyan
darkgoldenrod
darkgray
darkgreen
darkgrey
darkkhaki
darkmagenta
darkolivegreen
darkorange
darkorchid
darkred
darksalmon
darkseagreen
darkslateblue
darkslategray
darkslategrey
darkturquoise
darkviolet
deeppink
deepskyblue
dimgray
dimgrey
dodgerblue
firebrick
floralwhite
forestgreen
fuchsia
gainsboro
ghostwhite
gold
goldenrod
gray
graytext
green
greenyellow
grey
highlight
highlighttext
honeydew
hotpink
inactiveborder
inactivecaption
inactivecaptiontext
indianred
indigo
infobackground
infotext
ivory
khaki
lavender
lavenderblush
lawngreen
lemonchiffon
lightblue
lightcoral
lightcyan
lightgoldenrodyellow
lightgray
lightgreen
lightgrey
lightpink
lightsalmon
lightseagreen
lightskyblue
lightslategray
lightslategrey
lightsteelblue
lightyellow
lime
limegreen
linen
magenta
maroon
mediumaquamarine
mediumblue
mediumorchid
mediumpurple
mediumseagreen
mediumslateblue
mediumspringgreen
mediumturquoise
mediumvioletred
menu
menutext
midnightblue
mintcream
mistyrose
moccasin
navajowhite
navy
oldlace
olive
olivedrab
orange
orangered
orchid
palegoldenrod
palegreen
paleturquoise
palevioletred
papayawhip
peachpuff
peru
pink
plum
powderblue
purple
red
rosybrown
royalblue
saddlebrown
salmon
sandybrown
scrollbar
seagreen
seashell
sienna
silver
skyblue
slateblue
slategray
slategrey
snow
springgreen
steelblue
tan
teal
thistle
threeddarkshadow
threedface
threedhighlight
threedlightshadow
threedshadow
tomato
transparent
turquoise
violet
wheat
white
whitesmoke
window
windowframe
windowtext
yellow
yellowgreen
Those are the HTML4 color keywords, SVG color keywords, CSS2 UI Colors, and 'transparent' from [CSS3COLOR].
Thanks to Andy Lyttle and Lachlan Hunt for their useful comments.