Created
July 15, 2013 23:27
-
-
Save obviousjim/6004420 to your computer and use it in GitHub Desktop.
GLSL RGB -> HSL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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