blob: b3c70a49f660a09fd3f46e09958522c4973c04de [file] [log] [blame]
Lucas Dupinc40608c2017-04-14 18:33:08 -07001/*
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
17package android.app;
18
19import android.graphics.Color;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import android.util.Pair;
24
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070025import java.util.ArrayList;
Lucas Dupinc40608c2017-04-14 18:33:08 -070026import java.util.List;
27
28/**
29 * A class containing information about the colors of a wallpaper.
30 */
31public final class WallpaperColors implements Parcelable {
32
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070033 private static final float BRIGHT_LUMINANCE = 0.9f;
34 private final List<Pair<Color, Integer>> mColors;
35 private final boolean mSupportsDarkText;
36
Lucas Dupinc40608c2017-04-14 18:33:08 -070037 public WallpaperColors(Parcel parcel) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070038 mColors = new ArrayList<>();
39 int count = parcel.readInt();
40 for (int i=0; i < count; i++) {
41 Color color = Color.valueOf(parcel.readInt());
42 int weight = parcel.readInt();
43 mColors.add(new Pair<>(color, weight));
44 }
45 mSupportsDarkText = parcel.readBoolean();
Lucas Dupinc40608c2017-04-14 18:33:08 -070046 }
47
48 /**
49 * Wallpaper color details containing a list of colors and their weights,
50 * as if it were an histogram.
51 * This list can be extracted from a bitmap by the Palette API.
52 *
53 * Dark text support will be calculated internally based on the histogram.
54 *
55 * @param colors list of pairs where each pair contains a color
56 * and number of occurrences/influence.
57 */
58 public WallpaperColors(List<Pair<Color, Integer>> colors) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070059 this(colors, calculateDarkTextSupport(colors));
Lucas Dupinc40608c2017-04-14 18:33:08 -070060 }
61
62 /**
63 * Wallpaper color details containing a list of colors and their weights,
64 * as if it were an histogram.
65 * Explicit dark text support.
66 *
67 * @param colors list of pairs where each pair contains a color
68 * and number of occurrences/influence.
69 * @param supportsDarkText can have dark text on top or not
70 */
71 public WallpaperColors(List<Pair<Color, Integer>> colors, boolean supportsDarkText) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070072 if (colors == null)
73 colors = new ArrayList<>();
74 mColors = colors;
75 mSupportsDarkText = supportsDarkText;
Lucas Dupinc40608c2017-04-14 18:33:08 -070076 }
77
78 public static final Creator<WallpaperColors> CREATOR = new Creator<WallpaperColors>() {
79 @Override
80 public WallpaperColors createFromParcel(Parcel in) {
81 return new WallpaperColors(in);
82 }
83
84 @Override
85 public WallpaperColors[] newArray(int size) {
86 return new WallpaperColors[size];
87 }
88 };
89
90 @Override
91 public int describeContents() {
92 return 0;
93 }
94
95 @Override
96 public void writeToParcel(Parcel dest, int flags) {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -070097 int count = mColors.size();
98 dest.writeInt(count);
99 for (Pair<Color, Integer> color : mColors) {
100 dest.writeInt(color.first.toArgb());
101 dest.writeInt(color.second);
102 }
103 dest.writeBoolean(mSupportsDarkText);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700104 }
105
106 /**
107 * List of colors with their occurrences. The bigger the int, the more relevant the color.
108 * @return list of colors paired with their weights.
109 */
110 public List<Pair<Color, Integer>> getColors() {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700111 return mColors;
112 }
113
114 @Override
115 public boolean equals(Object o) {
116 if (o == null || getClass() != o.getClass()) {
117 return false;
118 }
119
120 WallpaperColors other = (WallpaperColors) o;
121 return mColors.equals(other.mColors) && mSupportsDarkText == other.mSupportsDarkText;
122 }
123
124 @Override
125 public int hashCode() {
126 return 31 * mColors.hashCode() + (mSupportsDarkText ? 1 : 0);
Lucas Dupinc40608c2017-04-14 18:33:08 -0700127 }
128
129 /**
130 * Whether or not dark text is legible on top of this wallpaper.
131 *
132 * @return true if dark text is supported
133 */
134 public boolean supportsDarkText() {
Lucas Dupinea1fb1e2017-04-05 17:39:44 -0700135 return mSupportsDarkText;
136 }
137
138 private static boolean calculateDarkTextSupport(List<Pair<Color, Integer>> colors) {
139 if (colors == null) {
140 return false;
141 }
142
143 Pair<Color, Integer> mainColor = null;
144
145 for (Pair<Color, Integer> color : colors) {
146 if (mainColor == null) {
147 mainColor = color;
148 } else if (color.second > mainColor.second) {
149 mainColor = color;
150 }
151 }
152 return mainColor != null &&
153 mainColor.first.luminance() > BRIGHT_LUMINANCE;
Lucas Dupinc40608c2017-04-14 18:33:08 -0700154 }
155}