Skip to content

Instantly share code, notes, and snippets.

@rocboronat
Last active June 30, 2018 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rocboronat/721a762740ad14e54082 to your computer and use it in GitHub Desktop.
Save rocboronat/721a762740ad14e54082 to your computer and use it in GitHub Desktop.
Know if a text on a dynamic background colour must be black or white to be easy to read
int color = palette.getVibrantColor(defaultColor);
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = (color >> 0) & 0xFF;
row.setBackgroundColor(color);
//@see http://stackoverflow.com/questions/12043187/how-to-check-if-hex-color-is-too-black
double luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
if (luma < 200) { //a magic number chosen with alexbailon.com, the lovely graphic designer
text.setTextColor(Color.rgb(255, 255, 255)); //a bright colour
} else {
text.setTextColor(Color.rgb(0, 0, 0)); //a dark colour
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment