Is there any way to take a base 64 string, for example:
.copyIcon {background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAW0lEQVR42mNgQALCi7//J4QZcAFiNOM1hJANBA0h1QC83iHFizDJ/dgww/7/LAQNwKUZbkjDfya8YQZXiCqJagilBmAzhLYGYDNsJBhAMD3gS854NS/6vg+fZgDKvmW19S7PRAAAAABJRU5ErkJggg==") center center no-repeat;}
And replace a solid color with another solid color using JavaScript?
In this specific example I have a solid color in the icon (#13A3F7
) that I would like to replace with another solid color (#ff6400
).
The reason for doing this is it is not a one-off. I would like to be able to change the icon to any color with a setting.
Is there any way I can do this?
But yes, different input strings will always encode to different Base64-encoded strings, and the same input string will always encode to the same Base64-encoded string. It's not a hash though, so small changes in the input will only result in small changes in the output.
Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.
With the introduction of multiplexing that arrived with HTTP/2, web browsers have become incredibly efficient in delivering hundreds of files through a single connection. This works around most limits that the Base64 encoding solved and in fact means Base64 now does more bad than good.
Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with ASCII. This is to ensure that the data remain intact without modification during transport.
Here is a little function which takes 3 parameters: data, colorFrom, colorTo (both colors should be supplied in hex)
function changeColInUri(data,colfrom,colto) {
// create fake image to calculate height / width
var img = document.createElement("img");
img.src = data;
img.style.visibility = "hidden";
document.body.appendChild(img);
var canvas = document.createElement("canvas");
canvas.width = img.offsetWidth;
canvas.height = img.offsetHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(img,0,0);
// remove image
img.parentNode.removeChild(img);
// do actual color replacement
var imageData = ctx.getImageData(0,0,canvas.width,canvas.height);
var data = imageData.data;
var rgbfrom = hexToRGB(colfrom);
var rgbto = hexToRGB(colto);
var r,g,b;
for(var x = 0, len = data.length; x < len; x+=4) {
r = data[x];
g = data[x+1];
b = data[x+2];
if((r == rgbfrom.r) &&
(g == rgbfrom.g) &&
(b == rgbfrom.b)) {
data[x] = rgbto.r;
data[x+1] = rgbto.g;
data[x+2] = rgbto.b;
}
}
ctx.putImageData(imageData,0,0);
return canvas.toDataURL();
}
An additional function is required to convert hex colors to RGB (for correct matching)
function hexToRGB(hexStr) {
var col = {};
col.r = parseInt(hexStr.substr(1,2),16);
col.g = parseInt(hexStr.substr(3,2),16);
col.b = parseInt(hexStr.substr(5,2),16);
return col;
}
Usage would be like so:
changeColInUri(
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAW0lEQVR42mNgQALCi7//J4QZcAFiNOM1hJANBA0h1QC83iHFizDJ/dgww/7/LAQNwKUZbkjDfya8YQZXiCqJagilBmAzhLYGYDNsJBhAMD3gS854NS/6vg+fZgDKvmW19S7PRAAAAABJRU5ErkJggg==",
"#13A3F7",
"#ff6400"
);
It will return a new data:image/png; URI with the swapped colors, here is a working jsfiddle of the end result
http://jsfiddle.net/V5dU2/
(tested on Chrome, Firefox and IE10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With