Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created December 29, 2014 23:53
Show Gist options
  • Save jhurliman/0df6cf69493587d34d71 to your computer and use it in GitHub Desktop.
Save jhurliman/0df6cf69493587d34d71 to your computer and use it in GitHub Desktop.
rgb2lab.fsh
varying vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
vec3 rgb2xyz(vec3 c) {
vec3 tmp;
tmp.x = (c.r > 0.04045) ? pow( (c.r + 0.055) / 1.055, 2.4) : c.r / 12.92;
tmp.y = (c.g > 0.04045) ? pow( (c.g + 0.055) / 1.055, 2.4) : c.g / 12.92;
tmp.z = (c.b > 0.04045) ? pow( (c.b + 0.055) / 1.055, 2.4) : c.b / 12.92;
return 100.0 * tmp * mat3(0.4124, 0.3576, 0.1805,
0.2126, 0.7152, 0.0722,
0.0193, 0.1192, 0.9505);
}
vec3 xyz2lab(vec3 c) {
vec3 n = c / vec3(95.047, 100, 108.883);
vec3 v;
v.x = (n.x > 0.008856) ? pow(n.x, 1.0 / 3.0) : (7.787 * n.x) + (16.0 / 116.0);
v.y = (n.y > 0.008856) ? pow(n.y, 1.0 / 3.0) : (7.787 * n.y) + (16.0 / 116.0);
v.z = (n.z > 0.008856) ? pow(n.z, 1.0 / 3.0) : (7.787 * n.z) + (16.0 / 116.0);
return vec3((116.0 * v.y) - 16.0, 500.0 * (v.x - v.y), 200.0 * (v.y - v.z));
}
vec3 rgb2lab(vec3 c) {
vec3 lab = xyz2lab(rgb2xyz(c));
return vec3(lab.x / 100.0, 0.5 + 0.5 * (lab.y / 127.0), 0.5 + 0.5 * (lab.z / 127.0));
}
void main() {
vec4 c = texture2D(inputImageTexture, textureCoordinate);
gl_FragColor = vec4(rgb2lab(c.rgb), c.a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment