C++で彩度が均一でランダムな色相の虹のようなものを作り出そうとしたのですが、意外と工夫が必要でした。
やり方を共有しておきます。
Photoshopなどで確認してみると、彩度がmaxの色は、
1.rgbのうち一つの要素が255で
2.他の二つのうち一つの要素が0で
3.残りの一つの要素が0 ~ 255の間の値
という条件を満たしているようです。
上記の要件を満たすようなrgbの値をランダムに生成します。
// 虹色ランダムの合成
int color_elem[3];
color_elem[0] = 255;
color_elem[1] = rand() % 256;
color_elem[2] = 0;
int color_r_index = rand() % 3;
int r_bin = rand() % 2;
int color_g_index = (color_r_index + 1 + r_bin) % 3;
int color_b_index = (color_r_index + 1 + !r_bin) % 3;
// openFrameworksの色構造体に色を格納
// color.set(red, green, blue);で色を作成できる
ofColor color;
color.set(color_elem[color_r_index], color_elem[color_g_index], color_elem[color_b_index]);
彩度を調節するためには、
color_elem[1] = rand() % 256;
のところを
color_elem[1] = rand() % (256 - BIUS) + BIUS;
// 0 < BIUS <= 256
のようにすると良さそうです。
ヨッシャ!イイカンジ!