You bet. Here’s the page:
Custom Colors
As described on that page, the user chooses 2 colors from 2 color pickers, then clicks on the 2 buttons to make the wall color, then the art color, change according to their chosen colors. This all worked perfectly with the code in the page body.
Two of the functions, getWallPixels() and getArtPixels() are triggered onload in the image tags. The buttons fire onclick events for changeWallColor() and changeArtColor() functions. The color-changing code is here:
var artWall = document.getElementById("wall-color");
var artOnly = document.getElementById("art-color");
var canvas = document.createElement("canvas");
var canvas2 = document.createElement("canvas");
var ctx = canvas.getContext("2d");
var ctx2 = canvas2.getContext("2d");
var originalPixels = null;
var currentPixels = null;
var originalPixels2 = null;
var currentPixels2 = null;
var test = "bear";
function getWallPixels(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 getArtPixels(img)
{
canvas2.width = img.width;
canvas2.height = img.height;
ctx2.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight, 0, 0, img.width, img.height);
originalPixels2 = ctx2.getImageData(0, 0, img.width, img.height);
currentPixels2 = ctx2.getImageData(0, 0, img.width, img.height);
img.onload = null;
}
function hexToRGB(hex)
{
var long = parseInt(hex.replace(/^#/, ""), 16);
return {
R: (long >>> 16) & 0xff,
G: (long >>> 8) & 0xff,
B: long & 0xff
};
}
function changeWallColor()
{
if(!originalPixels) return; // Check if image has loaded
var newColor = hexToRGB(document.getElementsByTagName("INPUT")[0].value);
for(var I = 0, L = originalPixels.data.length; I < L; I += 4)
{
if(currentPixels.data[I + 3] > 0) // If it is not a transparent pixel
{
currentPixels.data[I] = originalPixels.data[I] / 255 * newColor.R;
currentPixels.data[I + 1] = originalPixels.data[I + 1] / 255 * newColor.G;
currentPixels.data[I + 2] = originalPixels.data[I + 2] / 255 * newColor.B;
}
}
ctx.putImageData(currentPixels, 0, 0);
artWall.src = canvas.toDataURL("image/png");
}
function changeArtColor()
{
if(!originalPixels2) return; // Check if image has loaded
var newColor = hexToRGB(document.getElementsByTagName("INPUT")[1].value);
for(var I = 0, L = originalPixels2.data.length; I < L; I += 4)
{
if(currentPixels2.data[I + 3] > 0) // If it is not a transparent pixel
{
currentPixels2.data[I] = originalPixels2.data[I] / 255 * newColor.R;
currentPixels2.data[I + 1] = originalPixels2.data[I + 1] / 255 * newColor.G;
currentPixels2.data[I + 2] = originalPixels2.data[I + 2] / 255 * newColor.B;
}
}
ctx2.putImageData(currentPixels2, 0, 0);
artOnly.src = canvas2.toDataURL("image/png");
}
Thanks for taking a look.