LoginSignup
1
2

More than 3 years have passed since last update.

いい感じのランダムの色が欲しいなぁって時にこれ。

Posted at

例えば10個のいい感じのランダムの色が欲しいって時。
ガチのランダムにすると意味わからない変な色が出来上がったりしてしまうので。
っていうものをメモ程度に残しておきます。
意外と使える!

hueの部分の分母を欲しい個数にするだけです。
saturationとbrightnessに関しては自分の好みの色味にしてください。

下記コードそのまま使用した際にはこんな感じの色が出力されます。
似てる色があるとか言わないで
#CC8B29
#ABCC29
#49CC29
#29CC6A
#29CCCC
#296ACC
#4929CC
#AB29CC
#CC298B
#CC2929

swift.swift
let colorList = 
   [
    UIColor(hue: 0/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 1/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 2/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 3/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 4/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 5/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 6/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 7/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 8/10, saturation: 0.8, brightness: 0.8, alpha: 1),
    UIColor(hue: 9/10, saturation: 0.8, brightness: 0.8, alpha: 1)
   ]
java.java
    /*
    * いい感じの色のリストを指定数分作成する
    * colorCount: 色の数
    * brightness: 色味 0.0~1.0
    * */
    private int[] createRandomColorList(int colorCount, float brightness) {
        float [] f = new float[3];
        f[1] = brightness;
        f[2] = brightness;
        int[] cList = new int[colorCount];
        for (int i = 0; i < colorCount; i++) {
            f[0] = 360f * (i+1)/colorCount;
            cList[i] = Color.HSVToColor(f);
        }
        return cList;
    }

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2