Skip to content

Instantly share code, notes, and snippets.

@bosmacs
Forked from JensAyton/ahruman_color_wheel.fs
Created November 22, 2010 03:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bosmacs/709475 to your computer and use it in GitHub Desktop.
Save bosmacs/709475 to your computer and use it in GitHub Desktop.
/*
© 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 technique via http://prideout.net/blog/?p=22
float E = 0.5 * fwidth(r);
gl_FragColor = vec4(rgb, smoothstep(1.0 + E, 1.0 - E, r));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment