From eb1092c9f8f7c3497178007a3572637d94e3d367 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Mon, 23 Feb 2015 11:54:01 +0100 Subject: [PATCH] Add method to determine the perceived brightness of a color. --- enabler/src/de/schildbach/pte/dto/Style.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/enabler/src/de/schildbach/pte/dto/Style.java b/enabler/src/de/schildbach/pte/dto/Style.java index a7107b1b..8c61c80c 100644 --- a/enabler/src/de/schildbach/pte/dto/Style.java +++ b/enabler/src/de/schildbach/pte/dto/Style.java @@ -128,13 +128,16 @@ public class Style implements Serializable 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 - 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 - if (a >= 0.5) + if (perceivedBrightness(backgroundColor) < 0.5) return WHITE; else return BLACK;