I have an img tag. The image inserted has different parts:
I want to be able to color each white part alone.
I tried using this function:
function getPixels(img)
{
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, img.width, img.height);
originalPixels = ctx.getImageData(0, 0, img.width, img.height);
currentPixels = ctx.getImageData(0, 0, img.width, img.height);
img.onload = null;
}
function hexToRGB(hex)
{
console.log(hex)
var long = parseInt(hex.replace(/^#/, ""), 16);
return {
R: (long >>> 16) & 0xff,
G: (long >>> 8) & 0xff,
B: long & 0xff
};
}
function changeColor(a,b,c)
{
if(!originalPixels) return; // Check if image has loaded
var newColor = {R: eval(a), G: eval(b), B: eval(c)};
for(var I = 0, L = originalPixels.data.length; I < L; I += 4)
{
if(currentPixels.data[I + 3] > 0) // If it's not a transparent pixel
{
console.log(currentPixels.data[I + 3])
if ((currentPixels.data[I]!=0 && currentPixels.data[I+1]!=0 && currentPixels.data[I+2]!=0)) {
currentPixels.data[I] = newColor.R;
currentPixels.data[I + 1] = newColor.G;
currentPixels.data[I + 2] = newColor.B;
}
}
}
console.log(newColor)
ctx.putImageData(currentPixels, 0, 0);
mug.src = canvas.toDataURL("image/png");
}
function valueToHex(c) {
var hex1 = c.toString(16);
return hex1
}
I had the idea to make a condition in which i can detect all the white pixels color them and stop when a black pixel is detected, so it doesn’t go out of line, and then i can color each white part of the image of a different color. But i didn’t know how to do it as i’m really new to the pixel field.
Could you help me please?





