Add method to determine the perceived brightness of a color.

This commit is contained in:
Andreas Schildbach 2015-02-23 11:54:01 +01:00
parent c6861282f3
commit eb1092c9f8

View file

@ -128,13 +128,16 @@ public class Style implements Serializable
return color & 0xff; return color & 0xff;
} }
public static int deriveForegroundColor(final int backgroundColor) public static float perceivedBrightness(final int color)
{ {
// formula for perceived brightness computation: http://www.w3.org/TR/AERT#color-contrast // formula for perceived brightness computation: http://www.w3.org/TR/AERT#color-contrast
final double a = 1 - (0.299 * Style.red(backgroundColor) + 0.587 * Style.green(backgroundColor) + 0.114 * Style.blue(backgroundColor)) / 255; return (0.299f * Style.red(color) + 0.587f * Style.green(color) + 0.114f * Style.blue(color)) / 256;
}
public static int deriveForegroundColor(final int backgroundColor)
{
// dark colors, white font. Or light colors, black font // dark colors, white font. Or light colors, black font
if (a >= 0.5) if (perceivedBrightness(backgroundColor) < 0.5)
return WHITE; return WHITE;
else else
return BLACK; return BLACK;