Bug 1316869 - Choose black or white title text color for custom tabs toolbar based on background color. r=sebastian
authorDylan Roeh <droeh@mozilla.com>
Fri, 11 Nov 2016 09:26:31 -0600
changeset 348993 0114eda62ce256dfaf4eddb761cea277bdefe572
parent 348992 2047f4b419bf3b059e6d26b2ac2f5357b551f0ad
child 348994 efcde9f288abca491607c6e333c7c36da20f0b86
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1316869
milestone52.0a1
Bug 1316869 - Choose black or white title text color for custom tabs toolbar based on background color. r=sebastian
mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
mobile/android/base/java/org/mozilla/gecko/util/ColorUtil.java
--- a/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
@@ -159,16 +159,19 @@ public class CustomTabsActivity extends 
         if (toolbarColor == NO_COLOR) {
             final int color = getIntent().getIntExtra(EXTRA_TOOLBAR_COLOR, NO_COLOR);
             if (color == NO_COLOR) {
                 return;
             }
             toolbarColor = color;
         }
 
+        final int titleTextColor = ColorUtil.getReadableTextColor(toolbarColor);
+
         toolbar.setBackgroundColor(toolbarColor);
+        toolbar.setTitleTextColor(titleTextColor);
         final Window window = getWindow();
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
             window.setStatusBarColor(ColorUtil.darken(toolbarColor, 0.25));
         }
     }
 }
--- a/mobile/android/base/java/org/mozilla/gecko/util/ColorUtil.java
+++ b/mobile/android/base/java/org/mozilla/gecko/util/ColorUtil.java
@@ -14,12 +14,31 @@ public class ColorUtil {
         int blue = Color.blue(color);
         red = darkenColor(red, fraction);
         green = darkenColor(green, fraction);
         blue = darkenColor(blue, fraction);
         final int alpha = Color.alpha(color);
         return Color.argb(alpha, red, green, blue);
     }
 
+    public static int getReadableTextColor(final int backgroundColor) {
+        final int greyValue = grayscaleFromRGB(backgroundColor);
+        // 186 chosen rather than the seemingly obvious 128 because of gamma.
+        if (greyValue < 186) {
+            return Color.WHITE;
+        } else {
+            return Color.BLACK;
+        }
+    }
+
     private static int darkenColor(final int color, final double fraction) {
         return (int) Math.max(color - (color * fraction), 0);
     }
+
+    private static int grayscaleFromRGB(final int color) {
+        final int red = Color.red(color);
+        final int green = Color.green(color);
+        final int blue = Color.blue(color);
+        // Magic weighting taken from a stackoverflow post, supposedly related to how
+        // humans perceive color.
+        return (int) (0.299*red + 0.587*green + 0.114*blue);
+    }
 }