Using defined colors from an array

I am trying to figure out how to access the 5 grays in the array randomly to fill() the arc shape and rectangle.

Seems like it should be pretty easy to do but…
And haven’t been able to find anything in my books or online that has worked yet.
I’ve commented next to problem areas.

Any hints / guidance most welcome!

// halfmoon motif
import processing.pdf.*;

boolean record;

color c1 = #FFFFFF;
color c2 = #BEBEBE;
color c3 = #7F7F7F;
color c4 = #3F3F3F;
color c5 = #000000;

color [] grays = {c1, c2, c3, c4, c5};

float deg = 90; // 90 degrees
float rad = radians(deg); // convert to radians

void setup() {
  size(400, 400);
  
//______________________??? I should somehow initialize the array list here ???
  grays [0] = #FFFFFF;          // kind of lost here
  grays [1] = #BEBEBE;
  grays [2] = #7F7F7F;
  grays [3] = #3F3F3F;
  grays [4] = #000000;


  //grays = new color (grays.length);
  //for (int c = 0; c < grays.length; c++)
//______________________________???
  
frameRate(1);
}

void draw() {
  if (record) {
    beginRecord (PDF, "frame-####.pdf");
  }

  background (127);
  noStroke();

  float r = random(1);
  println (r);

  translate (200, 200);

  if (r < 0.40) {
    rotate (rad);
  } else if (r > 0.40 && r < 0.50) {
    rotate (rad*3);
  } else if (r > 0.50 && r < 0.65) {
    rotate (rad*2);
  } else {
    rotate (rad*4);
  }

  fill (int(random(grays))); // wrong, Would like a random selection from gray array list
  rectMode (CENTER);
  rect (0, 0, 200, 200);

  fill (grays); // wrong, Would like a random selection from gray array list

  arc (0, -100, 200, 200, 0, PI);

  if (record) {
    endRecord();
    record = false;
  }
}

void mousePressed() { // make PDF of current frame
  record = true;
}
1 Like

fill(grays[int(random(grays.length))]);

1 Like

Hello,

An example:

color c1 = #FFFFFF;
color c2 = #BEBEBE;
color c3 = #7F7F7F;
color c4 = #3F3F3F;
color c5 = #000000;

color [] grays = {c1, c2, c3, c4, c5};

int num = 0;

void setup() 
  {
  size(500, 500);
  }

void draw() 
  {
  //background(0);
  if (frameCount%60 == 0)
    {
    num = int(random(0, 5));
    println(num, hex(grays[num]));
  	}
  fill(grays[num]);
  //println(hex(grays[num]));
  circle(width/2, height/2, 300);
  }

:)

3 Likes

I’m going to try both approaches right now.
Thank you @Chrisir and @glv!!
Very much appreciated!!
:nerd_face:

1 Like

I should somehow initialize the array list here ??? // kind of lost here

there are different ways :

valid:

before setup() :

color[] grays = new color [5];

in setup() :

  grays [0] = #FFFFFF;         
  grays [1] = #BEBEBE;
  grays [2] = #7F7F7F;
  grays [3] = #3F3F3F;
  grays [4] = #000000;

or like this

color[] grays = {
  #FFFFFF, 
  #BEBEBE, 
  #7F7F7F, 
  #3F3F3F, 
  #000000
}; 

Full Code





color[] grays = {
  #FFFFFF, 
  #BEBEBE, 
  #7F7F7F, 
  #3F3F3F, 
  #000000
}; 

float deg = 90; // 90 degrees
float rad = radians(deg); // convert to radians

void setup() {
  size(400, 400);
  frameRate(1);
}

void draw() {

  background (127);
  noStroke();

  float r = random(1);
  // println (r);

  translate (200, 200);

  if (r < 0.40) {
    rotate (rad);
  } else if (r > 0.40 && r < 0.50) {
    rotate (rad*3);
  } else if (r > 0.50 && r < 0.65) {
    rotate (rad*2);
  } else {
    rotate (rad*4);
  }

  // Would like a random selection from gray array list
  int index = int(random(grays.length));  
  println(index); 
  fill(grays[index]);

  // rect 
  rectMode (CENTER);
  rect (0, 0, 200, 200);

  arc (0, -100, 200, 200, 0, PI);
}
2 Likes

This works nicely! Thank you again.
:grinning:

This works nicely too!
:grinning:

Quick question:

The “60” in this line. Is that chosen because it’s a multiple of 5 referring to the 5 elements in the array?

Or it could be any number? As frameCount%60 == 0 will equal “0” for the first 60 frames therefore,

will generate a random number 60 times?

Just want to make sure I understand what is happening under the hood.

1 Like

Good to know. That is part of my challenge. Not knowing which path is best to follow…

1 Like

if (frameCount%60 == 0) means:

  • only when the modulo (remainder) of a division by 60 is 0, a new random number is produced.

So in frame 60, then 120, and so on.

So, approx. once per second only

2 Likes

identical:

  // Would like a random selection from gray array list
  int index = int(random(grays.length));  
  println(index); 
  fill(grays[index]);
2 Likes

Ah yes, that makes much more sense regarding the modulo. Thank you!
:nerd_face:

Hello,

It is only part of the frameRate counter.

Reference:
https://processing.org/reference/modulo.html

Lots of uses for modulo:
Make modulo your friend! Concepts can be adapted for Processing.

And further on the subject:

:)

4 Likes

Thanks for this interesting link! Much appreciated. :slight_smile: