4

How do I invert the text-color of an element using jQuery?

<div style="color: rgb(0, 0, 0)">Invert me</div>
General Grievance
  • 4,555
  • 31
  • 31
  • 45
David Knaack
  • 172
  • 1
  • 1
  • 8
  • 2
    In a nutshell, get the colour, turn it into 3 decimal values (it's returned as hex IIRC) and subtract those values from 255. You then have your R, G and B channels and you use `$.css()` to apply them again. – Bojangles Feb 01 '12 at 18:33
  • 2
    http://webhole.net/2010/01/06/how-to-invert-an-elements-color/ – Dave Swersky Feb 01 '12 at 18:33
  • 2
    http://stackoverflow.com/questions/4766201/javascript-invert-color-on-all-elements-of-a-page – j08691 Feb 01 '12 at 18:36
  • @JamWaffles You should make this an answer. But do you really need the entire jQuery library to do this simple task? Maybe something like: `document.getElementById('').style.color = invert(0, 0, 0); function invert(r, g, b){r = 255-r, g = 255-g, b=255-b; return {'r':r,'g':g,'b':b}}` – Eric Hodonsky Feb 01 '12 at 18:39
  • @Relic Excellent point. It can of course be done without jQuery, but the OP is using it, so it's a little better to use jQuery in this case, although the only thing it makes easier is element selection. – Bojangles Feb 01 '12 at 18:40

4 Answers4

11

A bit late but better late than never:

function invert(rgb) {
  rgb = Array.prototype.join.call(arguments).match(/(-?[0-9\.]+)/g);
  for (var i = 0; i < rgb.length; i++) {
    rgb[i] = (i === 3 ? 1 : 255) - rgb[i];
  }
  return rgb;
}

console.log(
  invert('rgba(255, 0, 0, 0.3)'), // 0, 255, 255, 0.7
  invert('rgb(255, 0, 0)'), // 0, 255, 255
  invert('255, 0, 0'), // 0, 255, 255
  invert(255, 0, 0) // 0, 255, 255
);
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
yckart
  • 32,460
  • 9
  • 122
  • 129
1

First load http://www.phpied.com/files/rgbcolor/rgbcolor.js

Then you can do

$.fn.invertElement = function() {
  var prop = 'color';

  if (!this.css(prop)) return;

  var color = new RGBColor(this.css(prop));
  if (color.ok) {
    this.css(prop, 'rgb(' + (255 - color.r) + ',' + (255 - color.g) + ',' + (255 - color.b) + ')');
  }
};

$('div').invertElement();

This should also work when the color property is specified with a word (like "black") rather than an RGB value. It won't work well with transparency, however.

Muhd
  • 24,305
  • 22
  • 61
  • 78
1

I found a great 'Hexadecimal Color Inverter' function wrote by Matt LaGrandeur (http://www.mattlag.com/)

function invertHex(hexnum){
  if(hexnum.length != 6) {
    console.error("Hex color must be six hex numbers in length.");
    return false;
  }

  hexnum = hexnum.toUpperCase();
  var splitnum = hexnum.split("");
  var resultnum = "";
  var simplenum = "FEDCBA9876".split("");
  var complexnum = new Array();
  complexnum.A = "5";
  complexnum.B = "4";
  complexnum.C = "3";
  complexnum.D = "2";
  complexnum.E = "1";
  complexnum.F = "0";

  for(i=0; i<6; i++){
    if(!isNaN(splitnum[i])) {
      resultnum += simplenum[splitnum[i]]; 
    } else if(complexnum[splitnum[i]]){
      resultnum += complexnum[splitnum[i]]; 
    } else {
      console.error("Hex colors must only include hex numbers 0-9, and A-F");
      return false;
    }
  }

  return resultnum;
}

Source is here: http://www.mattlag.com/scripting/hexcolorinverter.php

Ofer Segev
  • 5,094
  • 2
  • 22
  • 22
1

For newer browsers (>2016), there is no need to do any extra calculations thanks to the CSS filter property. Setting the value to invert(1) will invert the foreground color.

MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/filter#invert

$('#invert-me').css({ filter: 'invert(1)' });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="invert-me" style="color: rgb(0, 255, 0);">Invert me (Opposite of green is magenta)</div>
General Grievance
  • 4,555
  • 31
  • 31
  • 45