OSDN Git Service

Simplifying some launcher themes
[android-x86/packages-apps-Launcher3.git] / src / com / android / launcher3 / graphics / IconPalette.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.launcher3.graphics;
18
19 import android.app.Notification;
20 import android.content.Context;
21 import android.graphics.Color;
22 import android.support.v4.graphics.ColorUtils;
23 import android.util.Log;
24
25 import com.android.launcher3.R;
26 import com.android.launcher3.Utilities;
27 import com.android.launcher3.util.Themes;
28
29 /**
30  * Contains colors based on the dominant color of an icon.
31  */
32 public class IconPalette {
33
34     private static final boolean DEBUG = false;
35     private static final String TAG = "IconPalette";
36
37     public static final IconPalette FOLDER_ICON_PALETTE = new IconPalette(Color.WHITE);
38
39     private static final float MIN_PRELOAD_COLOR_SATURATION = 0.2f;
40     private static final float MIN_PRELOAD_COLOR_LIGHTNESS = 0.6f;
41
42     public final int dominantColor;
43     public final int backgroundColor;
44     public final int textColor;
45     public final int secondaryColor;
46
47     private IconPalette(int color) {
48         dominantColor = color;
49         backgroundColor = getMutedColor(dominantColor);
50         textColor = getTextColorForBackground(backgroundColor);
51         secondaryColor = getLowContrastColor(backgroundColor);
52     }
53
54     /**
55      * Returns a color suitable for the progress bar color of preload icon.
56      */
57     public int getPreloadProgressColor(Context context) {
58         int result = dominantColor;
59
60         // Make sure that the dominant color has enough saturation to be visible properly.
61         float[] hsv = new float[3];
62         Color.colorToHSV(result, hsv);
63         if (hsv[1] < MIN_PRELOAD_COLOR_SATURATION) {
64             result = Themes.getColorAccent(context);
65         } else {
66             hsv[2] = Math.max(MIN_PRELOAD_COLOR_LIGHTNESS, hsv[2]);
67             result = Color.HSVToColor(hsv);
68         }
69         return result;
70     }
71
72     public static IconPalette fromDominantColor(int dominantColor) {
73         return new IconPalette(dominantColor);
74     }
75
76     /**
77      * Resolves a color such that it has enough contrast to be used as the
78      * color of an icon or text on the given background color.
79      *
80      * @return a color of the same hue with enough contrast against the background.
81      *
82      * This was copied from com.android.internal.util.NotificationColorUtil.
83      */
84     public static int resolveContrastColor(Context context, int color, int background) {
85         final int resolvedColor = resolveColor(context, color);
86
87         int contrastingColor = ensureTextContrast(resolvedColor, background);
88
89         if (contrastingColor != resolvedColor) {
90             if (DEBUG){
91                 Log.w(TAG, String.format(
92                         "Enhanced contrast of notification for %s " +
93                                 "%s (over background) by changing #%s to %s",
94                         context.getPackageName(),
95                         contrastChange(resolvedColor, contrastingColor, background),
96                         Integer.toHexString(resolvedColor), Integer.toHexString(contrastingColor)));
97             }
98         }
99         return contrastingColor;
100     }
101
102     /**
103      * Resolves {@param color} to an actual color if it is {@link Notification#COLOR_DEFAULT}
104      *
105      * This was copied from com.android.internal.util.NotificationColorUtil.
106      */
107     private static int resolveColor(Context context, int color) {
108         if (color == Notification.COLOR_DEFAULT) {
109             return context.getColor(R.color.notification_icon_default_color);
110         }
111         return color;
112     }
113
114     /** For debugging. This was copied from com.android.internal.util.NotificationColorUtil. */
115     private static String contrastChange(int colorOld, int colorNew, int bg) {
116         return String.format("from %.2f:1 to %.2f:1",
117                 ColorUtils.calculateContrast(colorOld, bg),
118                 ColorUtils.calculateContrast(colorNew, bg));
119     }
120
121     /**
122      * Finds a text color with sufficient contrast over bg that has the same hue as the original
123      * color.
124      *
125      * This was copied from com.android.internal.util.NotificationColorUtil.
126      */
127     private static int ensureTextContrast(int color, int bg) {
128         return findContrastColor(color, bg, true, 4.5);
129     }
130     /**
131      * Finds a suitable color such that there's enough contrast.
132      *
133      * @param color the color to start searching from.
134      * @param other the color to ensure contrast against. Assumed to be lighter than {@param color}
135      * @param findFg if true, we assume {@param color} is a foreground, otherwise a background.
136      * @param minRatio the minimum contrast ratio required.
137      * @return a color with the same hue as {@param color}, potentially darkened to meet the
138      *          contrast ratio.
139      *
140      * This was copied from com.android.internal.util.NotificationColorUtil.
141      */
142     private static int findContrastColor(int color, int other, boolean findFg, double minRatio) {
143         int fg = findFg ? color : other;
144         int bg = findFg ? other : color;
145         if (ColorUtils.calculateContrast(fg, bg) >= minRatio) {
146             return color;
147         }
148
149         double[] lab = new double[3];
150         ColorUtils.colorToLAB(findFg ? fg : bg, lab);
151
152         double low = 0, high = lab[0];
153         final double a = lab[1], b = lab[2];
154         for (int i = 0; i < 15 && high - low > 0.00001; i++) {
155             final double l = (low + high) / 2;
156             if (findFg) {
157                 fg = ColorUtils.LABToColor(l, a, b);
158             } else {
159                 bg = ColorUtils.LABToColor(l, a, b);
160             }
161             if (ColorUtils.calculateContrast(fg, bg) > minRatio) {
162                 low = l;
163             } else {
164                 high = l;
165             }
166         }
167         return ColorUtils.LABToColor(low, a, b);
168     }
169
170     private static int getMutedColor(int color) {
171         int alpha = (int) (255 * 0.15f);
172         return ColorUtils.compositeColors(ColorUtils.setAlphaComponent(color, alpha), Color.WHITE);
173     }
174
175     private static int getTextColorForBackground(int backgroundColor) {
176         return getLighterOrDarkerVersionOfColor(backgroundColor, 3f);
177     }
178
179     private static int getLowContrastColor(int color) {
180         return getLighterOrDarkerVersionOfColor(color, 1.5f);
181     }
182
183     private static int getLighterOrDarkerVersionOfColor(int color, float contrastRatio) {
184         int whiteMinAlpha = ColorUtils.calculateMinimumAlpha(Color.WHITE, color, contrastRatio);
185         int blackMinAlpha = ColorUtils.calculateMinimumAlpha(Color.BLACK, color, contrastRatio);
186         int translucentWhiteOrBlack;
187         if (whiteMinAlpha >= 0) {
188             translucentWhiteOrBlack = ColorUtils.setAlphaComponent(Color.WHITE, whiteMinAlpha);
189         } else if (blackMinAlpha >= 0) {
190             translucentWhiteOrBlack = ColorUtils.setAlphaComponent(Color.BLACK, blackMinAlpha);
191         } else {
192             translucentWhiteOrBlack = Color.WHITE;
193         }
194         return ColorUtils.compositeColors(translucentWhiteOrBlack, color);
195     }
196 }