Skip to content

Instantly share code, notes, and snippets.

@JensAyton
Created July 29, 2010 21:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JensAyton/499357 to your computer and use it in GitHub Desktop.
Save JensAyton/499357 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 left as an exercise for the reader.
vec4 rgba = vec4(rgb, step(r, 1.0));
gl_FragColor = rgba;
}
@randallrvr
Copy link

Do you have a corresponding rgb2hsv implementation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment