If you really donât want to use pushStyle / popStyle, then bit-shifting is definitely the way to go â in both directions (extracting the color value, and building the fill value). If you want to use red()
it is scaled to match current colorMode, and fill()
accepts arguments in the current colorMode â so just bypass both. Then you can leave your sketch in HSB colorMode the entire time.
colorMode()
doesnât change the underlying data in the pixels array, only the way it is interpreted by helper methods. Since your goal is to copy color information from this, skipping the mode entirely:
AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB
To, for example, a color channel:
FFFFFFFFRRRRRRRR0000000000000000
Here is an example:
/** RGB in HSB mode
* 2019-11-11 Processing 3.4
* Display the red channel of any pixel, while in HSB colorMode.
*/
int maxH = 360;
PImage img;
void setup(){
size(200,200);
img = loadImage("https://processing.org/img/processing3-logo.png");
img.resize(200,200);
colorMode(HSB, maxH, 100, 100); // set HSB colormode
}
void draw() {
image(img, 0, 0);
int rval = int(red(get(mouseX, mouseY)) * 255 / float(maxH)); // retrieve scaled r value
int rcolor = (255 << 24) + (rval << 16); // alpha + r channel // build r channel value
fill(rcolor);
rect(0, 0, 50, 50);
}
Edit
Oops, sorry, meant to also include the version showing an alternative to red() with bit shifting:
/** RGB in HSB mode
* 2019-11-11 Processing 3.4
* Display the red channel of any pixel, while in HSB colorMode.
*/
PImage img;
void setup(){
size(200,200);
img = loadImage("https://processing.org/img/processing3-logo.png");
img.resize(200,200);
colorMode(HSB, 360, 100, 100);
}
void draw() {
image(img, 0, 0);
// int rval = int(red(get(mouseX, mouseY)) * 255 / g.colorModeX); // retrieve scaled r value and rescale to 255
int rval = get(mouseX, mouseY) >> 16 & 0xFF; // retrieve direct 8-bit (255) r value,
int rcolor = (255 << 24) + (rval << 16); // alpha + r channel // build r channel value
fill(rcolor);
rect(0, 0, 50, 50);
}
I have also included a commented out lined demonstrating scaling red()
using g.colorModeX â thanks @Lexyth.