Skip to content

Instantly share code, notes, and snippets.

@rossy
Created April 27, 2012 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossy/2505625 to your computer and use it in GitHub Desktop.
Save rossy/2505625 to your computer and use it in GitHub Desktop.
colour.js
function srgb2lrgb(r, g, b)
{
if (r instanceof Array)
{
b = r[2];
g = r[1];
r = r[0];
}
return [
r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92,
g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92,
b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92,
];
}
function lrgb2srgb(r, g, b)
{
if (r instanceof Array)
{
b = r[2];
g = r[1];
r = r[0];
}
return [
r > 0.0031308 ? Math.pow(r, 1 / 2.4) * 1.055 - 0.055 : r * 12.92,
g > 0.0031308 ? Math.pow(g, 1 / 2.4) * 1.055 - 0.055 : r * 12.92,
b > 0.0031308 ? Math.pow(b, 1 / 2.4) * 1.055 - 0.055 : r * 12.92,
]
}
function gamma2lrgb(gamma, r, g, b)
{
if (r instanceof Array)
{
b = r[2];
g = r[1];
r = r[0];
}
return [
Math.pow(r, gamma),
Math.pow(g, gamma),
Math.pow(b, gamma),
];
}
function lrgb2gamma(gamma, r, g, b)
{
if (r instanceof Array)
{
b = r[2];
g = r[1];
r = r[0];
}
return [
Math.pow(r, 1 / gamma),
Math.pow(g, 1 / gamma),
Math.pow(b, 1 / gamma),
];
}
function srgb2gamma(gamma, r, g, b)
{
return lrgb2gamma(gamma, srgb2lrgb(r, g, b));
}
function gamma2srgb(gamma, r, g, b)
{
return lrgb2srgb(gamma2lrgb(gamma, r, g, b));
}
function rgb2ycrcb(kb, kr, r, g, b)
{
var y;
if (r instanceof Array)
{
b = r[2];
g = r[1];
r = r[0];
}
return [
(y = kr * r + (1 - kr - kb) * g + kb * b),
(b - y) / (1 - kb) * 0.5,
(r - y) / (1 - kr) * 0.5,
];
}
function rgb2bt601(r, g, b)
{
return rgb2ycrcb(0.114, 0.299, r, g, b);
}
function rgb2bt709(r, g, b)
{
return rgb2ycrcb(0.0722, 0.2126, r, g, b);
}
function lrgb2bt709(r, g, b)
{
return rgb2bt709(lrgb2gamma(2.35, r, g, b));
}
function srgb2bt709(r, g, b)
{
return rgb2bt709(srgb2gamma(2.35, r, g, b));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment