Professional Documents
Culture Documents
Clase Random para Colores
Clase Random para Colores
activeoldestvotes
up vote38down voteaccepted
As colours are separated into red green and blue, you can create a new random colour by creating random primary colours: // Java 'Color' class takes 3 floats, from 0 to 1. float r = rand.nextFloat(); float g = rand.nextFloat(); float b = rand.nextFloat();
Then to finally create the colour, pass the primary colours into the constructor: Color randomColor = new Color(r, g, b);
--------------------------------------------------------------------------------
You can also create different random effects using this method, such as creating random colours with more emphasis on certain colours ... pass in less green and blue to produce a "pinker" random colour. // Will produce a random colour with more red in it (usually "pink-ish") float r = rand.nextFloat(); float g = rand.nextFloat(0.5); float b = rand.nextFloat(0.5);
Or to ensure that only "light" colours are generated, you can generate colours that are always > 0.5 of each colour element: // Will produce only bright / light colours: float r = rand.nextFloat(0.5) + 0.5; float g = rand.nextFloat(0.5) + 0.5; float b = rand.nextFloat(0.5) + 0.5;
There are various other colour functions that can be used with the Color class, such as making the colour brighter: randomColor.brighter();
Greg 6,89322754
up vote13down vote
If you want pleasing, pastel colors, it is best to use the HLS system. final float hue = random.nextFloat(); // Saturation between 0.1 and 0.3 final float saturation = (random.nextInt(2000) + 1000) / 10000f; final float luminance = 0.9f; final Color color = Color.getHSBColor(hue, saturation, luminance);
up vote4down vote
If you don't want it to look horrible I'd suggest defining a list of colours in an array and then using a random number generator to pick one.
If you want a truly random colour you can just generate 3 random numbers between 0 and 255 and then use the Color(int,int,int) constructor to create a new Color instance. Random randomGenerator = new Random(); int red = randomGenerator.nextInt(255); int green = randomGenerator.nextInt(255);
And this way you can easily avoid points that are the same colour as the background. sje397 Nov 22 '10 at 14:29
up vote4down vote
Copy paste this for bright pastel rainbow colors int R = (int)(Math.random()*256); int G = (int)(Math.random()*256); int B= (int)(Math.random()*256); Color color = new Color(R, G, B); //random color, but can be bright or dull
//to get rainbow, pastel colors Random random = new Random(); final float hue = random.nextFloat(); final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull final float luminance = 1.0f; //1.0 for brighter, 0.0 for black color = Color.getHSBColor(hue, saturation, luminance);
Komplot 711
up vote3down vote
I use this method: function getRandomColor() { var letters = '0123456789ABCDEF'.split(''); var color = '#'; for (var i = 0; i < 6; i++ ) { color += letters[Math.round(Math.random() * 15)]; } return color; }
Sam 37.5k72346
Brenden 511
up vote1down vote
You can instantiate a color with three floats (r, g, b), each between 0.0 and 1.0: http://download.oracle.com/javase/6/docs/api/java/awt/Color.html#Color(float,%20float,%20floa t).
Using Java's Random class you can easily instantiate a new random color as such: Random r = new Random(); Color randomColor = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
up vote1down vote
Sure. Just generate a color using random RGB values. Like: public Color randomColor() { Random random=new Random(); // Probably really put this somewhere where it gets executed only once int red=random.nextInt(256); int green=random.nextInt(256);
You might want to vary up the generation of the random numbers if you don't like the colors it comes up with. I'd guess these will tend to be fairly dark.
Jay 12.8k22347
It's great. but, what can i do for creating lighter color? Elton.fd Nov 22 '10 at 14:44
you can use the Color.brighter() method to make any generated color look like. Andrew Nov 22 '10 at 14:48
Sandra, to influence the brightness, make sure the random values are never very dark. 0 is darkest and 255 is brightest, so just do a random.nextInt(128) + 128 for example to never get any colers darker than half brightness. Stijn de Witt Nov 22 '10 at 14:49
@Stijn: Ditto. I might add that if you want more uniform brightness, you might make the 2nd value depend on the 1st and the 3rd depending on the first two. Like say red=nextInt(255); green=nextInt(255-red); etc. You could play with this sort of thing endlessly until you get the results you want. Jay Nov 23 '10 at 17:22
up vote1down vote
You seem to want light random colors. Not sure what you mean exactly with light. But if you want random 'rainbow colors', try this
Random r = new Random(); Color c = Color.getHSBColor(r.nextFloat(),//random hue, color 1.0,//full saturation, 1.0 for 'colorful' colors, 0.0 for grey 1.0 //1.0 for bright, 0.0 for black );
Ishtar 4,699820
up vote0down vote
/** * The Class RandomColor. * * @author Adil OUIDAD */ public class RandomColor { /** * Constructeur par dfaut. */ public RandomColor() {
/** * Gets the random color. * * @return the random color */ public static String getRandomColor() { String[] letters = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; String color = "#"; for (int i = 0; i < 6; i++ ) { color += letters[(int) Math.round(Math.random() * 15)];
} return color; }