Bug 1316869 - Choose black or white title text color for custom tabs toolbar based on background color. r=sebastian

This commit is contained in:
Dylan Roeh 2016-11-11 09:26:31 -06:00
Родитель 32812d8ba8
Коммит 20b0a84702
2 изменённых файлов: 22 добавлений и 0 удалений

Просмотреть файл

@ -164,7 +164,10 @@ public class CustomTabsActivity extends GeckoApp implements Tabs.OnTabsChangedLi
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);

Просмотреть файл

@ -19,7 +19,26 @@ public class ColorUtil {
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);
}
}