OSDN Git Service

Add support for color extracted notification dots
[android-x86/packages-apps-Launcher3.git] / src / com / android / launcher3 / graphics / IconPalette.java
index cd7cf70..a17ceeb 100644 (file)
@@ -18,9 +18,12 @@ package com.android.launcher3.graphics;
 
 import android.app.Notification;
 import android.content.Context;
+import android.content.res.Resources;
 import android.graphics.Color;
 import android.graphics.ColorMatrix;
 import android.graphics.ColorMatrixColorFilter;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
 import android.support.v4.graphics.ColorUtils;
 import android.util.Log;
 
@@ -35,23 +38,32 @@ public class IconPalette {
     private static final boolean DEBUG = false;
     private static final String TAG = "IconPalette";
 
-    public static final IconPalette FOLDER_ICON_PALETTE = new IconPalette(Color.WHITE);
-
     private static final float MIN_PRELOAD_COLOR_SATURATION = 0.2f;
     private static final float MIN_PRELOAD_COLOR_LIGHTNESS = 0.6f;
 
+    private static IconPalette sBadgePalette;
+    private static IconPalette sFolderBadgePalette;
+
     public final int dominantColor;
     public final int backgroundColor;
     public final ColorMatrixColorFilter backgroundColorMatrixFilter;
+    public final ColorMatrixColorFilter saturatedBackgroundColorMatrixFilter;
     public final int textColor;
     public final int secondaryColor;
 
-    private IconPalette(int color) {
+    private IconPalette(int color, boolean desaturateBackground) {
         dominantColor = color;
-        backgroundColor = getMutedColor(dominantColor);
+        backgroundColor = desaturateBackground ? getMutedColor(dominantColor, 0.87f) : dominantColor;
         ColorMatrix backgroundColorMatrix = new ColorMatrix();
         Themes.setColorScaleOnMatrix(backgroundColor, backgroundColorMatrix);
         backgroundColorMatrixFilter = new ColorMatrixColorFilter(backgroundColorMatrix);
+        if (!desaturateBackground) {
+            saturatedBackgroundColorMatrixFilter = backgroundColorMatrixFilter;
+        } else {
+            // Get slightly more saturated background color.
+            Themes.setColorScaleOnMatrix(getMutedColor(dominantColor, 0.54f), backgroundColorMatrix);
+            saturatedBackgroundColorMatrixFilter = new ColorMatrixColorFilter(backgroundColorMatrix);
+        }
         textColor = getTextColorForBackground(backgroundColor);
         secondaryColor = getLowContrastColor(backgroundColor);
     }
@@ -74,8 +86,35 @@ public class IconPalette {
         return result;
     }
 
-    public static IconPalette fromDominantColor(int dominantColor) {
-        return new IconPalette(dominantColor);
+    public static IconPalette fromDominantColor(int dominantColor, boolean desaturateBackground) {
+        return new IconPalette(dominantColor, desaturateBackground);
+    }
+
+    /**
+     * Returns an IconPalette based on the badge_color in colors.xml.
+     * If that color is Color.TRANSPARENT, then returns null instead.
+     */
+    public static @Nullable IconPalette getBadgePalette(Resources resources) {
+        int badgeColor = resources.getColor(R.color.badge_color);
+        if (badgeColor == Color.TRANSPARENT) {
+            // Colors will be extracted per app icon, so a static palette won't work.
+            return null;
+        }
+        if (sBadgePalette == null) {
+            sBadgePalette = fromDominantColor(badgeColor, false);
+        }
+        return sBadgePalette;
+    }
+
+    /**
+     * Returns an IconPalette based on the folder_badge_color in colors.xml.
+     */
+    public static @NonNull IconPalette getFolderBadgePalette(Resources resources) {
+        if (sFolderBadgePalette == null) {
+            int badgeColor = resources.getColor(R.color.folder_badge_color);
+            sFolderBadgePalette = fromDominantColor(badgeColor, false);
+        }
+        return sFolderBadgePalette;
     }
 
     /**
@@ -172,13 +211,13 @@ public class IconPalette {
         return ColorUtils.LABToColor(low, a, b);
     }
 
-    private static int getMutedColor(int color) {
-        int alpha = (int) (255 * 0.15f);
-        return ColorUtils.compositeColors(ColorUtils.setAlphaComponent(color, alpha), Color.WHITE);
+    private static int getMutedColor(int color, float whiteScrimAlpha) {
+        int whiteScrim = ColorUtils.setAlphaComponent(Color.WHITE, (int) (255 * whiteScrimAlpha));
+        return ColorUtils.compositeColors(whiteScrim, color);
     }
 
     private static int getTextColorForBackground(int backgroundColor) {
-        return getLighterOrDarkerVersionOfColor(backgroundColor, 3f);
+        return getLighterOrDarkerVersionOfColor(backgroundColor, 4.5f);
     }
 
     private static int getLowContrastColor(int color) {