Using the below code, trying to read the HSB values from PGraphics screen.
Reading is from random areas.
As a test, am currently writing back read values expecting no change.
Running code shows change as should show unaltered output.
What’s wrong??
Please help and any suggestions appreciated.
Thank you.
PGraphics pg;
// Size for pg
// high res 20000 X 10000
int wid = 500;
int hei = 500;
void setup() {
// Size for pg
pg = createGraphics(500, 500);
// size for display
size(500, 500);
pg.beginDraw();
frameRate(60);
pg.colorMode(RGB);
pg.background(50,50,50);
pg.colorMode(HSB);
pg.endDraw();
pg.loadPixels();
pg.updatePixels();
image(pg, 0, 0);
}
void draw() {
for (int i = 0; i < 1; i++) {
pg.loadPixels();
drawrect();
pg.updatePixels();
}
image(pg, 0, 0);
}
void drawrect() {
int pixel;
int x =int(random(wid));
int y =int(random(hei));
int w =int(random(150));
int h =int(random(150));
for (int px = 0; px < w; px++) {
for (int py = 0; py < h; py++) {
pixel=(x+px) + ((y+py)*wid);
// get background HSB
if (pixel < wid*(hei-1)){
float hue2=hue(pg.pixels[pixel]);
float sat2=saturation(pg.pixels[pixel]);
float bri2=brightness(pg.pixels[pixel]);
float c=color(hue2,sat2,bri2);
pg.pixels[pixel] = color(hue2,sat2,bri2);
}
}
}
}
You set HSB-mode to pg with pg.colorMode(HSB); So the external graphics is in HSB-mode, but processing display is not. Then in drawrect you read HSB values and color is in RGB-mode.
To get rid of changes you need to set processing display to HSB mode with command colorMode(HSB);. A good place for it is just at the end of setup().
Why don’t you drop PGraphics part it’s kinda unnecessary. An you could trim down the code a bit further like this:
void setup() {
// size for display
size(500, 500);
// set color to RGB(50, 50, 50)
background(50);
// set color mode to HSB
colorMode(HSB);
}
//Draw is run repeatedly
void draw() {
loadPixels();
drawrect();
updatePixels();
}
void drawrect() {
int pixel;
int x =int(random(width));
int y =int(random(height));
int w =int(random(150));
int h =int(random(150));
for (int px = 0; px < w; px++) {
for (int py = 0; py < h; py++) {
pixel=(x+px) + ((y+py)*width);
// get HSB
if (pixel < width*height-1){
//Get hue, saturation and brightness
float hue2=hue(pixels[pixel]);
float sat2=saturation(pixels[pixel]);
float bri2=brightness(pixels[pixel]);
pixels[pixel] = color(hue2,sat2,bri2);
}
}
}
}
Understand now.
Pg was ok, but when copying to screen - that was in RGB so outputted an interpretation of H=R , S=G, B=B
Well done. I though problem was elsewhere and accidentally solved the problem You can mostly ignore the code I posted, but have a look at it. There are a few things might help you along.