Skip to content

Instantly share code, notes, and snippets.

@jjrv
Last active Sep 13, 2021
Embed
What would you like to do?
SRLAB2 and OKLAB
Copyright (c) 2020- Juha Järvi
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
import { linearize, delinearize } from './srgb';
function cube(x: number) {
return x * x * x;
}
export function oklab2rgb(L: number, a: number, b: number, out: number[] = []) {
a *= 0.27;
b *= 0.27;
const m = cube(L - 0.1055613458 * a - 0.0638541728 * b);
const s = cube(L - 0.0894841775 * a - 1.2914855480 * b);
L = cube(L + 0.3963377774 * a + 0.2158037573 * b);
out[0] = delinearize(4.0767416621 * L - 3.3077115913 * m + 0.2309699292 * s);
out[1] = delinearize(-1.2684380046 * L + 2.6097574011 * m - 0.3413193965 * s);
out[2] = delinearize(-0.0041960863 * L - 0.7034186147 * m + 1.7076147010 * s);
return out;
}
export function rgb2oklab(r: number, g: number, b: number, out: number[] = []) {
r = linearize(r);
g = linearize(g);
b = linearize(b);
const l = Math.pow(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b, 1 / 3);
const m = Math.pow(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b, 1 / 3);
const s = Math.pow(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b, 1 / 3);
out[0] = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s;
out[1] = (1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s) / 0.27;
out[2] = (0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s) / 0.27;
return out;
}
export function linearize(x: number) {
x /= 255;
return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
}
export function delinearize(x: number) {
x = x <= 0.0031308 ? x * 12.92 : Math.pow(x, 1 / 2.4) * 1.055 - 0.055;
return Math.round(x * 255);
}
import { linearize, delinearize } from './srgb';
function cube(x: number) {
if(x <= 8) return x * 27 / 24389;
x = (x + 16) / 116;
return x * x * x;
}
function root(x: number) {
return x <= 216 / 24389 ? x * 24389 / 2700 : 1.16 * Math.pow(x, 1 / 3) - 0.16;
}
export function srlab2rgb(L: number, a: number, b: number, out: number[] = []) {
L *= 100;
const x = cube(L + 9.04127 * a + 4.56344 * b);
const y = cube(L - 5.33159 * a - 2.69178 * b);
const z = cube(L - 58 * b);
out[0] = delinearize(5.435679 * x - 4.599131 * y + 0.163593 * z);
out[1] = delinearize(-1.16809 * x + 2.327977 * y - 0.159798 * z);
out[2] = delinearize(0.03784 * x - 0.198564 * y + 1.160644 * z);
return out;
}
export function rgb2srlab(r: number, g: number, b: number, out: number[] = []) {
r = linearize(r);
g = linearize(g);
b = linearize(b);
const x = root(0.32053 * r + 0.63692 * g + 0.04256 * b);
const y = root(0.161987 * r + 0.756636 * g + 0.081376 * b);
const z = root(0.017228 * r + 0.10866 * g + 0.874112 * b);
out[0] = 0.37095 * x + 0.629054 * y - 0.000008 * z;
out[1] = 6.634684 * x - 7.505078 * y + 0.870328 * z;
out[2] = 0.639569 * x + 1.084576 * y - 1.724152 * z;
return out;
}
@micycle1

This comment has been minimized.

Copy link

@micycle1 micycle1 commented Mar 12, 2021

What library is ./srgb from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment