Reading / Writing HSB values

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); 
      }
    }
  }
}