Skip to content

Instantly share code, notes, and snippets.

@obviousjim
Created July 15, 2013 23:27
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 obviousjim/6004420 to your computer and use it in GitHub Desktop.
Save obviousjim/6004420 to your computer and use it in GitHub Desktop.
GLSL RGB -> HSL
vec3 rgb2hsl( vec3 _input ){
float h = 0.0;
float s = 0.0;
float l = 0.0;
float r = _input.r;
float g = _input.g;
float b = _input.b;
float cMin = min( r, min( g, b ) );
float cMax = max( r, max( g, b ) );
l = ( cMax + cMin ) / 2.0;
if ( cMax > cMin ) {
float cDelta = cMax - cMin;
s = l < .05 ? cDelta / ( cMax + cMin ) : cDelta / ( 2.0 - ( cMax + cMin ) );
// hue
if ( r == cMax ) {
h = ( g - b ) / cDelta;
} else if ( g == cMax ) {
h = 2.0 + ( b - r ) / cDelta;
} else {
h = 4.0 + ( r - g ) / cDelta;
}
if ( h < 0.0) {
h += 6.0;
}
h = h / 6.0;
}
return vec3( h, s, l );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment