8

ランダムカラージェネレーターを作成しようとしていますが、同様の色をarrayListに表示したくありません

public class RandomColorGen {

public static Color RandColor() {
    Random rand = new Random();
    float r = rand.nextFloat();
    float g = rand.nextFloat();
    float b = rand.nextFloat();
    Color c = new Color(r, g, b, 1);
    return c;

}

public static ArrayList<Color> ColorList(int numOfColors) {
    ArrayList<Color> colorList = new ArrayList<Color>();
    for (int i = 0; i < numOfColors; i++) {
        Color c = RandColor();
        if(similarcolors){
            dont add
        }
        colorList.add(c);

    }
    return colorList;
}

}

私は本当に混乱しています助けてください:)

4

2 に答える 2

14

Color クラスに similarTo() メソッドを実装します。

次に使用します。

public static ArrayList<Color> ColorList(int numOfColors) {
    ArrayList<Color> colorList = new ArrayList<Color>();
    for (int i = 0; i < numOfColors; i++) {
        Color c = RandColor();
        boolean similarFound = false;
        for(Color color : colorList){
            if(color.similarTo(c)){
                 similarFound = true;
                 break;
            }
        }
        if(!similarFound){
            colorList.add(c);
        } 

    }
    return colorList;
}

similarTo を実装するには:

RGBA 色空間での色の類似性/距離とプログラムによる類似色の検索をご覧ください。簡単なアプローチは次のとおりです。

((r2 - r1) 2 + (g2 - g1) 2 + (b2 - b1) 2 ) 1/2

と:

boolean similarTo(Color c){
    double distance = (c.r - this.r)*(c.r - this.r) + (c.g - this.g)*(c.g - this.g) + (c.b - this.b)*(c.b - this.b)
    if(distance > X){
        return true;
    }else{
        return false;
    }
}

ただし、類似の想像に従って X を見つける必要があります。

于 2013-03-07T03:15:15.283 に答える