Appending more than one color to an array loop of colors

I want to append black and white to an array of lerped colors. Works okay when I append the first color, but not when I append the second color. What am I doing wrong?

// MAIN -----------------------------

Cell [] cells = new Cell[9];

void setup() {
  size (400, 600);
  color [] shuffledInventory = getColorListShuffled();

  int x = 0, y = x;
  int w = width, h = height/cells.length;

  for (int i = 0; i < cells.length; i++) {
    cells[i] = new Cell (x, y+i*h, w, h, shuffledInventory[i]);
  }
}
void draw() {
  background (51);

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

color [] getColorListShuffled() {

  color from = color (random(255), random(255), random(255));
  color to = color (random(255), random(255), random(255));
  float step = 1.0f / (cells.length -2); // *** when I append second color I change to cells.length -3 
  IntList inventory_1 = new IntList (cells.length);


  for (int k = 0; k < cells.length; k++) {
    inventory_1.set (k, lerpColor (from, to, k*step));
    inventory_1.append (color (255));
    //inventory_1.append (color (0)); *** does not work when I add second color
  }
  inventory_1.shuffle();
  return inventory_1.array();
}
// CLASS ----------------------------------------------

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(){
    noStroke ();
    fill (clr);
    rect (x, y, w, h);
  }
}
1 Like
color [] getColorListShuffled() {

  color from = color (random(255), random(255), random(255));
  color to = color (random(255), random(255), random(255));
  float step = 1.0f / (cells.length -2); // *** when I append second color I change to cells.length -3 
  IntList inventory_1 = new IntList (cells.length);


  for (int k = 0; k < cells.length; k++) {
    inventory_1.set (k, lerpColor (from, to, k*step));
    inventory_1.append (color (255));
    //inventory_1.append (color (0)); *** does not work when I add second color
  }
  inventory_1.shuffle();
  return inventory_1.array();
}

Just my guess, but it looks like you are reinitializing the array every time. Therefore you can only ever add one item.

IntList inventory_1 = new IntList (cells.length);

try placing that line of code somewhere outside of the loop.

Hello @paulgoux ,
I’m not sure what you mean by reinitializing the array every time.
Isn’t it already outside of the for loop?

everytyime you call the getColorListShuffled() function you initialize the array IntList inventory_1 = new IntList (cells.length);. Meaning it cannot ever be bigger than one.

Even if its not called inside a loop, its called inside a function which is told to reset it everytime.

Does that mean I would need to add the second color outside the getColorListShuffled() function?

Ok so I slightly misunderstood, the nature of intList there I thought it worked exactly like an arrayList.

Cannot say why this won’t allow you to append twice, however could you use the following approach

for (int k = 0; k < cells.length*2; k++) {
    //inventory_1.set (k, lerpColor (from, to, k*step));
    if(k&2==0)inventory_1.append (lerpColor (from, to, k*step));
    else inventory_1.append (color (255));
    //inventory_1.append (color (0)); *** does not work when I add second color
  }

I might be misunderstanding what you are trying to achieve though.

Final check

Cell [] cells = new Cell[9];
IntList inventory_1;
void setup() {
  size (400, 600);
  color [] shuffledInventory = getColorListShuffled();

  int x = 0, y = x;
  int w = width, h = height/cells.length;

  for (int i = 0; i < cells.length; i++) {
    cells[i] = new Cell (x, y+i*h, w, h, shuffledInventory[i]);
  }
}
void draw() {
  background (51);

  for (int i = 0; i < cells.length; i++) {
    cells[i].display();
  }
  fill(0);
  text(inventory_1.size(),100,100);
}

color [] getColorListShuffled() {

  color from = color (random(255), random(255), random(255));
  color to = color (random(255), random(255), random(255));
  float step = 1.0f / (cells.length -2); // *** when I append second color I change to cells.length -3 
   inventory_1 = new IntList (0);


  for (int k = 0; k < cells.length; k++) {
    inventory_1.set (k, lerpColor (from, to, k*step));
    //inventory_1.append (lerpColor (from, to, k*step));
    inventory_1.append (color (255));
    inventory_1.append (color (0)); 
    inventory_1.append (color (0)); 
    //*** does not work when I add second color
  }
  inventory_1.shuffle();
  return inventory_1.array();
}
// CLASS ----------------------------------------------

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(){
    noStroke ();
    fill (clr);
    rect (x, y, w, h);
  }
}

This is what I get with three append cycles. The size of the list.size() is 3* bigger than cell.length - 1

1 Like

Getting an error message with this "The operator & is undefined for the argument type(s) int, boolean.

Also in first line:

If I am understanding correctly, doesn’t that double the array length? Whereas I just want to add 2 colors…
:thinking:

Finally got this to work. Posting solution in case anyone else may be seeking how to add specific color(s) to a color array generated with a for loop, and then randomly shuffle.
And thank you @paulgoux!, your suggestions did help me think about this with fresh eyes. :slightly_smiling_face:

color [] getColorListShuffled() {

  color from = color (random(255), random(255), random(255));
  color to = color (random(255), random(255), random(255));
  float step = 1.0f / (cells.length-2);
  IntList inventory_1 = new IntList (cells.length);

  for (int k = 0; k < cells.length-2; k++) {
    inventory_1.set (k, lerpColor (from, to, k*step));
    
  }
  inventory_1.append(#000000); // add black to the array
  inventory_1.append(#ffffff); // add white to the array
  inventory_1.shuffle(); // all colors shuffled
  

  return inventory_1.array();
}
2 Likes