Skip to content

Commit

Permalink
Bug 1316869 - Choose black or white title text color for custom tabs …
Browse files Browse the repository at this point in the history
…toolbar based on background color. r=sebastian
  • Loading branch information
Dylan Roeh committed Nov 11, 2016
1 parent 32812d8 commit 20b0a84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ private void updateToolbarColor(final Toolbar toolbar) {
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);
Expand Down
19 changes: 19 additions & 0 deletions mobile/android/base/java/org/mozilla/gecko/util/ColorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,26 @@ public static int darken(final int color, final double fraction) {
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);
}
}

0 comments on commit 20b0a84

Please sign in to comment.