I get color value with jQuery with .css('color')
, and then I know color that it should be.
How can I compare color value that I get from jQuery with for example black color value?
I get color value with jQuery with .css('color')
, and then I know color that it should be.
How can I compare color value that I get from jQuery with for example black color value?
Here is an approach that should work on all browsers using JQuery:
<div id="dummy" style="display:none;"></div>
on your HTML page. (Creating the dummy element dynamically with JQuery does not work for named colors)$('#dummy').css('color','black');
i.e.
if($('#element').css('color') === $('#dummy').css('color')) {
//do something
}
What about...
if ($('#element').css('color') == 'rgb(0, 0, 0)')
{
// do something
}
Replace 0, 0, 0 with the red, green and blue values of the colour value you want to compare.
I had a similar problem where I had to toggle the bgnd color of an element. I solved it like this:
var color_one = "#FFA500";
var color_two = "#FFFF00";
function toggle_color(elem){
var bgcolor = elem.css("background-color");
elem.css("background-color", color_one); // try new color
if(bgcolor == elem.css("background-color")) // check if color changed
elem.css("background-color", color_two); // if here means our color was color_one
}
Here is my implementation of Mike's answer, in one function.
function compareColors(left, right) {
// Create a dummy element to assign colors to.
var dummy = $('<div/>');
// Set the color to the left color value, and read it back.
$(dummy).css('color', left);
var adjustedLeft = $(dummy).css('color');
// Set the color to the right color value, and read it back.
$(dummy).css('color', right);
var adjustedRight = $(dummy).css('color');
// Both colors are now adjusted to use the browser's internal format,
// so we can compare them directly.
return adjustedLeft == adjustedRight;
}
You don't need to actually add the elements to the DOM for this to work. Tested in IE8, IE10, FF, Chrome, Safari.
CSS Colors can appear in many formats - rgb
, rgba
, #hex
, hardly supported #hexalpha
, infamous named colors, and the special transparent
.
To compare any color to any color you need to normalize them first.
The colorValues
function found here (gist) or here (SO answer will always give you [r,g,b,a]
array of numeric values.
Then you can compare them like this:
var sameColor = colorValues(color1).join(',') === colorValues(color2).join(',');
colorValues function
// return array of [r,g,b,a] from any valid color. if failed returns undefined
function colorValues(color)
{
if (color === '')
return;
if (color.toLowerCase() === 'transparent')
return [0, 0, 0, 0];
if (color[0] === '#')
{
if (color.length < 7)
{
// convert #RGB and #RGBA to #RRGGBB and #RRGGBBAA
color = '#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3] + (color.length > 4 ? color[4] + color[4] : '');
}
return [parseInt(color.substr(1, 2), 16),
parseInt(color.substr(3, 2), 16),
parseInt(color.substr(5, 2), 16),
color.length > 7 ? parseInt(color.substr(7, 2), 16)/255 : 1];
}
if (color.indexOf('rgb') === -1)
{
// convert named colors
var temp_elem = document.body.appendChild(document.createElement('fictum')); // intentionally use unknown tag to lower chances of css rule override with !important
var flag = 'rgb(1, 2, 3)'; // this flag tested on chrome 59, ff 53, ie9, ie10, ie11, edge 14
temp_elem.style.color = flag;
if (temp_elem.style.color !== flag)
return; // color set failed - some monstrous css rule is probably taking over the color of our object
temp_elem.style.color = color;
if (temp_elem.style.color === flag || temp_elem.style.color === '')
return; // color parse failed
color = getComputedStyle(temp_elem).color;
document.body.removeChild(temp_elem);
}
if (color.indexOf('rgb') === 0)
{
if (color.indexOf('rgba') === -1)
color += ',1'; // convert 'rgb(R,G,B)' to 'rgb(R,G,B)A' which looks awful but will pass the regxep below
return color.match(/[\.\d]+/g).map(function (a)
{
return +a
});
}
}
Actually I tried Charlie Kilian's answer. For some reason it didn't work when you try to set .css('color')
with and 'rgb(0,0,0)' value.
I don't know why. Worked perfectly in the console. Maybe it was because my comparing function is in a javascript object and its some kind of a context issue or a reference problem. Either way finally I got frustrated and wrote my own function that will compare two colors regardless of the formats and does not create any elements or rely on jQuery. The function takes both HEX and RGB values.
It can probably be optimized but I really don't have the time right now. Hope this helps someone its pure javascript.
var compareColors= function (left, right) {
var l= parseColor(left);
var r=parseColor(right);
if(l !=null && r!=null){
return l.r == r.r && l.g == r.g && l.b == r.b;
}else{
return false;
}
function parseColor(color){
if(color.length==6 || color.length==7){
//hex color
return {
r:hexToR(color),
g:hexToG(color),
b:hexToB(color)
}
}else if(color.toLowerCase().indexOf('rgb')>-1){
var arr
var re = /\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*,\s*[0-9]{1,3}\s*/gmi;
var m;
if ((m = re.exec(color)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
arr = m[0].split(',');
return {
r: parseInt(arr[0].trim()),
g: parseInt(arr[1].trim()),
b: parseInt(arr[2].trim())
}
}else{
return null;
}
} else{
return null;
}
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
}
}
These following functions I took from www.javascripter.net
function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
Modified from Katie Kilian's but without requiring jQuery
<div id="dummy"></div>
function compareColors(left, right) {
// Create a dummy element to assign colors to.
document.getElementById('dummy').style.background = left;
var adjustedLeft = document.getElementById('dummy').style.background;
// Set the color to the right color value, and read it back.
document.getElementById('dummy').style.background = right;
var adjustedRight = document.getElementById('dummy').style.background;
// Both colors are now adjusted to use the browser's internal format,
// so we can compare them directly.
return adjustedLeft == adjustedRight;
}