We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello again,
I'm trying to write a code that will cycle through all possible color combinations of every pixel for a certain sized canvas. Basically, take the color values in pixels[]
and increase a value until it gets to 255, then start it over at 0 and increase the next value by 1 until it gets to 255, at which point it will reset and increase the next value by one, etc.
I found a way that I think would do it with a for
loop, but because of the fact that the loop occurs once every frame, it does this to all pixels at once instead of going pixel by pixel. The loop I've written looks like this:
loadPixels();
for (p = 0; p < pixels.length; p++) {
//to avoid alpha values:
if (p % 4 == 3) {
continue;
}
pixels[p] += 1;
if (pixels[p] == 255) {
pixels[p+1] += 1;
pixels[p] = 0;
}
}
updatePixels();
(I'm pretty sure my logic on how I built this is flawed, I still don't have a perfect grasp on javascript)
My thought for that loop was that would function like this:
pixels[]
pixels[]
becomes 255, reset it to zero and add 1 to the next value of pixels[]
Instead, the whole canvas just slowly goes from black to white and then repeats. Am I correct in my assumption that this is because the loop is executed once per frame?
I've written super rudimentary code that does a portion of what I need, but the problem with it is that I'd have to write three if
statements for every pixel of the canvas (which is pretty much impossible). Could somebody tell me how to streamline this? I've been banging my head against the wall on it for a while now.
loadPixels();
pixels[0] += 1;
if (pixels[0] == 255) {
pixels[1] += 1;
pixels[0] = 0;
}
if (pixels[1] == 255) {
pixels[2] += 1;
pixels[1] = 0;
}
if (pixels[2] == 255) {
pixels[4] += 1;
pixels[2] = 0;
}
//End first pixel, start second:
if (pixels[4] == 255) {
pixels[5] += 1;
pixels[4] = 0;
}
if (pixels[5] == 255) {
pixels[6] += 1;
pixels[5] = 0;
}
if (pixels[6] == 255) {
pixels[8] += 1);
pixels[6] = 0;
}
//End of second pixel
//etc.
updatePixels();
Thanks in advance for any help or advice!
Answers
Not so sure if I understood correctly what you want there!
Either way, made a "class" called ColorIncreaser in order to streamline & keep tabs of some needed values:
These are the 3 ColorIncreaser's instance variables:
Create an instance of it via:
new ColorIncreaser()
. All of its 3 parameters are optional.After that, you can increase value inside current pixels[]'s index via inc() method or decrease it via dec().
It goes to either next or previous index when trying to inc() w/
px[idx] == 255
or dec() w/px[idx] == 0
.Besides reseting current value to
0
or255
respectively 1st! :DHope I've hit bull's eye this time. Check it out: (*)
Thanks for taking a look at this, GoToLoop! What you've written is almost it, there's one thing that's different though.
When
px[idx] == 255
it resetspx[idx]
to0
and increasesidx
by one. Basically meaning that it only focuses on changing the color value of one pixel at a time. I'm trying to get it to continue increasing the initial pixel while increasing the subsequent pixel, which would then go on to increase the pixel after that, etc. What I'm trying to set up is something that basically would function like an odometer:[ r ] [ g ] [ b ] [ a ] [ r ] [ g ] [ b ] [ a ]
(Here the brackets are just delineating the individual wheels of an odometer, not meaning arrays)
[ 0 ] [ 0 ] [ 0 ] [ 255 ] [ 0 ] [ 0 ] [ 0 ] [ 255 ]
(255 because the alpha values start there and should stay there)
As the first red value hits 255:
[ 255 ] [ 0 ] [ 0 ] [ 255 ] [ 0 ] [ 0 ] [ 0 ] [255]
The next value, the green value, becomes 1 and the red resets to 0.
[ 0 ] [ 1 ] [ 0 ] [ 255 ] [ 0 ] [ 0 ] [ 0 ] [ 255 ]
Then red1 continues to go up to 255 and reset to zero, raising green1 each time it completes a cycle:
[ 255 ] [ 1 ] [ 0 ] [ 255 ] [ 0 ] [ 0 ] [ 0 ] [ 255 ]
[ 0 ] [ 2 ] [ 0 ] [ 255 ] [ 0 ] [ 0 ] [ 0 ] [ 255 ]
This happens until green1 gets to 255, at which point it advances blue1 by one, and resets itself to 0.
[ 255 ] [ 255 ] [ 0 ] [ 255 ] [ 0 ] [ 0 ] [ 0 ] [ 255 ]
[ 0 ] [ 0 ] [ 1 ] [ 255 ] [ 0 ] [ 0 ] [ 0 ] [ 255 ]
From here, the red1 value has to cycle to 255 to advance green1 255 more times in order for the blue1 to become 2. And then once blue1 hits 255 and returns to 0, red2 (the first red after the alpha value) increases by one.
Is there some way to make this happen? I've written a mathematic formula for how long it would take each slot to advance, but I don't really know how to translate it into code that Javascript can actually use:
Does that make it any clearer?
I realize this looks really inefficient or strange, but the idea is that it is something that is supposed to unfold over a very long amount of time
I believe that if there were a way for me to get the
if
statements I wrote in the original post to work like a formula the solution would be pretty simple:I'd still have to adjust to avoid the alpha values, but that would get me 99% there. Is there anything built into either P5.js or Javascript that would allow me to do something like that?
255 * 255 * 255=16581375
Do whatever with this.
.
TfGuy44, thanks! I hadn't considered breaking it into an if/else statement. This should work quite well. And thanks again to GoToLoop for taking a serious stab at it. You guys are awesome!