Last active
June 30, 2018 19:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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