Invert HTML Colors in text
by unitycoder
HTML
<h2>HTML HEX Color Inverter from Free Text</h2>
<h4>(Only ARGB format is currently supported)</h4>
<hr>
Input:<br>
<textarea id="source" rows="8" cols="75">
ThemeDarkestBackground=#FF121212
ThemeDarkMenuBar=#FF0C0C0C
ThemeMainBackgroundColor=#FF262626
</textarea><br>
<button type="button" onclick="Process()">Process Colors</button>
<hr>
Output:<br>
<textarea id="target" rows="8" cols="75">
</textarea><br>
JavaScript
function Process()
{
// get text areas
var s = document.getElementById("source");
var t = document.getElementById("target");
// get input text
var txt = s.value;
// find HTML colors in text
txt = txt.replace(/#(?:[0-9a-fA-F]{3,4}){1,2}/ig, function($1)
{
var c = HexToRGBA($1);
// invert
c.r = 255-c.r;
c.g = 255-c.g;
c.b = 255-c.b;
return ColorToHex(c);
});
// output modified colors and original string
t.value = txt;
}
// https://stackoverflow.com/a/28056903/5452781
function HexToRGBA(hex)
{
var a = parseInt(hex.slice(1, 3), 16),
r = parseInt(hex.slice(3, 5), 16),
g = parseInt(hex.slice(5, 7), 16),
b = parseInt(hex.slice(5, 7), 16);
//return "rgba(" + r + ", " + g + ", " + b + ", " + a + ")";
return {"r":r,"g":g,"b":b,"a":a};
}
function ColorToHex(c)
{
var r = c.r.toString(16);
r = r.length == 1 ? "0" + r : r;
var g = c.g.toString(16);
g = g.length == 1 ? "0" + g : g;
var b = c.b.toString(16);
b = b.length == 1 ? "0" + b : b;
var a = c.a.toString(16);
a = a.length == 1 ? "0" + a : a;
return ("#"+a+r+g+b).toUpperCase();
}