Last active
June 9, 2017 13:57
-
-
Save SpineyPete/4cb8ef34df9a9faf1686055aae7f9ba8 to your computer and use it in GitHub Desktop.
Colorspace Conversions
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
#version 330 | |
// Colorspace conversions, I haven't checked the validity of this stuff. | |
// Some of them cause discolorations when transformed to and back so I'm | |
// probably doing wrong things that I don't understand -- Caveat emptor. | |
// CIELab XYZ | |
// ---------- | |
// Matrices borrowed from Fundamentals Of Computer Graphics (3rd edition). | |
vec3 RGBtoLAB(vec3 rgb) { | |
mat3 toLAB = { | |
{0.4124, 0.3576, 0.1805}, | |
{0.2126, 0.7152, 0.0722}, | |
{0.0193, 0.1192, 0.9505} | |
}; | |
return toLAB * rgb; | |
} | |
vec3 LABtoRGB(vec3 xyz) { | |
mat3 toRGB = { | |
{ 3.2405, -1.5371, -0.4985}, | |
{-0.9693, 1.8706, 0.0416}, | |
{ 0.0556, -0.2040, 1.0572} | |
}; | |
return toRGB * xyz; | |
} | |
// YCoCg | |
// ----- | |
// Borrowed from the Wikipedia article. | |
vec3 RGBtoYCoCg(vec3 rgb) { | |
float R = rgb.r; | |
float G = rgb.g; | |
float B = rgb.b; | |
float Co = R - B; | |
float tmp = B + Co / 2; | |
float Cg = G - tmp; | |
float Y = tmp + Cg / 2; | |
return vec3(Y, Co, Cg); | |
} | |
vec3 YCoCgToRGB(vec3 YCoCg) { | |
float Y = YCoCg.r; | |
float Co = YCoCg.g; | |
float Cg = YCoCg.b; | |
float tmp = Y - Cg/2; | |
float G = Cg + tmp; | |
float B = tmp - Co/2; | |
float R = B + Co; | |
return vec3(R, G, B); | |
} | |
// Grayscale | |
// --------- | |
float Grayscale(vec3 x) { | |
return (x.r * 0.299) + (x.g * 0.587) + (x.b * 0.114); | |
} | |
float MaxRGB(vec3 x) { | |
return max(max(x.r, x.g), x.b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment