有一些地方可以获取HTML或X11 颜色名称及其 RGB 等效项的列表。例如,我们可以发现“Aquamarine”是“#70DB93”。大概浏览器知道映射。有没有办法使用 javascript 来询问浏览器并获取它支持的颜色名称列表(以及浏览器计划使用的 RGB)?
2 回答
这些是 Javascript 的元数据(它们用于 CSS 等),因此我怀疑它们是否可以以这种形式查询。
这是所有浏览器都应该知道的列表:CSS 颜色名称
从该页面:
The W3C HTML and CSS standards have listed only 16 valid color names:
aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive,
purple, red, silver, teal, white, and yellow.
编辑:既然你问了,我检查了这是否至少适用于 Safari。我能够做到这一点(我在几分钟内把它放在一起,忍受它):
<html>
<head>
<title>Color Test</title>
<script type="text/javascript">
function $(id) { return document.getElementById(id); }
function do_chocolate() {
$("foo").style.color = "chocolate";
alert($("foo").style.color);
}
</script>
</head>
<body>
<div id="foo">
This should change when you click below
</div>
<a href="#" onclick="do_chocolate();">Click me</a>
</body>
当我单击时,Safari 会向我显示此警报:
rgb(210, 105, 30)
I'm not familiar enough with Javascript to probe that color, but it looks like it can be done. If I were in a hurry on this project, I'd just stringize the color (like Safari did to display that alert to me) and grab each part. Since this is Javascript/DOM, however, I know there's a way to get in there and get each color component, but I don't know what it is. At least I've set you down the path, no?
Firefox 等浏览器支持的这些颜色名称不是 HTML 颜色名称,而是X11 颜色名称。
维基百科有这些颜色的列表和示例,因此您可以确定浏览器是否支持它们。
我会四处寻找,看看 Firefox(例如)在什么时候添加了这个支持,看看你是否可以通过 js 查询它。