OSDN Git Service

FastBitmapDrawable can draw an icon badge (notification count)
[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.graphics.Color;
20 import android.support.v4.graphics.ColorUtils;
21
22 /**
23  * Contains colors based on the dominant color of an icon.
24  */
25 public class IconPalette {
26
27     public int backgroundColor;
28     public int textColor;
29
30     public static IconPalette fromDominantColor(int dominantColor) {
31         IconPalette palette = new IconPalette();
32         palette.backgroundColor = getMutedColor(dominantColor);
33         palette.textColor = getTextColorForBackground(palette.backgroundColor);
34         return palette;
35     }
36
37     private static int getMutedColor(int color) {
38         int alpha = (int) (255 * 0.2f);
39         return ColorUtils.compositeColors(ColorUtils.setAlphaComponent(color, alpha), Color.WHITE);
40     }
41
42     private static int getTextColorForBackground(int backgroundColor) {
43         return getLighterOrDarkerVersionOfColor(backgroundColor, 3f);
44     }
45
46     private static int getLowContrastColor(int color) {
47         return getLighterOrDarkerVersionOfColor(color, 1.5f);
48     }
49
50     private static int getLighterOrDarkerVersionOfColor(int color, float contrastRatio) {
51         int whiteMinAlpha = ColorUtils.calculateMinimumAlpha(Color.WHITE, color, contrastRatio);
52         int blackMinAlpha = ColorUtils.calculateMinimumAlpha(Color.BLACK, color, contrastRatio);
53         int translucentWhiteOrBlack;
54         if (whiteMinAlpha >= 0) {
55             translucentWhiteOrBlack = ColorUtils.setAlphaComponent(Color.WHITE, whiteMinAlpha);
56         } else if (blackMinAlpha >= 0) {
57             translucentWhiteOrBlack = ColorUtils.setAlphaComponent(Color.BLACK, blackMinAlpha);
58         } else {
59             translucentWhiteOrBlack = Color.WHITE;
60         }
61         return ColorUtils.compositeColors(translucentWhiteOrBlack, color);
62     }
63 }