Colour changing! (get the values to go back)

I am working on a small project and I want the stoke of the lines I’m using to slowly gradient through different colours.

I have an array called “col” with three integers for each RGB value. Each are set to 255 and each one decreases by a slightly different increment; col[0] by 1, col[1] by 2, and col[2] by 3. I got that part figured out.

I need help figuring out how to get the values to go back up to 255 after hitting about 25.

I’ve tried if/else statements but they are limiting for this. As the number would hit 25 it would increase, but immediately go back to 25 as it is still technically under 255.

Some help would be much appreciated, I just got back into coding and I am a little stumped!

here’s my code, the part i need help with is the “void colourChange(){}”:

float angle = PI / 4 ;
float sizeMultiply = 0.67 ; 
int len = 250;
int[] col = {255, 255, 255}; 

void setup() {
 size(1000, 800);
}

void draw(){
  background(0);
  strokeWeight(2);
  text("Angle: " + angle, 800, 700);
  text("Line Length: " + len + " pixels", 800, 720);
  text("Line Fraction: " + sizeMultiply*100 + "%", 800, 740);
  translate(width/2, height);
  branch(len);
  colourChange();
}

//draws branches
void branch(float len){
  line(0, 0, 0, -len);
  stroke(col[0], col[1], col[2]);
  translate(0, -len);
  if (len > 2.5){
    push();
    rotate(angle);
    branch(len * sizeMultiply);
    pop();
    push();
    rotate(-angle);
    branch(len * sizeMultiply);
    pop();
  }
}

// when the mousewheel is used, add/subtract 0.025 * +/-1 depending on scroll direction.
void mouseWheel(MouseEvent event){
  float e = event.getCount();
  e = e * 0.025;
  angle = angle + e;
}

void colourChange(){
    col[0] = col[0] - 1;
    col[1] = col[1] - 2;
    col[2] = col[2] - 3;

    //need help here! Below is what I tried.
    if (col[0] <= 25){
      col[0] = col[0] + 1;
    }
    //I cant figure out how to stop it from declining while going back up to 255
    //If/Else statements are too limiting for it and I'm totally blanking on what to do instead
}

//when specific key is hit, do thing
void keyPressed(){
  if (keyPressed){
    if(keyCode == UP){
      len = len + 5;
    } else if (keyCode == DOWN){
      len = len - 5;
    } else if (keyCode == RIGHT){
      sizeMultiply = sizeMultiply + 0.001;
      if(sizeMultiply > 0.71){
        sizeMultiply = 0.71;
      }
    } else if (keyCode == LEFT) {
      sizeMultiply = sizeMultiply - 0.001;
      if (sizeMultiply < 0){
        sizeMultiply = 0;
      }
    }
  }
  
}
1 Like

Try this change below. You can do this similar logic for green and blue aka stepGreen and stepBlue.

Kf

int stepRed=1;
void colourChange(){
    col[0] = col[0] - stepRed;
    col[1] = col[1] - 2;
    col[2] = col[2] - 3;

    //need help here! Below is what I tried.
    if (col[0] <= 25 || col[0] >255){
      stepRed = -1 * stepRed; //Changes signs aka. direction when it reaches 25 or 255
    }

}
1 Like

worked perfectly, thanks!