Creating random colour in Java?

253,751

Solution 1

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);

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() / 2f;
float b = rand.nextFloat() / 2f;

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() / 2f + 0.5;
float g = rand.nextFloat() / 2f + 0.5;
float b = rand.nextFloat() / 2f + 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

Solution 2

A one-liner for random RGB values:

new Color((int)(Math.random() * 0x1000000))

Solution 3

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);

Solution 4

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);

Solution 5

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 from 0 to 255 and then use the Color(int,int,int) constructor to create a new Color instance.

Random randomGenerator = new Random();
int red = randomGenerator.nextInt(256);
int green = randomGenerator.nextInt(256);
int blue = randomGenerator.nextInt(256);

Color randomColour = new Color(red,green,blue);
Share:
253,751
Elton.fd
Author by

Elton.fd

Updated on July 05, 2022

Comments

  • Elton.fd
    Elton.fd almost 2 years

    I want to draw random coloured points on a JPanel in a Java application. Is there any method to create random colours?

  • sje397
    sje397 over 13 years
    And this way you can easily avoid points that are the same colour as the background.
  • Elton.fd
    Elton.fd over 13 years
    It's great. but, what can i do for creating lighter color?
  • Andrew
    Andrew over 13 years
    you can use the Color.brighter() method to make any generated color look like.
  • Stijn de Witt
    Stijn de Witt over 13 years
    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.
  • Jay
    Jay over 13 years
    @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
    Jay over 13 years
    Hey, whoever downvoted me, you might at least post a comment to say what your objection was. Ah well, my boss doesn't check my Stackoverflow score before filling out my paycheck, so I don't know why I care.
  • Shaun Wild
    Shaun Wild over 10 years
    Ahh. That's why I must have just skipped over it. The extra colour won't make a difference however :P
  • Xitcod13
    Xitcod13 almost 10 years
    your code for will only produce bright/light colors will not work. next float does not accept float nor double as an argument and adding .5 makes color go out of range.
  • Thomas Ahle
    Thomas Ahle almost 10 years
    Do you have a sample with some of the colors this generates?
  • Greg
    Greg almost 10 years
    @ThomasAhle It will produce any and every possible colour. I can't draw that in a picture. :)
  • giaffa86
    giaffa86 about 9 years
    As suggested above, doesn't exist anymore a nextFloat constructor that accepts a parameter.
  • patrickGranite
    patrickGranite about 8 years
    I prefer this way (HSB) of picking a random color. Using the RGB system can create very weird combinations. As a side note, the hue value is internally multiplied by 360 to produce the hue angle in the HSB color model (Javadoc).
  • kabuto178
    kabuto178 almost 8 years
    If you want a more "purple" tone of colors what would be the code changes?
  • Greg
    Greg almost 8 years
    Purple is made up of blue and red, so increasing the amount of those two colours, or decreasing the amount of green will make it a more purple tone. Hope this helps.
  • Ryan Pelletier
    Ryan Pelletier over 5 years
    Thanks, I know how to do this...but I didn't feel like it and you saved me some time!