Here is a clever jQuery solution I devised on jsfiddle:
It's worth noting that with this method, the background-color value can range from rgb(50, 205, 50) to lime to #32CD32 and still function correctly.
Here is how you would implement it in your HTML:
<div id="test" style="background-color: #000000;">Can you see this text?</div>
And for the JavaScript part:
$(document).ready(function(){
var color = $('#test').css('background-color');
var startIndex = color.indexOf('(') + 1;
var lastIndex = color.indexOf(')');
var values = color.substr(startIndex, lastIndex - startIndex).split(',');
var r = 0;
var g = 0;
var b = 0;
$(values).each(function(i) {
if (i == 0) {
r = 255 - values[i];
}
if (i == 1) {
g = 255 - values[i];
}
if (i == 2) {
b = 255 - values[i];
}
});
$('#test').css('color', 'rgb(' + r + ',' + g + ',' + b + ')');
});
UPDATE: A non-jQuery approach that doesn't support color names like lime, blue, red, etc:
function invert(color) {
var startIndex = color.indexOf('(') + 1;
var lastIndex = color.indexOf(')');
var values = color.substr(startIndex, lastIndex - startIndex).split(',');
var r = 0;
var g = 0;
var b = 0;
for(i= 0; i < values.length; i++) {
if (i == 0) {
r = 255 - values[i];
}
if (i == 1) {
g = 255 - values[i];
}
if (i == 2) {
b = 255 - values[i];
}
}
return 'rgb(' + r + ',' + g + ',' + b + ')';
}