HTML5 Canvas: Image Color Change

I have an image (in grayscale) from which I want to change the color (custom). Since it is quite difficult to change the image color in shades of gray, I came up with an approach.

The image is divided into two parts.

  • One image with white color.
  • Secondly, a translucent image with shades of gray.

Now I put both images on top of each other (with a white image at the bottom and a gray image at the top) so that when I change the color of the white image, it will be visible to the user.

Problem: This approach works for me, except for one problem. When I color the white image, it will be a pixel from the corners.

JSFiddle: http://jsfiddle.net/pandey_mohit/BeSwL/

JSFiddle contains three images for capsules:

  • Top of the capsule White image (for coloring)
  • Bottom Capsule Part White Picture (for coloring)
  • Translucent image for 3D effect (using grayscale)

Choose red, green, or blue to see the problem.

function hexToRgb(color) {
    var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
    color = color.replace(shorthandRegex, function(m, r, g, b) {
        return r + r + g + g + b + b;
    });

    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(color);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : {
        r: 0,
        g: 0,
        b: 0
    };
}

function colorImage(imgId,hexaColor) {
    // create hidden canvas (using image dimensions)
    var imgElement = document.getElementById(imgId);

    var canvas = document.createElement("canvas");
    canvas.width = imgElement.width;
    canvas.height = imgElement.height;

    var ctx = canvas.getContext("2d");
    ctx.drawImage(imgElement,0,0);

    var imageData = ctx.getImageData(0,0,canvas.width,canvas.height);

    var data = imageData.data;

    // convert image to grayscale
    var rgbColor = hexToRgb(hexaColor);

    for(var p = 0, len = data.length; p < len; p+=4) {
        if(data[p+3] == 0)
           continue;
        data[p + 0] = rgbColor.r;
        data[p + 1] = rgbColor.g;
        data[p + 2] = rgbColor.b;
        data[p + 3] = 255;
    }
    ctx.putImageData(imageData, 0, 0);

    // replace image source with canvas data
    imgElement.src = canvas.toDataURL();
}

// changing color of capsule on select event
document.getElementById('sel_top').onchange = function(){
    colorImage('img_top', this.value);
}
document.getElementById('sel_bottom').onchange = function(){
    colorImage('img_bottom', this.value);
}
+4
source share
1 answer

Your repeat algorithm maps the fourth byte of each RGBA quartet to 255, which discards the alpha overlay and breaks the anti-aliasing around the edges of the image. Saving the alpha channel of the original gives the best results:

for(var p = 0, len = data.length; p < len; p+=4) {
    data[p + 0] = rgbColor.r;
    data[p + 1] = rgbColor.g;
    data[p + 2] = rgbColor.b;
}

JSFiddle with commented out lines

+7
source

Source: https://habr.com/ru/post/1545968/


All Articles