DoTheGenes
DoTheGenes

Reputation: 197

How can I generate a list of semi-random colours?

I want to go through a list of random generated colours and check if they differ from each other. Not as in colour1 == colour2, but making sure that the generated colours are not too similar.

I mean to use this code (or something like it) to generate the colourlist:

Random randomGenerator = new Random();
ArrayList<Color> colours = new ArrayList<Color>();
while(true) {
    int red = randomGenerator.nextInt(255);
    int green = randomGenerator.nextInt(255);
    int blue = randomGenerator.nextInt(255);
    Color randomColour = new Color(red,green,blue);
    if(!colours.contains(randomColour)) {
        colours.add(randomColour);
    }
    if(colours.size() >= 100) {
        break;
    }
}

Upvotes: 0

Views: 241

Answers (1)

Mc Kevin
Mc Kevin

Reputation: 972

My first thought is something like this, it's pseudo random

    Random randomGenerator = new Random();
    ArrayList<Color> colours = new ArrayList<Color>();
    int k1 = 0, k2=0,  k3 = 0;
    while(true) {
        k1 = k1 +1 <= 4 ? k1 +1 : (k1+1)%4;
        k2 = k2 +2 <= 4 ? k2 +2 : (k2+2)%4;
        k3 = k3 +3 <= 4 ? k3 +3 : (k3+3)%4;
        int red = randomGenerator.nextInt(255/4*k1);
        int green = randomGenerator.nextInt(255/4*k2);
        int blue = randomGenerator.nextInt(255/4*k3);
        Color randomColour = new Color(red,green,blue);
        System.out.println("R:" +randomColour.getRed()+"G:" +randomColour.getGreen()+"B:" +randomColour.getBlue());
        if(!colours.contains(randomColour)) {
            colours.add(randomColour);
        }
        if(colours.size() >= 100) {
            break;
        }

    }

Upvotes: 2

Related Questions