Skip to content

Instantly share code, notes, and snippets.

@yiwenl
Last active Aug 16, 2021
Embed
What would you like to do?
GLSL Color Coversions
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);
}
vec3 rgb2hsl(vec3 color) {
vec3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove first part)
float fmin = min(min(color.r, color.g), color.b); //Min. value of RGB
float fmax = max(max(color.r, color.g), color.b); //Max. value of RGB
float delta = fmax - fmin; //Delta RGB value
hsl.z = (fmax + fmin) / 2.0; // Luminance
if (delta == 0.0) //This is a gray, no chroma...
{
hsl.x = 0.0; // Hue
hsl.y = 0.0; // Saturation
} else //Chromatic data...
{
if (hsl.z < 0.5)
hsl.y = delta / (fmax + fmin); // Saturation
else
hsl.y = delta / (2.0 - fmax - fmin); // Saturation
float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;
float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;
float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;
if (color.r == fmax)
hsl.x = deltaB - deltaG; // Hue
else if (color.g == fmax)
hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hue
else if (color.b == fmax)
hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hue
if (hsl.x < 0.0)
hsl.x += 1.0; // Hue
else if (hsl.x > 1.0)
hsl.x -= 1.0; // Hue
}
return hsl;
}
vec3 rgb2hsv(vec3 rgb) {
float Cmax = max(rgb.r, max(rgb.g, rgb.b));
float Cmin = min(rgb.r, min(rgb.g, rgb.b));
float delta = Cmax - Cmin;
vec3 hsv = vec3(0., 0., Cmax);
if (Cmax > Cmin) {
hsv.y = delta / Cmax;
if (rgb.r == Cmax)
hsv.x = (rgb.g - rgb.b) / delta;
else {
if (rgb.g == Cmax)
hsv.x = 2. + (rgb.b - rgb.r) / delta;
else
hsv.x = 4. + (rgb.r - rgb.g) / delta;
}
hsv.x = fract(hsv.x / 6.);
}
return hsv;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment