Hi All,
I’m currently trying to develop a sketch where you can rub out a colour from the screen, but it keeps fading back from black to the colour.
You can see an index of experiments here:
Sketches here
The sketch in question is here:
Screen fades back - caution - will slow your computer right down!
You can see the source code here:
GitHub link to source code
loadPixels();
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
for (let i = 0; i < d; i++) {
for (let j = 0; j < d; j++) {
// if the colour of the current pixel matches the given colour, then increment numberOfPixelsThatMatchGivenColour
index = 4 * ((y * d + j) * width * d + (x * d + i));
currentColour = color(
pixels[index],
pixels[index + 1],
pixels[index + 2],
pixels[index + 3]
);
if (
pixels[index] == redTarget &&
pixels[index + 1] == greenTarget &&
pixels[index + 2] == blueTarget
) {
//do nothing, we are already there
} else {
//this is bad because we lerp forever, never quite getting there...
let newColor = lerpColor(currentColour, targetColour, lerpAmount);
pixels[index] = red(newColor);
pixels[index + 1] = green(newColor);
pixels[index + 2] = blue(newColor);
pixels[index + 3] = alpha(newColor);
}
}
}
}
}
//https://p5js.org/reference/#/p5/updatePixels
updatePixels();
It’s very inefficient to have to check each pixel on the screen - I was hoping to be able to get access to the pixels as an HSB rather than RGBA array - that way I could just increment each pixels saturation to fade from black to the foreground colour, but this doesn’t seem to be possible.
Should I be using a shader? Should I be drawing in a smaller offscreen buffer and then scaling to the size of the screen? All suggestions gratefully received!
Thanks,
Joel