Skip to content

Instantly share code, notes, and snippets.

@marcelklehr
Created December 30, 2011 16:14
Show Gist options
  • Save marcelklehr/1540463 to your computer and use it in GitHub Desktop.
Save marcelklehr/1540463 to your computer and use it in GitHub Desktop.
Calculate contrast from two RGB colors.
function contrast(c1, c2) {
v1 = Math.min(Math.min(c1.r, c1.g),c1.b) + Math.max(Math.max(c1.r, c1.g),c1.b) / 2;
v2 = Math.min(Math.min(c2.r, c2.g),c2.b) + Math.max(Math.max(c2.r, c2.g),c2.b) / 2;
return Math.abs(v1-v2);
};
contrast({r:0, g:0, b:0},// black
{r:128, g:83, b:83}// light-brown-red
);
// 147
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment