2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3.graphics;
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;
25 import com.android.launcher3.R;
26 import com.android.launcher3.Utilities;
27 import com.android.launcher3.util.Themes;
30 * Contains colors based on the dominant color of an icon.
32 public class IconPalette {
34 private static final boolean DEBUG = false;
35 private static final String TAG = "IconPalette";
37 public static final IconPalette FOLDER_ICON_PALETTE = new IconPalette(Color.WHITE);
39 private static final float MIN_PRELOAD_COLOR_SATURATION = 0.2f;
40 private static final float MIN_PRELOAD_COLOR_LIGHTNESS = 0.6f;
42 public final int dominantColor;
43 public final int backgroundColor;
44 public final int textColor;
45 public final int secondaryColor;
47 private IconPalette(int color) {
48 dominantColor = color;
49 backgroundColor = getMutedColor(dominantColor);
50 textColor = getTextColorForBackground(backgroundColor);
51 secondaryColor = getLowContrastColor(backgroundColor);
55 * Returns a color suitable for the progress bar color of preload icon.
57 public int getPreloadProgressColor(Context context) {
58 int result = dominantColor;
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);
66 hsv[2] = Math.max(MIN_PRELOAD_COLOR_LIGHTNESS, hsv[2]);
67 result = Color.HSVToColor(hsv);
72 public static IconPalette fromDominantColor(int dominantColor) {
73 return new IconPalette(dominantColor);
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.
80 * @return a color of the same hue with enough contrast against the background.
82 * This was copied from com.android.internal.util.NotificationColorUtil.
84 public static int resolveContrastColor(Context context, int color, int background) {
85 final int resolvedColor = resolveColor(context, color);
87 int contrastingColor = ensureTextContrast(resolvedColor, background);
89 if (contrastingColor != resolvedColor) {
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)));
99 return contrastingColor;
103 * Resolves {@param color} to an actual color if it is {@link Notification#COLOR_DEFAULT}
105 * This was copied from com.android.internal.util.NotificationColorUtil.
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);
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));
122 * Finds a text color with sufficient contrast over bg that has the same hue as the original
125 * This was copied from com.android.internal.util.NotificationColorUtil.
127 private static int ensureTextContrast(int color, int bg) {
128 return findContrastColor(color, bg, true, 4.5);
131 * Finds a suitable color such that there's enough contrast.
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
140 * This was copied from com.android.internal.util.NotificationColorUtil.
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) {
149 double[] lab = new double[3];
150 ColorUtils.colorToLAB(findFg ? fg : bg, lab);
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;
157 fg = ColorUtils.LABToColor(l, a, b);
159 bg = ColorUtils.LABToColor(l, a, b);
161 if (ColorUtils.calculateContrast(fg, bg) > minRatio) {
167 return ColorUtils.LABToColor(low, a, b);
170 private static int getMutedColor(int color) {
171 int alpha = (int) (255 * 0.15f);
172 return ColorUtils.compositeColors(ColorUtils.setAlphaComponent(color, alpha), Color.WHITE);
175 private static int getTextColorForBackground(int backgroundColor) {
176 return getLighterOrDarkerVersionOfColor(backgroundColor, 3f);
179 private static int getLowContrastColor(int color) {
180 return getLighterOrDarkerVersionOfColor(color, 1.5f);
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);
192 translucentWhiteOrBlack = Color.WHITE;
194 return ColorUtils.compositeColors(translucentWhiteOrBlack, color);