Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RGB to ColorName String Javascript

Tags:

javascript

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

<input type="text" style="color:Red">

I can get it easily with 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

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

my question here is how to convert RGB into a simple color name?

like image 522
Marwan Avatar asked Mar 30 '11 13:03

Marwan


People also ask

How do you convert RGB to RGBA in Java?

Source => Target = (BGColor + Source) = Target. R = ((1 - Source. A) * BGColor. R) + (Source.

How are colors represented in JavaScript?

Colors are typically expressed through hexadecimal codes, either prefixed using a pound sign ( # ) or 0x to denote base 16 values. Combining these channels together, we end up with RGB codes, such as: 0xff0000 — Red. 0x00ff00 — Green.


3 Answers

Here is the library that will do it: Name That Color - http://chir.ag/projects/name-that-color/

like image 100
SiburNY Avatar answered Sep 18 '22 11:09

SiburNY


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.

like image 40
jaygooby Avatar answered Sep 22 '22 11:09

jaygooby


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

The x11 color names are a superset of the ones defined in css.

like image 42
Douglas Avatar answered Sep 21 '22 11:09

Douglas