为什么HTML认为“chucknorris”是一种颜色? [英] Why does HTML think “chucknorris” is a color?

查看:73
本文介绍了为什么HTML认为“chucknorris”是一种颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML中作为背景颜色输入时,某些随机字符串如何产生颜色?例如:

< body bgcolor =chucknorris> test< / body>

所有浏览器和平台上的红色背景 有趣的是, chucknorri 红色背景以及 chucknorr 会产生黄色背景。 发生了什么 here

这是一个从Netscape的日子中保留:


丢失的数字被视为0 [...]。例如#F0F0F0,F0F0F0,F0F0F,#FxFxFx和FxFxFx的值都是相同的。


它来自博客文章 关于微软的一点咆哮Internet Explorer的颜色解析 详细介绍了它的含义,包括不同颜色值的长度等。

如果我们依次应用规则从博客文章中,我们得到以下内容:


  1. 用0替换所有无效的十六进制字符

      chucknorris变成c00c0000000 


  2. 放到下一个可被3整除的字符总数(11 - > 12)

      c00c 0000 0000 

  3. 分成三组,分别代表RGB颜色对应的颜色分量:

      RGB(c00c,0000,0000)


  4. 将每个参数从右到下截断为两个字符


其中给出以下结果:

  RGB(c0,00,00)=#C00000或RGB(192,0,0)

下面是一个演示 bgcolor 属性的例子,用于产生这种惊人的颜色swatch:

< pre class =snippet-code-html lang-html prettyprint-override> < table> < TR> < td bgcolor =chucknorriscellpadding =8width =100align =center> chuck norris< / td> < td bgcolor =mrtcellpadding =8width =100align =centerstyle =color:#ffffff> Mr T< / td> < td bgcolor =ninjaturtlecellpadding =8width =100align =centerstyle =color:#ffffff> ninjaturtle< / td> < / TR> < TR> < td bgcolor =sickcellpadding =8width =100align =center>生病< / td> < td bgcolor =crapcellpadding =8width =100align =center>废话< / td> < td bgcolor =grasscellpadding =8width =100align =center> grass< / td> < / tr>< / table>

问题的另一部分;为什么 bgcolor =chucknorr会产生黄色?那么,如果我们应用这些规则,字符串就是:

  c00c00000 => c00 c00 000 => c0 c0 00 [RGB(192,192,0)] 

这给出浅黄色的金色。由于字符串以9个字符开始,因此我们保留第二个C,因此它以最终颜色值结束。



我最初遇到这个问题时有人指出你可以做 color =crap,好吧,它变成棕色。


How come certain random strings produce colors when entered as background colors in HTML? For example:

<body bgcolor="chucknorris"> test </body>

...produces a document with a red background across all browsers and platforms.

Interestingly, while chucknorri produces a red background as well, chucknorr produces a yellow background.

What's going on here?

解决方案

It's a holdover from the Netscape days:

Missing digits are treated as 0[...]. An incorrect digit is simply interpreted as 0. For example the values #F0F0F0, F0F0F0, F0F0F, #FxFxFx and FxFxFx are all the same.

It is from the blog post A little rant about Microsoft Internet Explorer's color parsing which covers it in great detail, including varying lengths of color values, etc.

If we apply the rules in turn from the blog post, we get the following:

  1. Replace all nonvalid hexadecimal characters with 0's

    chucknorris becomes c00c0000000
    

  2. Pad out to the next total number of characters divisible by 3 (11 -> 12)

    c00c 0000 0000
    

  3. Split into three equal groups, with each component representing the corresponding colour component of an RGB colour:

    RGB (c00c, 0000, 0000)
    

  4. Truncate each of the arguments from the right down to two characters

Which gives the following result:

RGB (c0, 00, 00) = #C00000 or RGB(192, 0, 0)

Here's an example demonstrating the bgcolor attribute in action, to produce this "amazing" colour swatch:

<table>
  <tr>
    <td bgcolor="chucknorris" cellpadding="8" width="100" align="center">chuck norris</td>
    <td bgcolor="mrt"         cellpadding="8" width="100" align="center" style="color:#ffffff">Mr T</td>
    <td bgcolor="ninjaturtle" cellpadding="8" width="100" align="center" style="color:#ffffff">ninjaturtle</td>
  </tr>
  <tr>
    <td bgcolor="sick"  cellpadding="8" width="100" align="center">sick</td>
    <td bgcolor="crap"  cellpadding="8" width="100" align="center">crap</td>
    <td bgcolor="grass" cellpadding="8" width="100" align="center">grass</td>
  </tr>
</table>

This also answers the other part of the question; why does bgcolor="chucknorr" produce a yellow colour? Well, if we apply the rules, the string is:

c00c00000 => c00 c00 000 => c0 c0 00 [RGB(192, 192, 0)]

Which gives a light yellow gold colour. As the string starts off as 9 characters, we keep the second C this time around hence it ends up in the final colour value.

I originally encountered this when someone pointed out you could do color="crap" and, well, it comes out brown.

这篇关于为什么HTML认为“chucknorris”是一种颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆