Skip to content

Instantly share code, notes, and snippets.

@lucamtudor
Forked from alexfu/ColorUtils.java
Created June 18, 2014 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucamtudor/e2d808915a71ed765f2d to your computer and use it in GitHub Desktop.
Save lucamtudor/e2d808915a71ed765f2d to your computer and use it in GitHub Desktop.
public class ColorUtils {
private static final double LM_RED_COEFFICIENT = 0.2126;
private static final double LM_GREEN_COEFFICIENT = 0.7152;
private static final double LM_BLUE_COEFFICIENT = 0.0722;
public static int calculateRelativeLuminance(int color) {
int red = (int) (Color.red(color) * LM_RED_COEFFICIENT);
int green = (int) (Color.green(color) * LM_GREEN_COEFFICIENT);
int blue = (int) (Color.blue(color) * LM_BLUE_COEFFICIENT);
return red + green + blue;
}
}
Resources res = getResources();
int light = res.getColorStateList(R.color.primary_text_holo_light);
int dark = res.getColorStateList(R.color.primary_text_holo_dark);
double luminance = ColorUtils.calculateRelativeLuminance(myColor)/255.0;
if (luminance > 0.60) {
ribbon.setTextColor(light);
} else {
ribbon.setTextColor(dark);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment