Colored Letters with Colorcycle | Processing

Viewed 35

I found a program, that generates random letters in a grid and gives them a random color. How can I have the letters cange in color or brightness while the program is running? (sourcecode: https://happycoding.io/examples/processing/for-loops/letters)

I tried making the fill(r, g, b) have a 'r' that cycles from 1 to 255 and back while having 'g' and 'b' at 0, but I could´t get it to update the color. Im cinda new to programming so I´d love to know how I could make that happen.

1 Answers

First, let's change the fill method to accept RGB values:

fill(random(256),random(256),random(256));

To change the colors while the program is running, the changes must be made inside the draw() method, that will constantly loop and update the canvas. Further information about draw here I believe the following code outputs what you asked for:

int rows = 10;
int cols = 10;

int cellHeight;
int cellWidth;

void setup(){
  size(500, 500);
  cellHeight = height/rows;
  cellWidth = width/cols;
  textAlign(CENTER, CENTER);
  textSize(28);
}

void draw(){
  background(32);
  for(int y = 0; y < rows; y++){
    for(int x = 0; x < cols; x++){
         
      //get a random ascii letter
      char c = '!';
      c += random(93);
      
      //calculate cell position
      int pixelX = cellWidth * x;
      int pixelY = cellHeight * y;
      
      //add half to center letters
      pixelX += cellWidth/2;
      pixelY += cellHeight/2;
      
      fill(random(256),random(256),random(256));
      text(c, pixelX, pixelY);
    }
  }
  delay(100);
}
Related