7/27/2019 Clase Random Para Colores
1/16
9 Answers
activeoldestvotes
up vote38down voteaccepted
Use the random library:
import java.util.Random;
Then create a random generator:
Random rand = new Random();
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);
7/27/2019 Clase Random Para Colores
2/16
--------------------------------------------------------------------------------
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();
An overview of the Color class can be read here:
http://download.oracle.com/javase/6/docs/api/java/awt/Color.html
share|improve this answer
7/27/2019 Clase Random Para Colores
3/16
edited Nov 22 '10 at 14:59
answered Nov 22 '10 at 14:28
Greg
6,89322754
7/27/2019 Clase Random Para Colores
4/16
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);
share|improve this answer
answered Nov 22 '10 at 15:45
7/27/2019 Clase Random Para Colores
5/16
Sualeh Fatehi
1,039612
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);
7/27/2019 Clase Random Para Colores
6/16
int blue = randomGenerator.nextInt(255);
Color randomColour = new Color(red,green,blue);
share|improve this answer
answered Nov 22 '10 at 14:26
Rob Stevenson-Leggett
11.3k94392
And this way you can easily avoid points that are the same colour as the background. sje397 Nov
22 '10 at 14:29
7/27/2019 Clase Random Para Colores
7/16
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);
share|improve this answer
answered Jan 5 '12 at 7:54
7/27/2019 Clase Random Para Colores
8/16
Komplot
711
Works great, thanks Komplot! Sahil Muthoo Jan 27 '12 at 12:04
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;
}
7/27/2019 Clase Random Para Colores
9/16
You can see the code at work here.
share|improve this answer
edited Sep 22 '12 at 0:44
Sam
37.5k72346
answered Sep 21 '12 at 19:33
Brenden
511
7/27/2019 Clase Random Para Colores
10/16
1
can't you see he wants it in java..
Atharva Johri Apr 4 at 9:06
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());
I can't guarantee they'll all be pretty, but they'll be random =)
share|improve this answer
7/27/2019 Clase Random Para Colores
11/16
answered Nov 22 '10 at 14:27
Jason Nichols
3,52821133
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);
7/27/2019 Clase Random Para Colores
12/16
int blue=random.nextInt(256);
return new Color(red, green, blue);
}
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.
share|improve this answer
answered Nov 22 '10 at 14:30
Jay
12.8k22347
It's great. but, what can i do for creating lighter color? Elton.fd Nov 22 '10 at 14:44
7/27/2019 Clase Random Para Colores
13/16
you can use the Color.brighter() method to make any generated color look like. Andrew Nov 22
'10 at 14:48
1
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
yes, It works nice, thanks :) Elton.fd Nov 22 '10 at 15:03
@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
show 2 more comments
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
7/27/2019 Clase Random Para Colores
14/16
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
);
Search for HSB color model for more information.
share|improve this answer
answered Nov 22 '10 at 15:37
Ishtar
4,699820
7/27/2019 Clase Random Para Colores
15/16
up vote0down vote
/** * */ package com.adil.util;
/** * 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)];
7/27/2019 Clase Random Para Colores
16/16
}
return color;
}