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

问题描述

为什么某些随机字符串在 HTML 中作为背景颜色输入时会产生颜色?

Why do certain random strings produce colors when entered as background colors in HTML?

例如:

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

...在所有浏览器和平台上生成具有红色背景的文档.

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

另一方面,chucknorr 产生黄色背景

这是怎么回事?

推荐答案

这是 Netscape 时代的产物:

It’s a holdover from the Netscape days:

缺失的数字被视为 0[...].错误的数字被简单地解释为 0.例如,#F0F0F0、F0F0F0、F0F0F、#FxFxFx 和 FxFxFx 的值都是相同的.

它来自博客文章对 Microsoft Internet Explorer 的颜色解析的一点抱怨,其中详细介绍了它,包括不同长度的颜色值等.

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. 将所有无效的十六进制字符替换为 0:

  1. Replace all nonvalid hexadecimal characters with 0’s:

chucknorris becomes c00c0000000

  • 填充到下一个可被 3 整除的字符总数 (11 → 12):

  • Pad out to the next total number of characters divisible by 3 (11 → 12):

    c00c 0000 0000
    

  • 分成三个相等的组,每个分量代表 RGB 颜色的相应颜色分量:

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

    RGB (c00c, 0000, 0000)
    

  • 将每个参数从右边截断为两个字符.

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

    最后得到以下结果:

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

    这是一个示例,演示了 bgcolor 属性的作用,以产生这个惊人"的颜色样本:

    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>

    这也回答了问题的另一部分:为什么 bgcolor="chucknorr" 会产生黄色?好吧,如果我们应用规则,字符串是:

    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)]
    

    呈现淡黄色金色.由于字符串以 9 个字符开头,因此这次我们保留第二个C",因此它以最终颜色值结束.

    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.

    当有人指出您可以执行 color="crap" 时,我最初遇到了这个问题,但结果是棕色的.

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

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

  • 08-21 15:48