Ranjithkumar
Ranjithkumar

Reputation: 18386

Reduce brightness of colors in android?

I am working with AChart Engine.

And set the colors at runtime dynamically.

Here is my code,

private int getRandomColor() {

    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
    return color;
 }

My problem is I get brightness color also(randomly).

So how to avoid brightness color only?

My output:

enter image description here

Upvotes: 0

Views: 1605

Answers (2)

MatBos
MatBos

Reputation: 2390

If I understand you correctly and you want to avoid bright colours you have to change this line:

int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));  

to those lines:

int MAX_LEVEL = 128;
int color = Color.argb(255, rnd.nextInt(MAX_LEVEL), rnd.nextInt(MAX_LEVEL), rnd.nextInt(MAX_LEVEL));  

This will result in only darker colours being generated.

If you want only light colours just do it like this:

int BASE_LEVEL = 128;
int MAX_LEVEL = 128;
int color = Color.argb(255, BASE_LEVEL + rnd.nextInt(MAX_LEVEL), BASE_LEVEL + rnd.nextInt(MAX_LEVEL), BASE_LEVEL + rnd.nextInt(MAX_LEVEL));

Upvotes: 2

Rich
Rich

Reputation: 104

You might be better to use the HSB (Hue, Saturation, Brightness) color space rather than RGB. You can use getHSBColor() instead and hard-code the saturation and brightness to keep them all the same, just randomizing the color.

private Color getRandomColor() {
    Random rnd = new Random();
    return Color.getHSBColor(rnd.nextFloat(), 0.5f, 0.5f);
}

Upvotes: 5

Related Questions