1

I'm trying to create an array which includes separate distinct colors. Color array will created automatically when the range 'n' given. It's something as following:

variable n = 2;
colourarrray = [red,green];

variable n = 4;
colourarrray = [red,green,blue,yellow];

What is the easiest method to generate such a color array?

DT7
  • 1,615
  • 14
  • 26
nrnw
  • 441
  • 6
  • 13
  • Where is your Java code? –  Oct 02 '13 at 14:10
  • You might create a color spectrum using a `LinearGradientPaint` as seen in [this answer](http://stackoverflow.com/a/6996263/418556). Then you could get the relevant color using a loop and incrementing the `x` value used to get the corresponding color in the gradient. – Andrew Thompson Oct 02 '13 at 14:19

2 Answers2

0

An enum. Because it is scalable.

public enum Colors 
{
    BLACK(255, 255, 255),
    WHITE(0, 0, 0);

    private int red;  
    private int green;
    private int blue;

    private Colors(final int red, final int green, final int blue)
    {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }

    public int red()
    {
        return red;
    }

    public int green()
    {
        return green;
    }

    public int blue()
    {
        return blue;
    }
}

Then dinamically add them to a List<Colors> as you need them.

Georgian
  • 8,795
  • 8
  • 46
  • 87
0

Since you didn't put any specification, additional infos or anything to your question and I have nothing better to do right now:

private java.util.Random rnd = new java.util.Random();

public java.awt.Color[] getColors(int num) {
    java.util.List<java.awt.Color> colors = new java.util.ArrayList<>(num);
    int i = 0;
    while (i++ < num) {
        colors.add(new java.awt.Color(rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255), 100));
    }
    java.awt.Color[] array = colors.toArray(new java.awt.Color[num]);
    return array;
}
mrak
  • 2,826
  • 21
  • 21