Incrementing/Decrementing Color Brightness

Hello all, I am working on developing a drawing tool for one of my classes and I am having trouble with a specific part of the code. The color variable “x” should increment by 10 when the UP key is pressed and decrement by 10 when the DOWN key is pressed. However, nothing happens when I press either of those. The goal is that the color of the line being drawn in redLine() (there is previous code in the program to draw a line that follows mouse movement, and this works fine) should go up in brightness when the UP key is pressed and down in brightness when the DOWN key is pressed.

I have only included the parts of the code that are directly relevant here. These are three functions that are called in draw(). The startOver() function is included to show what happens if the user starts by pressing the UP key, thereby trying to create a brightness past 255.

I am still quite new to Processing so apologies in advance. Thank you!

color x = 255;

// The line being drawn turns red when 'r' is pressed
void redLine() {
  if (keyPressed) {
    if (key == 'r') {
      stroke(x, 0, 0);
    }
  }
}

// Brightness of red increases when UP arrow is pressed
// Brightness of red decreases when DOWN arrow is pressed
void changeBrightness() {
  if (keyPressed) {
    if (key == CODED) {
      if (keyCode == DOWN) {
        x = x - 10;
      } else if (keyCode == UP) {
        x = x + 10;
      }
    }
  }
}

// Brightness cannot be more than 255 or less than 0
void startOver() {
  if (x < 0) {
    x = 255;
  } else if (x > 255) {
    x = 0;
  }
}
1 Like

Hi, i suggest you to use the function keyPressed, it’s called every time that you pressed a key.

for example

int x = 255;
boolean showRedLine = false;
void draw(){
if(showRedLine){
  stroke(x, 0, 0);
  }
}

void keyPressed(){
if(keyCode == UP){
    x = x + 10;
    if(x >=255){
       x = 255;
      }       
   }
if(keyCode == DOWN){
    x = x - 10;
    if(x <= 0){
      x = 0;
    }
  }
if(key == 'r'){
   showRedLine != showRedLine;
   } 
}

https://processing.org/reference/keyPressed_.html

2 Likes

there is a easy way for “edit” variables like color…
by mouse wheel,
where you combine it with a keypressed, pls check

2 Likes