17

I am trying to create a random color by randomly generating numbers for R,G, and B values with a random number generator, and using the values to make a color. The following code is in my onCreate() method:

Random rand = new Random();
    // Java 'Color' class takes 3 floats, from 0 to 1.
    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();
    Color randomColor = new Color(r, g, b);

How come eclipse tells me "The constructor Color(float, float, float) is undefined"? Shouldn't this work correctly?

ThatGuyThere
  • 365
  • 2
  • 10
  • 23
  • Have you correctly imported it? – Scary Wombat Dec 13 '13 at 07:06
  • 2
    Make sure you're declaring "java.awt.Color": http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html. "android.graphics.Color", on the other hand, does NOT have a constructor with float rgb: http://developer.android.com/reference/android/graphics/Color.html#rgb%28int – paulsm4 Dec 13 '13 at 07:07
  • Seems like eclipse auto-imported "android.graphics.Color". I made it import "java.awt.Color" and it is now working. Thank you – ThatGuyThere Dec 13 '13 at 07:10
  • @ThatGuyThere coding for android right? – Raghunandan Dec 13 '13 at 07:29

5 Answers5

46

You should use nextInt(int n):int to generate a random integer between 0 and 255. (note that according to API the range is not checked within the Color methods so if you don't limit it yourself you'll end up with invalid color values)

// generate the random integers for r, g and b value
Random rand = new Random();
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);

Then get an int color value with the static Color.rgb(r,g,b):int method. The only constructor that exists for android.graphics.Color is a non argument constructor.

int randomColor = Color.rgb(r,g,b);

Finally, as an example, use the setBackgroundColor(int c):void method to set a color background to a view.

View someView.setBackgroundColor(randomColor);
Michael
  • 9,639
  • 3
  • 64
  • 69
hcpl
  • 17,382
  • 7
  • 72
  • 73
  • `public static int rgb` return type is `int` – Raghunandan Dec 13 '13 at 07:16
  • 1
    indeed fixed that and added some more info on how to use the int color value to set background color to view for instance. – hcpl Dec 13 '13 at 09:52
  • the post looks more complete now. – Raghunandan Dec 13 '13 at 09:53
  • 2
    It should be rand.nextInt(256). nextInt "returns a pseudo-random uniformly distributed int in the half-open range [0, n)." – TechAurelian May 03 '14 at 17:59
  • 1 line code: `view.setBackgroundColor(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));` – Prabs Jan 03 '17 at 12:20
  • @Prabs One line of code isn't always better, especially when showing examples I would vote against the single line notation. – hcpl Jan 03 '17 at 12:33
  • @hcpl I feel it's a good practice, because we aren't allocating memory to variables which we will use only once. It might not be good for explanation but it is good for usage. Don't you think so? – Prabs Jan 04 '17 at 04:53
3
public int randomColor(int alpha) {

    int r = (int) (0xff * Math.random());
    int g = (int) (0xff * Math.random());
    int b = (int) (0xff * Math.random());

    return Color.argb(alpha, r, g, b);
}

can it help?

venciallee
  • 775
  • 4
  • 19
3

http://developer.android.com/reference/android/graphics/Color.html

Color()

Constructor does not take any params

Use

public static int rgb (int red, int green, int blue)

Return a color-int from red, green, blue components. The alpha component is implicity 255 (fully opaque). These component values should be [0..255], but there is no range check performed, so if they are out of range, the returned color is undefined.

Parameters red Red component [0..255] of the color green Green component [0..255] of the color blue Blue component [0..255] of the color

Use

Random rand = new Random();
int r = rand.nextInt(255);
...// rest of the code  
int randomcolor = Color.rgb(r,g,b); // takes int as param
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

Makeuse of Color.rgb() method

Color.rgb((randval)r,(randval)g,(randval)b);

to generate random color.

Nambi
  • 11,944
  • 3
  • 37
  • 49
1

If The constructor Color(float, float, float) is undefined than convert it into int like.

Random rand = new Random();
// Java 'Color' class takes 3 floats, from 0 to 1.
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
int Red = Integer.parseInt(String.valueOf(r));
int Green= Integer.parseInt(String.valueOf(g));
int Blue= Integer.parseInt(String.valueOf(b));
Color randomColor = new Color(Red , Green, Blue);

But Don't know that it works or not if doesn't work then try this:

Random rand = new Random();
int r = rand.nextInt(255);
int g = rand.nextInt(255);
int b = rand.nextInt(255);
Color randomColor = new Color(r, g, b);

It should works but if it doesn't works then comment.

Cropper
  • 1,177
  • 2
  • 14
  • 36