Creating array of colors with lerpColor

@Chrisir (I don’t know what happened to my response as I had withdrawn the topic. But am reposting the response here…)

No problem at all!!
I was just now working on this and have arrived at the following solution.
My next step is to streamline the code.

  • I would like to be able to incorporate a loop to generate a list of lerped colors so I don’t have to itemize each instance in the variables above setup. I assume this is possible? I may need to ask for help once further along.
  • And, variable float step = 1/(num-2) doesn’t work as i expected…
    Why isn’t it doing the division?
int w = 40, h = 400-40;
int num = 9;
float step = .1428;
//float step = 1/(num-2); not sure why this doesn't work??????

Cell [] cells = new Cell [num];

color from = color(random(255), random(255), random(255)); 
color to = color(random(255), random(255), random(255));
color c2 = lerpColor(from, to, step); 
color c3 = lerpColor(from, to, step*2);
color c4 = lerpColor(from, to, step*3);
color c5 = lerpColor(from, to, step*4);
color c6 = lerpColor(from, to, step*5);
color c7 = lerpColor(from, to, step*6);
color c8 = lerpColor(from, to, step*7);

color [] colors =
{
from, c2, c3, c4, c5, c6, c7, c8, to
};

void setup() {
  size (400, 400);
  
  int k = 0;

  for (int i = 0; i < num; i++) {
    cells[i] = new Cell(x+i*w, y, w, h, colors[k]);
    k++;
    }
  }

void draw() {
  
  background (51);

  for (int i = 0; i < num; i++) {
    cells[i].display();
  }
}

//////////////////////////

class Cell {
  float x, y, w, h;
  color clr;

  Cell ( 
    float tempX, float tempY, 
    float tempW, float tempH, color tempClr) {

    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    clr = tempClr;
  }
  void display() {
    
    stroke(255); 
    fill(clr);
    rect(x, y, w, h);
  }
}

When we use only int in a formula processing assumes we want int as a result. Bad.

write float step = 1.0f/(num-2)

1 Like

I looked up the use of f following the float number 1.0 and learned about floating point literals!
Thank you!!

1 Like