Reputation: 33
How to check brightness in android?
I have an integer value of color.I wanna check this color is dark color or light color base on integer value of color.
if (checkColor == Color.RED || checkColor == Color.BLACK) {
//set fore color is white
} else {
//set fore color is black
}
instead of above code, i wanna change
if (!isBrightColor(checkColor)) {
//set fore color is white
} else {
//set fore color is black
}
private boolean isBrightColor(int checkColor){
boolean rtnValue;
//How to check this color is bright or dark
return rtnValue;
}
Upvotes: 3
Views: 6417
Reputation: 1
I know this is too late but this could help someone in future as it is much more easier and trusted answer ::-
You can just use the already present library in java:-
ColorUtils.calculateLuminance()
Example:-
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
if(ColorUtils.calculateLuminance(color) < 0.5) {
//darker or brighter color
// hence use white foreground
}
else{
//lighter color
//hence use black as foreground
}
Upvotes: 0
Reputation: 4374
I know this is an old question, but there is now a Color.luminance(int)
that already does what the selected answer suggests (API 24+)
/**
* Returns the relative luminance of a color.
* <p>
* Assumes sRGB encoding. Based on the formula for relative luminance
* defined in WCAG 2.0, W3C Recommendation 11 December 2008.
*
* @return a value between 0 (darkest black) and 1 (lightest white)
*/
public static float luminance(@ColorInt int color) {
ColorSpace.Rgb cs = (ColorSpace.Rgb) ColorSpace.get(ColorSpace.Named.SRGB);
DoubleUnaryOperator eotf = cs.getEotf();
double r = eotf.applyAsDouble(red(color) / 255.0);
double g = eotf.applyAsDouble(green(color) / 255.0);
double b = eotf.applyAsDouble(blue(color) / 255.0);
return (float) ((0.2126 * r) + (0.7152 * g) + (0.0722 * b));
}
There is also a difference between luminance and brightness:
As per wikipedia:
A more perceptually relevant alternative is to use luma, Y′, as a lightness dimension (fig. 12d). Luma is the weighted average of gamma-corrected R, G, and B, based on their contribution to perceived lightness, long used as the monochromatic dimension in color television broadcast. For sRGB, the Rec. 709 primaries yield Y′709, digital NTSC uses Y′601 according to Rec. 601 and some other primaries are also in use which result in different coefficients.[26][J]
Y 601 ′ = 0.2989 * R + 0.5870 * G + 0.1140 * B (SDTV)
Y 240 ′ = 0.212 * R + 0.701 * G + 0.087 * B (Adobe)
Y 709 ′ = 0.2126 * R + 0.7152 * G + 0.0722 * B (HDTV)
Y 2020 ′ = 0.2627 * R + 0.6780 * G + 0.0593 * B (UHDTV, HDR)
So, you would first need to understand if what you want is actually brightness (a average of the sum of R, G and B on RGB), or luminance which accounts for the human eye perception.
For completeness, if you want the actual brightness, that would be the V value on an HSV representation of color, so you could use:
val hsv = FloatArray(3)
Color.colorToHSV(color, hsv)
val brightness = hsv[2]
Upvotes: 4
Reputation: 192
You Should try this....
public static boolean isBrightColor(int color) {
if (android.R.color.transparent == color)
return true;
boolean rtnValue = false;
int[] rgb = { Color.red(color), Color.green(color), Color.blue(color) };
int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1]
* rgb[1] * .691 + rgb[2] * rgb[2] * .068);
// color is light
if (brightness >= 200) {
rtnValue = true;
}
return rtnValue;
}
reference: Android/Java: Determining if text color will blend in with the background?
Upvotes: 7