as per title, my question is: Does Android provide any way to analyze/determine if a color (that will be obviously dynamic for the purpose) is light or dark?
Value is the measurement of the amount of black or white mixed into a pure hue. By adding black to the color, the value is made darker, resulting in what is referred to as a “shade.” When white is added to a color, the result is a lighter value, which is referred to as a “tint.”
The hexadecimal color code #a4c639 is a shade of yellow-green. In the RGB color model #a4c639 is comprised of 64.31% red, 77.65% green and 22.35% blue.
Android doesn't provide it, you can implement a method to determine this. Here a method to do that:
public boolean isColorDark(int color){ double darkness = 1-(0.299*Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color))/255; if(darkness<0.5){ return false; // It's a light color }else{ return true; // It's a dark color } }
If you use support library v4 (or AndroidX), you can use ColorUtils.calculateLuminance(color)
, which returns luminance of color as float between 0.0
and 1.0
.
So you can do something like:
boolean isDark(int color) { return ColorUtils.calculateLuminance(color) < 0.5; }
See:
Note since Android API 24 there is also method: Color.luminance(color)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With