From 47ab9b1fc5a8625bc714a1def91374d762927877 Mon Sep 17 00:00:00 2001 From: Tony Wickham Date: Tue, 6 Jun 2017 17:38:57 -0700 Subject: [PATCH] Generalize findContrastColor() to work for dark backgrounds Previously it assumed the background was lighter than the foreground. Bug: 62380473 Change-Id: Icd53750a2f9181890c8b9c62721d07946e115e99 --- .../launcher3/graphics/IconPalette.java | 31 +++++++++---------- .../notification/NotificationItemView.java | 2 +- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/com/android/launcher3/graphics/IconPalette.java b/src/com/android/launcher3/graphics/IconPalette.java index 60ca7b236b..750f4deec5 100644 --- a/src/com/android/launcher3/graphics/IconPalette.java +++ b/src/com/android/launcher3/graphics/IconPalette.java @@ -134,43 +134,40 @@ public class IconPalette { * This was copied from com.android.internal.util.NotificationColorUtil. */ private static int ensureTextContrast(int color, int bg) { - return findContrastColor(color, bg, true, 4.5); + return findContrastColor(color, bg, 4.5); } /** * Finds a suitable color such that there's enough contrast. * - * @param color the color to start searching from. - * @param other the color to ensure contrast against. Assumed to be lighter than {@param color} - * @param findFg if true, we assume {@param color} is a foreground, otherwise a background. + * @param fg the color to start searching from. + * @param bg the color to ensure contrast against. * @param minRatio the minimum contrast ratio required. * @return a color with the same hue as {@param color}, potentially darkened to meet the * contrast ratio. * * This was copied from com.android.internal.util.NotificationColorUtil. */ - private static int findContrastColor(int color, int other, boolean findFg, double minRatio) { - int fg = findFg ? color : other; - int bg = findFg ? other : color; + private static int findContrastColor(int fg, int bg, double minRatio) { if (ColorUtils.calculateContrast(fg, bg) >= minRatio) { - return color; + return fg; } double[] lab = new double[3]; - ColorUtils.colorToLAB(findFg ? fg : bg, lab); + ColorUtils.colorToLAB(bg, lab); + double bgL = lab[0]; + ColorUtils.colorToLAB(fg, lab); + double fgL = lab[0]; + boolean isBgDark = bgL < 50; - double low = 0, high = lab[0]; + double low = isBgDark ? fgL : 0, high = isBgDark ? 100 : fgL; final double a = lab[1], b = lab[2]; for (int i = 0; i < 15 && high - low > 0.00001; i++) { final double l = (low + high) / 2; - if (findFg) { - fg = ColorUtils.LABToColor(l, a, b); - } else { - bg = ColorUtils.LABToColor(l, a, b); - } + fg = ColorUtils.LABToColor(l, a, b); if (ColorUtils.calculateContrast(fg, bg) > minRatio) { - low = l; + if (isBgDark) high = l; else low = l; } else { - high = l; + if (isBgDark) low = l; else high = l; } } return ColorUtils.LABToColor(low, a, b); diff --git a/src/com/android/launcher3/notification/NotificationItemView.java b/src/com/android/launcher3/notification/NotificationItemView.java index 20707aa67e..cc81b11216 100644 --- a/src/com/android/launcher3/notification/NotificationItemView.java +++ b/src/com/android/launcher3/notification/NotificationItemView.java @@ -99,7 +99,7 @@ public class NotificationItemView extends PopupItemView implements LogContainerP if (mNotificationHeaderTextColor == Notification.COLOR_DEFAULT) { mNotificationHeaderTextColor = IconPalette.resolveContrastColor(getContext(), palette.dominantColor, - Themes.getAttrColor(getContext(), R.attr.popupColorSecondary)); + Themes.getAttrColor(getContext(), R.attr.popupColorPrimary)); } mHeaderCount.setTextColor(mNotificationHeaderTextColor); }