here is an example using modulo
please note that modulo is not used here to make an array but to read the content of an array (use it)
more precise, we have more slots on the screen to fill with a color than the color array has elements.
To avoid that we read over the end of the color array, we use % modulo, so that after the end of the array we restart reading the color array from the beginning (in setup() it’s this line: shuffledInventory[ i % shuffledInventory.length ]
).
Alternative to modulo would be (as I said) ... shuffledInventory[ k ] ...
and then
k++;
if(k>shuffledInventory.length)
k=0;
Chrisir
// global array
Cell [] cells = new Cell [39];
void setup() {
size (900, 400);
// NOT shuffled at the moment
color[] shuffledInventory = getColorListShuffled();
println(shuffledInventory.length);
int x = 20, y = x;
int w = (width-40)/cells.length, h = 360;
for (int i = 0; i < cells.length; i++) {
cells[i] = new Cell(x+i*w, y,
w, h,
shuffledInventory[ i % shuffledInventory.length ] );
}//for
}//func
void draw() {
background (51);
for (int i = 0; i < cells.length; i++) {
cells[i].display();
}//for
}//func
//-----------------------------------------------------------------------------------------
// Tools
color[] getColorListShuffled() {
// for the command lerpColor:
color from = color(random(255), random(255), random(255));
color to = color(random(255), random(255), random(255));
float step = 1.0f / (10-1); // **must use a float number to return a float number**
IntList inventory = new IntList(); // stores list of colors
// for loop to generate an IntList of random colors
for (int k = 0; k < 10; k++) {
inventory.set(k, lerpColor (from, to, k*step));
}
// inventory.shuffle(); // NOT randomly shuffles the list inventory
// directs the inventory
// as an array to
return inventory.array();
}
///////////////////////////////////////////////////////////////////////////////////////////
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);
}
//
}//class
//