mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-15 17:10:30 +00:00
Add method to determine the perceived brightness of a color.
This commit is contained in:
parent
c6861282f3
commit
eb1092c9f8
1 changed files with 6 additions and 3 deletions
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue