Assign random color to each circle

I am trying to complete a school project and this is what I have right now (see below). I want to assign a random color to each circle every time - but right now, I am stuck at where the same color is being assigned to all the circles (a random color each time)… Can anyone help me? I want each circle to have a different shade of brown :frowning:

Tribble[] tribble = new Tribble[50];
color aColor = color( random(255),random(255),random(255) );

void setup() {
  size(400, 400);
  background(255);
  frameRate(30);
  for (int i = 0; i < tribble.length; i++) {
    tribble[i] = new Tribble(random(0, 400), random(0, 400));
  }
}

void draw() {
  background(255);
  tribble[(int)random(tribble.length)].excited();
  for (int i=0; i<tribble.length; i++) {
    tribble[i].display();
  }
}


class Tribble {
  float xPos, yPos;
  int start;
  float time;
  int offset;
  boolean wakeup;


  Tribble(float x, float y) {
    xPos = x;
    yPos = y;
  }
  //Makes the tribble excited
 void excited() {
time = (frameCount/60) % 7;
if (time >= 4 && time < 6) xPos += random(-1, 1);
}

  //Draws the tribble.
  void display() {
    ellipseMode(CENTER);
    fill(aColor);
    ellipse(xPos, yPos, 50, 50);
  }
}
1 Like

Why don’t you make the variable aColor a field of your class Tribble as well? :roll_eyes:

1 Like

An online example sketch w/ colored balloons: :sunglasses:
http://Studio.ProcessingTogether.com/sp/pad/export/ro.9FqnMDLUZqQCV

1 Like

I’m a little confused because in my code I’m constructing 200 circles at once

color aColor can be an attribute in your Tribble class. In the constructor of your class, you can assign it by:

...
  color aColor;
  Tribble(float x, float y) {
    xPos = x;
    yPos = y;
    aColor = color(random(255),random(255),random(255))
  }
...

And now, when you’re calling fill(aColor) in your display function, you should be getting different colors. You can also experiment with different color spaces like colorMode(HSB) and have acolor=color(random(255),255,255) so that you specify a certain range of hue you like.

1 Like

You’ve only got 1 aColor variable:
color aColor = color( random(255),random(255),random(255) );

Instead, turn it into an additional field for your class Tribble, like all the other 6 fields already are:

class Tribble {
  float xPos, yPos, time;
  int start, offset;
  boolean wakeup;
  color aColor = (int) random(#000000);
}

This way, each instance of Tribble got its own aColor variable. :upside_down_face:

1 Like

How does the

(int) random(#000000);

work? Isn’t that saying pick a number between 0 and 0? or am I understanding that completely wrong? :thinking:

Try out: println(#000000); on Processing’s IDE (PDE): -16777216
https://Processing.org/reference/color_datatype.html

2 Likes