var isRed = function(element) {
var bg = window.getComputedStyle(element).backgroundColor;
if (!bg) return false; // Sometimes there is no background color, in which case the element isn't red
// Otherwise, `bg` is a string:
var rb = bg.indexOf('(');
bg = bg.substr(rb + 1); // Trim off everything up until and including the "(" character
bg = bg.split(','); // Split on the "," delimiter
var r = parseInt(bg[0].trim());
var g = parseInt(bg[1].trim());
var b = parseInt(bg[2].trim());
return r > g && r > b;
};
console.log('#red?', isRed(document.getElementById('red')));
console.log('#green?', isRed(document.getElementById('green')));
console.log('#blue?', isRed(document.getElementById('blue')));
console.log('#quiteRed?', isRed(document.getElementById('quiteRed')));
console.log('#notRed?', isRed(document.getElementById('notRed')));
console.log('#transparentRed?', isRed(document.getElementById('transparentRed')));
.col {
width: 100%;
height: 30px;
color: #ffffff;
line-height: 30px;
}
#red { background-color: #ff0000; }
#green { background-color: #00ff00; }
#blue { background-color: #0000ff; }
#quiteRed { background-color: rgb(220, 130, 120); }
#notRed { background-color: #80b0c0; }
#transparentRed { background-color: rgba(200, 50, 50, 0.4); }
<div class="col" id="red">red</div>
<div class="col" id="green">green</div>
<div class="col" id="blue">blue</div>
<div class="col" id="quiteRed">quiteRed</div>
<div class="col" id="notRed">notRed</div>
<div class="col" id="transparentRed">transparentRed</div>