Created
July 29, 2010 21:54
-
-
Save JensAyton/499357 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
© 2010 Jens Ayton | |
This code may be used freely. | |
*/ | |
uniform float uValue; // Value/brighness component. | |
const float kPi = 3.141592653589793; | |
vec3 hsv2rgb(float h, float s, float v) | |
{ | |
float hi = h * 3.0 / kPi; // Sector, [-3..3) | |
float f = hi - floor(hi); // Fractional part. | |
vec4 components = vec4 | |
( | |
0.0, | |
s, | |
s * f, | |
s * (1.0 - f) | |
); | |
components = (1.0 - components) * v; | |
#if 0 | |
if (hi < -2.0) | |
{ | |
return components.xwy; | |
} | |
else if (hi < -1.0) | |
{ | |
return components.zxy; | |
} | |
else if (hi < 0.0) | |
{ | |
return components.yxw; | |
} | |
else if (hi < 1.0) | |
{ | |
return components.yzx; | |
} | |
else if (hi < 2.0) | |
{ | |
return components.wyx; | |
} | |
else | |
{ | |
return components.xyz; | |
} | |
#else | |
return (hi < 0.0) ? | |
( | |
(hi < -2.0) ? components.xwy : | |
(hi < -1.0) ? components.zxy : | |
components.yxw | |
) : | |
( | |
(hi < 1.0) ? components.yzx : | |
(hi < 2.0) ? components.wyx : | |
components.xyz | |
); | |
#endif | |
} | |
void main() | |
{ | |
vec2 coords = gl_TexCoord[0].st * 2.0 - 1.0; | |
float theta = atan(coords.y, -coords.x); | |
float r = length(coords); | |
vec3 rgb = hsv2rgb(theta, r, uValue); | |
// Clamp to a circle. Anti-aliasing left as an exercise for the reader. | |
vec4 rgba = vec4(rgb, step(r, 1.0)); | |
gl_FragColor = rgba; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have a corresponding rgb2hsv implementation?