为什么十六进制数的前缀是 # 而不是 0x?
Why are hex numbers prefixed with # instead of 0x?
由于任何 RGB 值都只是一个 24 位整数,因此将颜色表示为单个十六进制数是完全合理的。由于十六进制数字的商定前缀是 0x,为什么我们改为引入 new 颜色前缀 (#)?
据我所知,十六进制表示法源自 X11(X Window 系统),其历史可追溯到 1987 年,甚至在 Linux 和 WWW 存在之前。
这是 Xlib 手册中的相关页面:
最终选择它的原因可能是基于 Unix 的系统是当时学术界的选择 OS,他们熟悉这种表示法,并考虑使用已经支持的东西而不是重新发明轮子。
当第一个网络浏览器问世时,它只是纯文本。一些浏览器为您提供了设置背景和文本颜色的选项,但它不是网页的一部分。但有些人想要背景颜色:
"Mosaic has the capability to change the background color of a page in the
prefs. I asked NCSA if it was possible for their program to read a "page
color" tag, and then set the page color appropriately. They told me that
would take a change in HTML specs and only then will they support it."
Bradford Bohonus (Sep 22 1994)
当然,HTML 背后的整个想法是将结构和表示分开,所以这不会发生。在 HTML 2.0(1995 年)发布之前,样式表 的想法很快就出现了。
有很多关于样式表的提议,一个早期的提议确实使用了 0x
十六进制表示法:
Rob Raisch (raisch@ora.com)
Thu, 10 Jun 1993 15:23:20 -0400
...
(fo) foreground= COLOR DEFAULT:''
Describes the recommended foreground color representation
for a character. Colors are specified as 'inherit', text
names, (eg. black, white, magenta), or as RGB color
values in hexidecimal, (eg. 0x000000, 0xffffff, 0xff00ff)
(ba) background= COLOR DEFAULT:''
Describes the recommended background color representation
for a character. Colors are specified as 'inherit', text
names, (eg. black, white, magenta), or as RGB color
values in hexidecimal, (eg. 0x000000, 0xffffff, 0xff00ff)
...
另一个名为 "Style Sheets for HTML" 的提议(Joe English,1994)建议如下:
All the colors used by a style sheet must be declared in the (optional) COLORS element. This element contains one or more COLOR elements, each of which specifies a single color.
COLOR elements have two required attributes: ID, a unique identifier, and RGB, which defines
the color. Colors are referenced by ID.
Colors are defined by their red, green, and blue components using the X11 hex notation: a pound
sign followed by 3, 6, 9 or 12 hexadecimal digits. The digits are interpreted as three groups of 1, 2,
3 or 4 half-bytes, the first specifying the red component, the second green, and the third blue. Hex
digits A through F may be upper or lower case.
<colors>
<color id=red rgb="#F00">
<color id=green rgb="#00FF00">
<color id=blue rgb="#000000000FFFFF">
<color id=grey rgb="#c0c0c0">
<color id=white rgb="#FFFFFF">
</colors>
虽然没有直接归因于 Joe English,但最终 #RGB 和 #RRGGBB 十六进制符号最终出现在 CSS1 草稿和最终推荐中,您可以在这里看到:https://www.w3.org/Style/CSS/history.html。
我个人认为,这可能与使用特定规则(#RGB vs #RRGGBB)解析文本的词法分析有关,而不是像 C/C++ 中那样表示原始数据源代码。在 CSS 规范草案中,它定义了解析 "hexcolor":
所需的规则
h [0-9a-fA-F]
hexcolor #{h}{h}{h}|#{h}{h}{h}{h}{h}{h}|#{h}{h}{h}{h}{h}{h}{h}{h}{h}
所以基本上他们想要一个表示颜色代码的文本定义,而现有的 X11 规范正好符合要求。由于像 0xCAFE
、$CAFE
或 CAFEh
这样的十六进制符号用于表示无法描述的原始数据,因此对那些已经存在的符号施加更严格的规则可能会导致混淆。
由于任何 RGB 值都只是一个 24 位整数,因此将颜色表示为单个十六进制数是完全合理的。由于十六进制数字的商定前缀是 0x,为什么我们改为引入 new 颜色前缀 (#)?
据我所知,十六进制表示法源自 X11(X Window 系统),其历史可追溯到 1987 年,甚至在 Linux 和 WWW 存在之前。
这是 Xlib 手册中的相关页面:
最终选择它的原因可能是基于 Unix 的系统是当时学术界的选择 OS,他们熟悉这种表示法,并考虑使用已经支持的东西而不是重新发明轮子。
当第一个网络浏览器问世时,它只是纯文本。一些浏览器为您提供了设置背景和文本颜色的选项,但它不是网页的一部分。但有些人想要背景颜色:
"Mosaic has the capability to change the background color of a page in the prefs. I asked NCSA if it was possible for their program to read a "page color" tag, and then set the page color appropriately. They told me that would take a change in HTML specs and only then will they support it."
Bradford Bohonus (Sep 22 1994)
当然,HTML 背后的整个想法是将结构和表示分开,所以这不会发生。在 HTML 2.0(1995 年)发布之前,样式表 的想法很快就出现了。
有很多关于样式表的提议,一个早期的提议确实使用了 0x
十六进制表示法:
Rob Raisch (raisch@ora.com)
Thu, 10 Jun 1993 15:23:20 -0400
...
(fo) foreground= COLOR DEFAULT:''
Describes the recommended foreground color representation
for a character. Colors are specified as 'inherit', text
names, (eg. black, white, magenta), or as RGB color
values in hexidecimal, (eg. 0x000000, 0xffffff, 0xff00ff)
(ba) background= COLOR DEFAULT:''
Describes the recommended background color representation
for a character. Colors are specified as 'inherit', text
names, (eg. black, white, magenta), or as RGB color
values in hexidecimal, (eg. 0x000000, 0xffffff, 0xff00ff)
...
另一个名为 "Style Sheets for HTML" 的提议(Joe English,1994)建议如下:
All the colors used by a style sheet must be declared in the (optional) COLORS element. This element contains one or more COLOR elements, each of which specifies a single color.
COLOR elements have two required attributes: ID, a unique identifier, and RGB, which defines the color. Colors are referenced by ID.
Colors are defined by their red, green, and blue components using the X11 hex notation: a pound sign followed by 3, 6, 9 or 12 hexadecimal digits. The digits are interpreted as three groups of 1, 2, 3 or 4 half-bytes, the first specifying the red component, the second green, and the third blue. Hex digits A through F may be upper or lower case.
<colors> <color id=red rgb="#F00"> <color id=green rgb="#00FF00"> <color id=blue rgb="#000000000FFFFF"> <color id=grey rgb="#c0c0c0"> <color id=white rgb="#FFFFFF"> </colors>
虽然没有直接归因于 Joe English,但最终 #RGB 和 #RRGGBB 十六进制符号最终出现在 CSS1 草稿和最终推荐中,您可以在这里看到:https://www.w3.org/Style/CSS/history.html。
我个人认为,这可能与使用特定规则(#RGB vs #RRGGBB)解析文本的词法分析有关,而不是像 C/C++ 中那样表示原始数据源代码。在 CSS 规范草案中,它定义了解析 "hexcolor":
所需的规则h [0-9a-fA-F]
hexcolor #{h}{h}{h}|#{h}{h}{h}{h}{h}{h}|#{h}{h}{h}{h}{h}{h}{h}{h}{h}
所以基本上他们想要一个表示颜色代码的文本定义,而现有的 X11 规范正好符合要求。由于像 0xCAFE
、$CAFE
或 CAFEh
这样的十六进制符号用于表示无法描述的原始数据,因此对那些已经存在的符号施加更严格的规则可能会导致混淆。