JuSchu JuSchu - 6 months ago 13
Java Question

getColorMode() in Processing

With

colorMode(HSB);
colorMode(RGB);


I can set the color mode.
Apparently the function
getColorMode()
does not exist.
Is there an other soloution to get the color mode?
What I'm trying to do is something like this

int cMode = getColorMode();
colorMode(HSB);
// draw stuff
colorMode(cMode);


This code is inside of my class
Track
. I want to draw a recorded GPS track and the hue value should be controled by the speed or the elevation.
Right now I set the color mode back to RGB after I finished drawing. Of course it would be better to set it back to the color mode it was before rather than just assuming, that it already was RGB.

Answer

Let's take a look at the source for PApplet. That class contains this variable:

public PGraphics g;

And here is the colorMode() function:

public void colorMode(int mode) {
  if (recorder != null) recorder.colorMode(mode);
  g.colorMode(mode);
}

That tells us that PApplet#colorMode() is really just a shortcut to PGraphics#colorMode().

So let's look at the source for PGraphics. The colorMode() function from that class eventually leads here:

public void colorMode(int mode,
                        float max1, float max2, float max3, float maxA) {
    colorMode = mode;
    //more code

Which leads us to the colorMode variable:

/** The current colorMode */
public int colorMode; // = RGB;

In other words, to get to the current color mode, you have to go from PApplet to its g variable, to its colorMode variable. If you're in a sketch, it looks like this:

void setup() {
 size(500, 500);
 colorMode(HSB);
}

void draw() {

  background(0);

  if(g.colorMode == RGB){
    println("RGB");
  }
  else if(g.colorMode == HSB){
    println("HSB");
  }
}

If you aren't in a sketch, then you'll need to pass in the PApplet instance using the this keyword.

PApplet mySketch;
int colorMode = mySketch.g.colorMode;
Comments