LoginSignup
3
3

More than 3 years have passed since last update.

HSVからRGBへの変換をGLSLで実装する

Posted at

前提

HSVからRGBへの変換 - Wikipediaに基づいて、HSV色空間からRGB色空間への変換を実装する。変換の対象となるH, S, Vの定義は以下の通りとし、変換後のR, G, Bは0.0 - 1.0の範囲とする。

  • H (Hue) : 色相
    • 0.0 - 360.0のfloat
  • S (Saturation) : 彩度
    • 0.0 - 1.0のfloat
  • V (Value) : 明度
    • 0.0 - 1.0のfloat

実装

円柱モデルと円錐モデルでわずかに異なる。
下記のコードでは円錐モデルを採用しているためfloat c = sとなっているが、円柱モデルにする場合はこれをfloat c = v * sとすれば良い。

vec3 hsvToRgb(float h, float s, float v) {
    // h: 0.0 - 360.0, s: 0.0 - 1.0, v: 0.0 - 1.0
    float c = s; // float c = v * s;
    float h2 = h / 60.0;
    float x = c * (1.0 - abs(mod(h2, 2.0) - 1.0));
    vec3 rgb = (v - c) * vec3(1.0, 1.0, 1.0);

    if (0.0 <= h2 && h2 < 1.0) {
        rgb += vec3(c, x, 0.0);
    } else if (1.0 <= h2 && h2 < 2.0) {
        rgb += vec3(x, c, 0.0);
    } else if (2.0 <= h2 && h2 < 3.0) {
        rgb += vec3(0.0, c, x);
    } else if (3.0 <= h2 && h2 < 4.0) {
        rgb += vec3(0.0, x, c);
    } else if (4.0 <= h2 && h2 < 5.0) {
        rgb += vec3(x, 0.0, c);
    } else if (5.0 <= h2 && h2 < 6.0) {
        rgb += vec3(c, 0.0, x);
    }

    return rgb;
}

以上

3
3
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
3
3