ariken929
ariken929

Reputation: 67

how to generate random numbers of random colors for canvas.drawColor

I am still new at android development and all the app i've been dealing with so far are not graphical related. Now i am making a app that displays a graph, pie graph to be exact and im making this without any 3rd party libs. This is the tutorial which i followed.

Now the problem is the data which i will be using to construct the graph is dynamic, and so to assign each item with a color for the graph i need to generate a number of random colors for the canvas.drawColor. the number of colors will of course depend on my dynamic data which is determined at run time and the color value also needs to be generated. Also it would be nice if the colors generated are vibrant colors that stands out.

I've never dealt with canvas, any idea how this can be done? Example code would be much appreciated.

Thanks

Upvotes: 0

Views: 6802

Answers (3)

aioobe
aioobe

Reputation: 420991

I wouldn't use just completely random colors. You'd get completely different saturations and brightnesses which will look plain ugly.

You could fix a saturation (choose a high value for vibrant colors) and brightness and choose a random hue value, but you would run the risk of getting two consecutive pie-slices with almost the same color.

I would compute a set of spread out hue-values as shown in the answer to this question:

Reposting the answer below


import java.awt.*;

public class TestComponent extends JPanel {

    int numCols = 6;

    public void paint(Graphics g) {

        float h = 0, dh = (float) getHeight() / numCols;
        Color[] cols = getDifferentColors(numCols);

        for (int i = 0; i < numCols; i++) {
            g.setColor(cols[i]);
            g.fillRect(0, (int) h, getWidth(), (int) (h += dh));
        }
    }

    public static Color[] getDifferentColors(int n) {
        Color[] cols = new Color[n];
        for (int i = 0; i < n; i++)
            cols[i] = Color.getHSBColor((float) i / n, 1, 1);
        return cols;
    }

    public static void main(String s[]) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new TestComponent());
        f.setSize(400, 400);
        f.setVisible(true);
    }
}

enter image description here

If you need like more than 30 colors, you could of course change the brightness and perhaps the saturation as well, and have, for instance, 10 dark colors, 10 midtone colors, and 10 bright colors.

Upvotes: 1

user717572
user717572

Reputation: 3652

You should take a random float for each color component:

Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();

Since you want vibrant colors, you could use the Color.brighter() method.

Color c = new Color(r, g, b);
c = c.brighter();   

Otherwise you could set a threshold, so that the color values have a minimum value (higher is brighter):

float minimumBrightness = 0.6; //higher is more vibrant
float r = rand.nextFloat(1-minimumBrightness) + minimumBrightness;  //for each  component

This will ensure vibrant colors.

Upvotes: 1

Snicolas
Snicolas

Reputation: 38168

It would be quite hard to get different and usefull colors at runtime. The best is not to use so much randomness, but make an array of let's say 20 fixed colors quite distinct and use the X first ones for your data. If you need more, then maybe use real random colors using random RGB values :

Random r = new Random();
Color c = new Color( r.nextInt(255), r.nextInt(255), r.nextInt(255) );

Upvotes: 2

Related Questions