Skip to content

Instantly share code, notes, and snippets.

@lzlrd
Last active November 18, 2020 00:23
Show Gist options
  • Select an option

  • Save lzlrd/db02824eea921e8474d1291683fdca37 to your computer and use it in GitHub Desktop.

Select an option

Save lzlrd/db02824eea921e8474d1291683fdca37 to your computer and use it in GitHub Desktop.
A JS library for inverting colour.
/*
invertJS (A JS library for inverting colour)
By: Diab Neiroukh
Licensed-under: The GNU GPLv3
Last-updated: 23/Feb/2020
*/
function invertcolour(colour) {
colour = colour.replace('#', '');
if (colour.length !== 6) {
colour = Array.prototype.join.call(arguments).match(/(-?[0-9\.]+)/g);
for (i = 0; i < colour.length; i++) {
colour[i] = ((i === 3 ? 1 : 255) - colour[i]);
}
for (j = 1; j < colour.length; j++)
colour[j] = " " + colour[j];
colour = "rgb(" + colour + ")";
return colour;
}
colour = colour.toUpperCase();
splitnum = colour.split("");
resultnum = "";
simplenum = "FEDCBA9876".split("");
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 {
return false;
}
}
resultnum = "#" + resultnum;
return resultnum;
}
@lzlrd

lzlrd commented Nov 18, 2020

Copy link
Copy Markdown
Author

Add RGBA support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment