# Color selected from pallette randomly for ellipses drawn by an array

Hey there Guys,

I was wondering if you could offer me some insight in to how I can color each ellipse randomly that is drawn by an array.

Currently I have created a second array for colors which it draws from when the sketch is launched but each of the ellipse comes out the same color. How can I make each ellipse select the colour randomly. Here is some of the code.

``````void setup() {

index = int(random(colors.length));
size(500, 500);

for (int i = 0; i < words.length; i++) // X Values
{
words[i] = new word(width / 2, height / 2, i*2, index );
}

}

void draw() {
background(yellow);

for (int i = 0; i < words.length; i++) // X Values
{
words[i].display();
words[i].floatAimlessly();

}
}

``````

// Draw the blob to the canvas
void display() { // Defines how the words will be displayed

pushMatrix();
textSize(70);
noStroke();
//fill(random(255,200),random(100,255),0);
fill(colors[index]);
ellipse(pos.x,y,r,r);
//text(“Array.”, pos.x, y); // Specify a z-axis value
popMatrix();

}

Here is an image, you can see each ellipse is the same color forming one large shape. I’d like to select from these colors.

``````color yellow = color(255, 188, 103);
color red = color(218, 114, 126);
color pink = color(172, 108, 130);
color violet = color(104, 92, 121);
color blue = color(62, 78, 89, 100);
``````
1 Like

Hi,

Start by putting all your colors in an array.
Then randomly pick an integer in the range of the size of your color array.
Then use that random number as your index to pick a color in your array.

Small code example:

``````color[] myColors;

void setup() {
size(1280, 1024);
background(20);
noStroke();

// Defining the colors
myColors = new color[5];
myColors[0] = color(255, 188, 103);
myColors[1] = color(218, 114, 126);
myColors[2] = color(172, 108, 130);
myColors[3] = color(104, 92, 121);
myColors[4] = color(62, 78, 89, 100);

// Creating the ellipse with random colors:
for (int i = 0; i < 100; i++) {
int randomColorIndex = (int)random(myColors.length);
color fillColor = myColors[randomColorIndex];
fill(fillColor);
float ellipseSize = random(50, 100);
ellipse(random(width), random(height), ellipseSize, ellipseSize);
}
}
``````
3 Likes

Amazing, Thanks so much for you help! I’ll get back to work on it and see how it goes