How does HTML parse <font color="testing">?
This question already has an answer here:
What it's actually doing is splitting it into the RGB values, not the hex color value. So you're not creating #0e0000
, you're creating RGB(0e0, 000, 000)
. Since we know that 000
is simply 0
, we only have to look at the 0e0
part of it. From here, you need to remove the leading 0
s down to two digits if there are more than 2 digits, and then truncate to the two left digits in the number, which gives you e0
. When you convert that from hex to decimal, you end up with e0 = 224
. What this gives you is RGB(224, 0, 0)
, or a mostly red color.
More examples:
eesting => ee00000 => ee0 000 000 => RGB(ee0, 000, 000) => RGB(ee, 00, 00) => RGB(238, 0, 0)
eeeting => eee0000 => eee 000 000 => RGB(eee, 000, 000) => RGB(ee, 00, 00) => RGB(238, 0, 0)
eeeeing => eeee000 => eee e00 000 => RGB(eee, e00, 000) => RGB(ee, e0, 00) => RGB(238, 224, 0)
eefeefeef => eefeefeef => eef eef eef => RGB(eef, eef, eef) => RGB(ee, ee, ee) => RGB(238, 238, 238)
teeteetee => 0ee0ee0ee => 0ee 0ee 0ee => RGB(0ee, 0ee, 0ee) => RGB(ee, ee, ee) => RGB(238, 238, 238)
0f0f0f => 0f0f0f => 0f 0f 0f => RGB(0f, 0f, 0f) => RGB(0f, 0f, 0f) => RGB(15, 15, 15)
tftftf => 0f0f0f => 0f 0f 0f => RGB(0f, 0f, 0f) => RGB(0f, 0f, 0f) => RGB(15, 15, 15)
ttfttfttf => 00f00f00f => 00f 00f 00f => RGB(00f, 00f, 00f) => RGB(0f, 0f, 0f) => RGB(15, 15, 15)
Yes you are right it is using following parsing algoritham with following steps First, remove any hash-marks, then replace any non-hexadecimal characters (0-9a-f) with 0's.
Eg: #DIXIT becomes D0000.
For lengths 1-2, right pad to 3 characters with 0's.
Eg: "0F" becomes "0F0", "F" becomes "F00".
For length 3, take each digit as a value for red, green, or blue, and prepend a 0 to that value.
Eg: "0F0" becomes RGB( 0, F, 0), which becomes RGB( 00, 0F, 00) or 000F00.
Any value shorter than 4 digits long is done at this point.
For lengths 4 and longer, the field is right-padded with 0's to the next full multiple of 3. This step is important for longer fields.
Eg: "0F0F" becomes "0F0F00"
Next, the string is broken into three even parts, representing red, green and blue, from left to right.
"0F0F00" behaves as expected, becoming RGB(0F, 0F, 00). Any string of 6 characters is done at this point.
To verify above thing click here
To test algoritham check for following sample you will get same result
<body bgcolor="DIXIT">
<body bgcolor="D00000">
Following steps will be perform to parse dixit
上一篇: 是否有这样的事情分钟