We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have 5 colors defined as colors and I want to pick one of them randomly. Although I thought this was simple it doesn't seem so (unless it really is...?). I defined the 5 colors. I tried storing them in an array list, randomly picking a number between 1-5 and then use that to get the index of the array list. Probably is... "color" isn't a type although it is constructed like a type (int, float, etc.). color myColor = color(255, 120, 5);
I've tried putting them in hashmaps which worked well and was making a function to get them out only you can't "return" a color from a function. color (or Color) getRandomColor() (for example) doesn't work.
Any suggestions for this simple exercise?
Answers
What's wrong with a plain old array?
Thanks! I suppose after doing more research that "color" is a primitive and not a class and therefore cannot be stored in an ArrayList?
Arrays, arraylists, lists, linkedlists + many others I'm sure. Still getting used to it...!
color
is alias forint
primitive datatype:https://Processing.org/reference/color_datatype.html
https://Processing.org/reference/int.html
Java's Collection containers can't store primitive datatypes though!
For that we gotta use a primitive's corresponding wrapper class instead.
For
color
&int
, it is Integer:http://docs.Oracle.com/javase/8/docs/api/java/lang/Integer.html
http://docs.Oracle.com/javase/8/docs/api/java/lang/Number.html
BtW, Processing 2 series offers alternative classes for
ArrayList<Integer>
,ArrayList<Float>
&ArrayList<String>
. All backed up w/ real primitives! Check them out:https://processing.org/reference/IntList.html
https://processing.org/reference/FloatList.html
https://processing.org/reference/StringList.html (Not primitive of course! :P)
Thanks for the info. Here's another quick question related. What if I want to store a random color in an evictingqueue (data structure of n size that take out the nth object when adding a new one). I think it is derived from a collection. http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/EvictingQueue.html
You can't do EvictingQueue'<color'>, for instance. (without the ' ' )
So I made my own class:
and then I can at least use EvictingQueue'<RandomColor'>. (without the ' ')
All I really want to store is something like {r, g, b} like Toxi's Vec3D, an object I guess. In java, this isn't obvious though.
The complexitiy might be hendering my performance too. 100 of these random colors in an evicting queue drawing circles at 200 points on the screen = 20,000 circles per frame. Maybe that goes slow anyways...
int
, and consequently its aliascolor
, is Integer.