Skip to content

Instantly share code, notes, and snippets.

@1bardesign
Last active April 30, 2021 05:28
Show Gist options
  • Save 1bardesign/c46e80d183cfb08793f9388b0b01155f to your computer and use it in GitHub Desktop.
Save 1bardesign/c46e80d183cfb08793f9388b0b01155f to your computer and use it in GitHub Desktop.
oklab to rgb and back for love2d glsl - see https://bottosson.github.io/posts/oklab/ - todo: polar coordinate mode
vec3 oklab_to_rgb(vec3 lab) {
float L = lab.x;
float a = lab.y;
float b = lab.z;
//
float l_ = L + 0.3963377774 * a + 0.2158037573 * b;
float m_ = L - 0.1055613458 * a - 0.0638541728 * b;
float s_ = L - 0.0894841775 * a - 1.2914855480 * b;
float l = pow(l_, 3.0);
float m = pow(m_, 3.0);
float s = pow(s_, 3.0);
return gammaCorrectColor(vec3(
(+4.0767245293 * l - 3.3072168827 * m + 0.2307590544 * s),
(-1.2681437731 * l + 2.6093323231 * m - 0.3411344290 * s),
(-0.0041119885 * l - 0.7034763098 * m + 1.7068625689 * s)
));
}
vec3 rgb_to_oklab(vec3 rgb) {
rgb = unGammaCorrectColor(rgb);
float r = rgb.r;
float g = rgb.g;
float b = rgb.b;
float l = 0.4121656120 * r + 0.5362752080 * g + 0.0514575653 * b;
float m = 0.2118591070 * r + 0.6807189584 * g + 0.1074065790 * b;
float s = 0.0883097947 * r + 0.2818474174 * g + 0.6302613616 * b;
float l_ = pow(l, 1.0 / 3.0);
float m_ = pow(m, 1.0 / 3.0);
float s_ = pow(s, 1.0 / 3.0);
return vec3(
0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_,
1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_,
0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment