How to go back to original colour using IF statements?

I want to be able to cycle my colours indefinitely on mouse clicks, and I know that if I have three colours in total, so colourOne=0, colourTwo=1, colourThree=2, then I will need to make it so when the mouse is clicked at colourThree, i'll get the value 0. but I am not sure how to put that in my code.

boolean colourOne= false;
boolean colourTwo= false;
boolean colourThree= false;

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

void draw(){

  rect(100,100,50,50);
  allColours();
}

void colourRed(){
  background(255,0,0);
}

void colourGreen(){
  background(0,255,0);
}

void colourBlue(){
  background(0,0,255);
}
void allColours(){
  if(colourOne){
    colourRed();
    if(colourTwo){
      colourGreen();
      if(colourThree){
        colourBlue();

}
    }
  }
}

void mouseClicked(){
  if(!colourOne){
    colourOne=true;
  }
  else if(!colourTwo){
    colourTwo=true;
  }
  else if(!colourThree){
    colourThree=true;
  }
}

Answers

  • edited November 2017 Answer ✓

    https://Processing.org/reference/modulo.html
    https://OpenProcessing.org/sketch/473642

    /** 
     * Palette Cycle (v1.0)
     * GoToLoop (2017/Nov/11)
     *
     * Forum.Processing.org/two/discussion/24960/
     * how-to-go-back-to-original-colour-using-if-statements#Item_1
     *
     * Forum.Processing.org/two/discussion/24988/processing-with-mouse-click
     *
     * OpenProcessing.org/sketch/473642
     */
    
    static final boolean JAVA = 1/2 != 1/2.;
    static final color[] PALETTE = { #FF0000, #008000, #0000FF };
    int idx;
    
    void setup() {
      size(300, 200);
      noLoop();
      colorMode(RGB);
    }
    
    void draw() {
      final color c = PALETTE[idx];
      background(c);
      if (JAVA)  getSurface().setTitle("Color: " + idx + "  -  #" + hex(c, 6));
    }
    
    void mousePressed() {
      keyPressed();
    }
    
    void keyPressed() {
      idx = (idx + 1) % PALETTE.length;
      redraw();
    }
    
Sign In or Register to comment.