将 RGB 转换为 ColorName 字符串 Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5486730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert RGB to ColorName String Javascript
提问by Marwan
I want to find out if a DOM Element has a style which is for example a color:Red. If this is defined on the inline style just like
我想知道 DOM 元素是否具有例如颜色:红色的样式。如果这是在内联样式上定义的,就像
<input type="text" style="color:Red">
I can get it easily with JavaScript
我可以用 JavaScript 轻松搞定
But how about if it's defined on a role - I mean as a css class and assign the class to the DOM Element
但是如果它是在一个角色上定义的 - 我的意思是作为一个 css 类并将该类分配给 DOM 元素
With javascript I know I can get the color with some method call computedStyle and give it the name of the property but it brings me the color in RGB numbers mode in Firefox and in IE it gets the color name
使用 javascript 我知道我可以通过一些方法调用来获得颜色并给它属性的名称,但它在 Firefox 中以 RGB 数字模式为我带来颜色,在 IE 中它获得颜色名称
my question here is how to convert RGB into a simple color name?
我的问题是如何将 RGB 转换为简单的颜色名称?
回答by SiburNY
Here is the library that will do it: Name That Color - http://chir.ag/projects/name-that-color/
这是可以执行此操作的库:Name That Color - http://chir.ag/projects/name-that-color/
回答by jaygooby
There's a sample function you could call in this answer:
您可以在此答案中调用一个示例函数:
Javascript function to convert color names to hex codes
It does a conversion from the name to the hex value.
它执行从名称到十六进制值的转换。
回答by Douglas
I think that these are the names supported in most browsers, you could store them in a dictionary and look them up: http://www.febooti.com/products/iezoom/online-help/html-color-names-x11-color-chart.html
我认为这些是大多数浏览器支持的名称,您可以将它们存储在字典中并查找它们:http: //www.febooti.com/products/iezoom/online-help/html-color-names-x11-颜色图表.html
The x11 color names are a superset of the ones defined in css.
x11 颜色名称是 css 中定义的颜色名称的超集。
回答by Torsten Becker
A color name to RGB converter look up table will help, start here: http://web.njit.edu/~walsh/rgb.html.
On the other hand someone might have build it before, so try to google it: https://encrypted.google.com/search?num=100&q=SlateGray1+SlateGray2+inurl%3A.js
RGB 转换器查找表的颜色名称会有所帮助,从这里开始:http: //web.njit.edu/~walsh/rgb.html。
另一方面,之前可能有人构建过它,所以尝试用谷歌搜索它:https: //encrypted.google.com/search?num=100&q=SlateGray1+SlateGray2+inurl%3A.js
回答by Evan Lévesque
Checkout this project https://github.com/evansque/colorazeand here is the node module for it https://www.npmjs.com/package/coloraze
检查这个项目https://github.com/evansque/coloraze,这里是它的节点模块https://www.npmjs.com/package/coloraze
回答by Micah Lindstrom
Check out the https://www.w3schools.com/lib/w3color.jsconversion script. It is very comprehensive, converting between named colors, hex, hsl, hwb, cmyk, and ncol It does not, however, support colors with alpha
查看https://www.w3schools.com/lib/w3color.js转换脚本。非常全面,可以在命名颜色、hex、hsl、hwb、cmyk、ncol之间进行转换,但是不支持带alpha的颜色
An easy web interface is also provided at https://www.w3schools.com/colors/colors_converter.asp
https://www.w3schools.com/colors/colors_converter.asp还提供了一个简单的 Web 界面