Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Colors???HELP!

 
Annette Pomeroy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if anyone could tell me which method would i use to randomly generate colors, when i am not sure how many times i need it generated.
 
Rowan Brownlee
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Annette,
The closest I've gotten to this is setting up an array of colours and cycling through them over and over. Though this isn't random, I suppose you could repeatedly generate a random number between 0 and the number of colours in the array to choose a colour at random.
I haven't tried to generate customised colours, but looking at the constructors in the Color class, it looks like you can feed in your own (randomly generated )numbers to make a colour. For generating random numbers, there's the Random class, and Math.random.
Constructor example from Color API
~~~~~~~~~
Color(int r, int g, int b, int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
~~~~~~~~~
cheerio
rowan
 
Annette Pomeroy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help, I appreciate it much!
I will try what u have told me.
Thanks again,
Annette
 
Rikard Qvarforth
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maby you could do something like this ..
Color ColorTest(){
int R = 0;
int G = 0;
int B = 0;
R = (int)(1+Math.random()*(255));
G = (int)(1+Math.random()*(255));
B = (int)(1+Math.random()*(255));
return new Color(R,G,B));
}
hope it helps ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic