22

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?

1 Answer 1

27

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)

5
  • Great thinking. Note that it does require <canvas> support, which IE 8 and earlier don’t have natively. Nov 16, 2012 at 15:31
  • @PaulD.Waite IMHO, as IE 8 doesn't even play well with data URI (32KB for max length), this should not be a problem.
    – xiaoyi
    Nov 16, 2012 at 15:57
  • 1
    From what I have read on caniuse.com the IE problem (as you note, depending on size) is with data URIs in HTML, not in CSS...if that helps.
    – adam-asdf
    Nov 16, 2012 at 16:04
  • Hi there, great answer and script. Implemented it. However, on the first load it seems to always output a transparent png. When using the script on user interaction it seems to work fine. Anyone encountered this problem?
    – Redox
    Aug 13, 2015 at 9:04
  • 2
    DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The source width is 0
    – Vlad
    Jul 25, 2019 at 10:42

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.