PGraphics Background Color

edited April 13 in Library Questions

Hi there, when I save PGraphics to jpg file, the background color is always Black. I've tried ####.background(255) like this to change the background color. But I'm using my canvas with live Video. So the outcome was not what I want. How can I change the background Color when I save PGraphics? Is there any way to change it?

Here is my Code.

import processing.video.*;

Capture video;

color trackColor = color(145,43,54);
color trackColor2 = color(142,156,42);
color trackColor3 = color(18,35,40);

float threshold = 25;
PGraphics topLayer;

float r2;
float g2;
float b2;

float r3;
float g3;
float b3;

float r4;
float g4;
float b4;

float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
  float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  return d;

}
float distSq2(float x1, float y1, float z1, float x2, float y2, float z2) {
  float d2 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  return d2;

}
float distSq3(float x1, float y1, float z1, float x2, float y2, float z2) {
  float d3 = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
  return d3;

}

void setup() {
  size(640, 360);
  String[] cameras = Capture.list();
  printArray(cameras);
  video = new Capture(this, cameras[3]);
  video.start();
  trackColor = color(145,43,54);
  trackColor2 = color(142,156,42);
  trackColor3 = color(18,35,40);

  topLayer = createGraphics(width, height, g.getClass().getName());
}

void captureEvent(Capture video) {
  video.read();
}

void draw() {

  video.loadPixels();
  image(video, 0, 0);

  //threshold = map(mouseX, 0, width, 0, 100);
  threshold = 25;
  float thresholdSquare = threshold * threshold;

  // Begin loop to walk through every pixel

  topLayer.beginDraw();
  topLayer.clear();
  for (int x = 0; x < video.width; x++ ) {
    for (int y = 0; y < video.height; y++ ) {
      int loc = x + y * video.width;
      // What is current color
      color currentColor = video.pixels[loc];

      float r1 = (currentColor>>16)&0xFF;
      float g1 = (currentColor>>7) & 0xFF ;
      float b1 = (currentColor) & 0xFF;

      r2 = (trackColor>>16)&0xFF;
      g2 = (trackColor>>7) & 0xFF;
      b2 = (trackColor) & 0xFF;

      r3 = (trackColor2>>16)&0xFF;
      g3 = (trackColor2>>7) & 0xFF;
      b3 = (trackColor2) & 0xFF;

      r4 = (trackColor3>>16)&0xFF;
      g4 = (trackColor3>>7) & 0xFF;
      b4 = (trackColor3) & 0xFF;
      //
      float d = distSq(r1, g1, b1, r2, g2, b2);
      float d2 = distSq2(r1, g1, b1, r3, g3, b3);
      float d3 = distSq3(r1, g1, b1, r4, g4, b4);

      if (d < thresholdSquare) {
        topLayer.stroke(trackColor);
        topLayer.fill(trackColor);
        topLayer.ellipse(x, y,2,2);
      }
      else if( d2 < thresholdSquare){  
        topLayer.stroke(trackColor2);
        topLayer.fill(trackColor2);
        topLayer.ellipse(x,y,2,2);
      }
      else if(d3 < thresholdSquare){
        topLayer.stroke(trackColor3);
        topLayer.fill(trackColor3);
        topLayer.ellipse(x,y,2,2);
      }
      }
     ////////////////////////////////////////
      }
        topLayer.endDraw(); 
        image(topLayer, 0, 0);

    if(mousePressed){
      topLayer.save("example.jpg");
    }
}

Answers

  • Don't do topLayer.clear or topLayer.background. Instead, move line 50 to 69. This will ensure the transparency is not change (Better way to do it.... probly. This change is just a test)

    Don't do this:

       if(mousePressed){
          topLayer.save("example.jpg");
        }
    

    Instead, move that call to

    void mousePressed(){ topLayer.save("example.jpg"); }

    The former is called many times in draw while a key is pressed. The latter is called once.

    Kf

Sign In or Register to comment.