Skip to content

Instantly share code, notes, and snippets.

@hmasato
Last active December 18, 2015 07:28
Show Gist options
  • Select an option

  • Save hmasato/5746303 to your computer and use it in GitHub Desktop.

Select an option

Save hmasato/5746303 to your computer and use it in GitHub Desktop.
[GLSL] rgb <=> hsv
// RGB -> HSV
vec3 rgb2hsv(vec3 v){
const float e=1e-20;
vec4 c=vec4(v,0);
c=mix(vec4(c.rbg,-1),c,step(c.b,c.g));
c=mix(vec4(c.grb,-1.0/3.0-c.a),c,step(c.g,c.r));
float cr=c.r-min(c.g,c.b);
return vec3(abs(c.a+(c.g-c.b)/(6*cr+e)),cr/(c.r+e),c.r);
}
// HSV -> RGB
vec3 hsv2rgb(vec3 v){
return (clamp(abs(fract(v.x+vec3(0,2,1)/3)*6-3)-2,-1,0)*v.y+1)*v.z;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment