JuSchu JuSchu - 1 year ago 62
Java Question

getColorMode() in Processing



I can set the color mode.
Apparently the function
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();
// draw stuff

This code is inside of my class
. 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 Source

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

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

void draw() {


  if(g.colorMode == RGB){
  else if(g.colorMode == 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;