diff options
author | Tony Wickham <twickham@google.com> | 2017-06-06 17:38:57 -0700 |
---|---|---|
committer | Tony Wickham <twickham@google.com> | 2017-06-06 17:38:57 -0700 |
commit | 47ab9b1fc5a8625bc714a1def91374d762927877 (patch) | |
tree | 1af4cb5d8a2c1c21f75bf89a534a84767914e89d /src/com/android/launcher3/graphics | |
parent | 15fcdbed076e271e047a0f3a2b2ddd2511ce9fd4 (diff) | |
download | android_packages_apps_Trebuchet-47ab9b1fc5a8625bc714a1def91374d762927877.tar.gz android_packages_apps_Trebuchet-47ab9b1fc5a8625bc714a1def91374d762927877.tar.bz2 android_packages_apps_Trebuchet-47ab9b1fc5a8625bc714a1def91374d762927877.zip |
Generalize findContrastColor() to work for dark backgrounds
Previously it assumed the background was lighter than the foreground.
Bug: 62380473
Change-Id: Icd53750a2f9181890c8b9c62721d07946e115e99
Diffstat (limited to 'src/com/android/launcher3/graphics')
-rw-r--r-- | src/com/android/launcher3/graphics/IconPalette.java | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/src/com/android/launcher3/graphics/IconPalette.java b/src/com/android/launcher3/graphics/IconPalette.java index 60ca7b236..750f4deec 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); |