Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Created July 14, 2015 18:44
Show Gist options
  • Save kylemcdonald/f8df3bc2f8d38ca2b7cb to your computer and use it in GitHub Desktop.
Save kylemcdonald/f8df3bc2f8d38ca2b7cb to your computer and use it in GitHub Desktop.
Every unique HSV to RGB conversion function named "hsv2rgb" on ShaderToy.
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
const vec4 K = vec4(1., 2. / 3., 1. / 3., 3.);
vec3 hsv2rgb(vec3 c)
{
// from http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
vec3 p = abs(fract(c.xxx + K.xyz) * 6. - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0., 1.), c.y);
}
// iq's smooth hsv to rgb
vec3 hsv2rgb( in vec3 c )
{
vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
rgb = rgb*rgb*(3.0-2.0*rgb);
return c.z * mix( vec3(1.0), rgb, c.y);
}
// HSV to RGB
vec3 hsv2rgb (in vec3 hsv) {
#ifdef HSV2RGB_SAFE
hsv.yz = clamp (hsv.yz, 0.0, 1.0);
#endif
#ifdef HSV2RGB_FAST
return hsv.z * (1.0 + 0.5 * hsv.y * (cos (2.0 * PI * (hsv.x + vec3 (0.0, 2.0 / 3.0, 1.0 / 3.0))) - 1.0));
#else
return hsv.z * (1.0 + hsv.y * clamp (abs (fract (hsv.x + vec3 (0.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0) - 2.0, -1.0, 0.0));
#endif
}
vec3 hue2rgb(float hue) {
return clamp(
abs(mod(hue * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0,
0.0, 1.0);
}
vec3 hsv2rgb(vec3 c) {
vec3 rgb = hue2rgb(c.x);
return c.z * mix(vec3(1.0), rgb, c.y);
}
vec3 hsv2rgb( in vec3 c )
{
vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
return c.z * mix( vec3(1.0), rgb, c.y);
}
vec3 hsv2rgb (in vec3 hsv) {
hsv.yz = clamp (hsv.yz, 0.0, 1.0);
return hsv.z * (1.0 + 0.5 * hsv.y * (cos (2.0 * 3.14159 * (hsv.x + vec3 (0.0, 2.0 / 3.0, 1.0 / 3.0))) - 1.0));
}
vec4 hsv2rgb(float h,float s,float v) {
const float eps=1e-3;
vec4 result=vec4(0.0, 0.0, 0.0, 1.0);
if(s<=0.0)result.r=result.g=result.b=v;
else {
float hi=floor(h/60.0);
float f=(h/60.0)-hi;
float m=v*(1.0-s);
float n=v*(1.0-s*f);
float k=v*(1.0-s*(1.0-f));
if(hi<=0.0+eps) {
result.r=v;
result.g=k;
result.b=m;
} else if(hi<=1.0+eps) {
result.r=n;
result.g=v;
result.b=m;
} else if(hi<=2.0+eps) {
result.r=m;
result.g=v;
result.b=k;
} else if(hi<=3.0+eps) {
result.r=m;
result.g=n;
result.b=v;
} else if(hi<=4.0+eps) {
result.r=k;
result.g=m;
result.b=v;
} else if(hi<=5.0+eps) {
result.r=v;
result.g=m;
result.b=n;
}
}
return result;
}
vec3 hsv2rgbc(vec3 c) {
vec3 rgb = hue2rgb(c.x);
rgb = smoothstep(vec3(0.0),vec3(1.0),rgb);
return c.z * mix(vec3(1.0), rgb, c.y);
}
vec3 hsv2rgb( vec3 in_hsv )
{
float u = in_hsv.y * sin(in_hsv.x);
float v = in_hsv.y * cos(in_hsv.x);
vec3 out_rgb;
out_rgb.r = in_hsv.z + u;
out_rgb.g = in_hsv.z + v;
out_rgb.b = clamp( in_hsv.z - u - v, 0.0, 1.0 );
return out_rgb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment