Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
© 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;
}
@raegnar

This comment has been minimized.

Copy link

@raegnar raegnar commented Feb 25, 2013

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