Generalize findContrastColor() to work for dark backgrounds

Previously it assumed the background was lighter than the foreground.

Bug: 62380473
Change-Id: Icd53750a2f9181890c8b9c62721d07946e115e99
diff --git a/src/com/android/launcher3/graphics/IconPalette.java b/src/com/android/launcher3/graphics/IconPalette.java
index 60ca7b2..750f4de 100644
--- a/src/com/android/launcher3/graphics/IconPalette.java
+++ b/src/com/android/launcher3/graphics/IconPalette.java
@@ -134,43 +134,40 @@
      * 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 20707aa..cc81b11 100644
--- a/src/com/android/launcher3/notification/NotificationItemView.java
+++ b/src/com/android/launcher3/notification/NotificationItemView.java
@@ -99,7 +99,7 @@
             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);
         }