I'm kind of new to JavaScript and jQuery and now I'm facing a problem:
I need to post some data to PHP and one bit of the data needs to be the background color hex of div X.
jQuery has the css("background-color") function and with it I can get RGB value of the background into a JavaScript variable.
The CSS function seems to return a string like this rgb(0, 70, 255).
I couldn't find any way to get hex of the background-color (even though it's set as hex in CSS).
So it seems like I need to convert it. I found a function for converting RGB to hex, but it needs to be called with three different variables, r, g and b. So I would need to parse the string rgb(x,xx,xxx) into var r=x; var g=xx; var b=xxx; somehow.
I tried to google parsing strings with JavaScript, but I didn't really understand the regular expressions thing.
Is there a way to get the background-color of div as hex, or can the string be converted into 3 different variables?
How about this solution, function stringRGB2HEX returns a copy of the input string where all occurencies of colors in the format "rgb(r,g,b)" have been replaced by the hex format "#rrggbb".
This one converts also rgb colors in complex styles like
background
.A
style.background
value like:"none no-repeat scroll rgb(0, 0, 0)"
is easily converted into"none no-repeat scroll #000000"
by simply doingstringRGB2HEX(style.background)
With JQuery..
http://www.phpied.com/rgb-color-parser-in-javascript/
A JavaScript class that accepts a string and tries to figure out a valid color out of it. Some accepted inputs are for example:
Here you go, this will allow you to use $(selector).getHexBackgroundColor() to return the hex value easily :