LoginSignup
5
4

More than 5 years have passed since last update.

【C++】ランダムな色相の虹を合成

Last updated at Posted at 2014-01-17

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

のようにすると良さそうです。

スクリーンショット 2014-01-17 10.13.57.png

ヨッシャ!イイカンジ!

5
4
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
5
4