Skip to content

Instantly share code, notes, and snippets.

@jc4p
Created June 23, 2015 21:34
Show Gist options
  • Save jc4p/d9da27e47c7933fe6b03 to your computer and use it in GitHub Desktop.
Save jc4p/d9da27e47c7933fe6b03 to your computer and use it in GitHub Desktop.
/**
* Calculates if the given textColor is readable when laid ontop of the given backgroundColor.
*
* http://www.seas.upenn.edu/~cse400/CSE400_2012_2013/reports/01_report.pdf
*/
public static boolean isTextReadable(int backgroundColor, int textColor) {
double brightnessBackground = (299*Color.red(backgroundColor) + 587*Color.green(backgroundColor) + 114*Color.blue(backgroundColor)) / 1000.0;
double brightnessText = (299*Color.red(textColor) + 587*Color.green(textColor) + 114*Color.blue(textColor)) / 1000.0;
double brightnessDelta = Math.abs(brightnessBackground - brightnessText);
double hueDelta = Math.abs(Color.red(backgroundColor) - Color.red(textColor)) +
Math.abs(Color.green(backgroundColor) - Color.green(textColor)) + Math.abs(Color.blue(backgroundColor) - Color.blue(textColor));
return brightnessDelta > 125 && hueDelta > 500;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment