HSV
https://gyazo.com/288442075997720bcb7072d5c6c4ea0e
RGB to HSV
$ V = \max(R, G, B)
$ M = \min(R, G, B)
$ C = V - M
$ S = \left\{ \begin{aligned} & 0 && {\rm if} \ V = 0 \\ & \frac{C}{V} && {\rm otherwise} \end{aligned} \right.
$ H = \left\{ \begin{aligned} & 0 && {\rm if} \ C = 0 \\ & 60 \ {\rm deg} \cdot \left( \frac{G - B}{C} \ {\rm mod} \ 6 \right) && {\rm if} \ V = R \\ & 60 \ {\rm deg} \cdot \left( \frac{B - R}{C} + 2 \right) && {\rm if} \ V = G \\ & 60 \ {\rm deg} \cdot \left( \frac{R - G}{C} + 4 \right) && {\rm if} \ V = B \end{aligned} \right.
GLSL
code:glsl
vec3 rgb2hsv(vec3 rgb) {
float v = max(max(rgb.r, rgb.g), rgb.b);
float m = min(min(rgb.r, rgb.g), rgb.b);
float c = v - m;
float s = v == 0.0 ? 0.0 : c / v;
float h = c == 0.0 ? 0.0 :
v == rgb.r ? mod((rgb.g - rgb.b) / c, 6.0) :
v == rgb.g ? (rgb.b - rgb.r) / c + 2.0 :
(rgb.r - rgb.g) / c + 4.0;
return vec3(h / 6.0, s, v);
}
HSV to RGB
$ x = \frac{H}{60 \ {\rm deg}}とおき、
$ R_H = {\rm saturate}(|x \ {\rm mod} \ 6 - 3|-1)
$ G_H = {\rm saturate}(|(x + 4) \ {\rm mod} \ 6 - 3|-1)
$ B_H = {\rm saturate}(|(x + 2) \ {\rm mod} \ 6 - 3|-1)
https://gyazo.com/62d05a355499fb12522acbcb482fd404
$ RGB = V * {\rm lerp}(\{1, 1, 1\}, \{R_H, G_H, B_H\}, S)
GLSL
code:glsl
vec3 hsv2rgb(vec3 hsv) {
vec3 h = saturate(abs(mod(6.0 * hsv.x + vec3(0, 4, 2), 6.0) - 3.0) - 1.0);
return hsv.z * mix(vec3(1.0), h, hsv.y);
}
肝心の虹色部分
どうせこれ使いたいだけでしょ
code:glsl
vec3 rainbow(float t) {
return saturate(abs(mod(6.0 * t + vec3(0, 4, 2), 6.0) - 3.0) - 1.0);
}