Skip to content

Instantly share code, notes, and snippets.

@christabor
Created June 5, 2015 16:27
Show Gist options
  • Save christabor/fcc5f11e2921fabdef25 to your computer and use it in GitHub Desktop.
Save christabor/fcc5f11e2921fabdef25 to your computer and use it in GitHub Desktop.
w3c color recommendation algo
function checkVisibility(color1, color2) {
// http://www.w3.org/TR/AERT#color-contrast
// Color brightness is determined by the following formula:
// ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000
// Note: This algorithm is taken from a formula for converting RGB values to
// YIQ values. This brightness value gives a perceived brightness for a color.
// Color difference is determined by the following formula:
// (maximum (Red value 1, Red value 2) - minimum (Red value 1, Red value 2)) + (maximum (Green value 1, Green value 2) - minimum (Green value 1, Green value 2)) + (maximum (Blue value 1, Blue value 2) - minimum (Blue value 1, Blue value 2))
// The range for color brightness difference is 125. The range for color difference is 500.
var red_max = Math.max(color1.r, color2.r);
var red_min = Math.min(color1.r, color2.r);
var green_max = Math.max(color1.g, color2.g);
var green_min = Math.min(color1.g, color2.g);
var blue_max = Math.max(color1.b, color2.b);
var blue_min = Math.min(color1.b, color2.b);
var color_diff = (red_max - red_min) + (green_max - green_min) + (blue_max - blue_min);
var brightness_diff = ((color1.r * 299) + (color1.g * 587) + (color1.b * 114)) / 1000;
return [color_diff, brightness_diff];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment