I’ve made a sketch that gradually reveals a picture by starting with all black, then gradually increasing the red value of each pixel up to the desired level, then doing the same for green, then blue (or any other order I choose). It works fine, creating a nice effect. I am trying to do the exact same same thing with the HSB color mode, and failing, and can’t figure out why.
Here is an excerpt from the code that works for RGB:
void colorByColor() { // called by draw() code that starts seqColorCtr at 0 and
// increments it with each call
if(seqColorCtr < 255) { // do 255 levels of red first
incrColor(‘r’); // bump red level by 1 where needed for all of darkPicture,
}
else if(seqColorCtr < 511) { // then 255 levels of green
incrColor(‘g’);
}
else if(seqColorCtr < 767) { // then 255 levels of blue
incrColor(‘b’);
}
else { // go to next picture
picStatus = 0;
}
void incrColor(char _colorToDo) {
int ctr;
darkPicture.loadPixels();
currentPicture.loadPixels();
if(_colorToDo == 'r') {// red
for(ctr = 0 ; ctr < currentPicture.width * currentPicture.height ; ctr++) {
if(red(currentPicture.pixels[ctr]) > (darkPicture.pixels[ctr] >> 16 & 0xFF)) {
darkPicture.pixels[ctr] += 0x010000;
} // end if red
} // end for ctr. Bumped up red by 1 tick after this pass
} // end if colorToTdo==r
if(_colorToDo == 'g') {// green
for(ctr = 0 ; ctr < currentPicture.width * currentPicture.height ; ctr++) {
if(green(currentPicture.pixels[ctr]) > (darkPicture.pixels[ctr] >> 8 & 0xFF)) {
darkPicture.pixels[ctr] += 0x100;
} // end if green
} // end for ctr. Bumped up green by 1 tick after this pass
} // end if colorToTdo==g
if(_colorToDo == 'b') {// blue
for(ctr = 0 ; ctr < currentPicture.width * currentPicture.height ; ctr++) {
if(blue(currentPicture.pixels[ctr]) > (darkPicture.pixels[ctr] & 0xFF)) {
darkPicture.pixels[ctr] += 0x1;
} // end if blue
} // end for ctr. Bumped up blue by 1 tick after this pass
} // end if colorToTdo==b
} // end incrColor
I use bit-shifting to find the r, g, and b elements of each pixel, and I add a hex value to increment each of them by 1 when necessary. It works fine.
When I do the same thing in the HSB mode, it doesn’t work. I get results that are just weird, and never end up making darkPicture look the same a currentPicture as it is supposed to. It really seems like it is incrementing a single color first, which it shouldn’t be doing.
In HSB mode, are H, S, and B stored in the same two-bytes-each way as R, G, and B are stored in RGB mode? I am beginning to think they are not, and that HSB mode stores values as RGB, then converts them on the fly, maybe by some kind of lookup table. Is this how it works?
Closely related question: When I create darkPicture, I use an HSB argument:
darkPicture = createImage(currentPicture.width, currentPicture.height, HSB);
The reference page doesn’t say I can do this. I don’t see any error message when I do it, but is it accomplishing anything? Is the pixel color still being stored in the pixel array with R in two bytes, G in two bytes, and B in two bytes? It certainly doesn’t feel right to use an RGB argument if what I want is HSB.
I suppose I could use get() and set() to increment the H, S and B values rather than directly writing to the pixel array. But that would be less efficient, and I hate to give up if I don’t have to. Thanks for any light you can shed on this.