Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 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 android.app; |
| 18 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
vincentpeng | be09f28 | 2018-12-11 11:05:02 +0800 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Canvas; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 24 | import android.graphics.Color; |
Lucas Dupin | ffbf37e | 2018-03-02 12:18:50 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 30 | import android.util.Size; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 31 | |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 32 | import com.android.internal.graphics.ColorUtils; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 33 | import com.android.internal.graphics.palette.Palette; |
Lucas Dupin | 1d3c00d5 | 2017-06-05 08:40:39 -0700 | [diff] [blame] | 34 | import com.android.internal.graphics.palette.VariationalKMeansQuantizer; |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 35 | import com.android.internal.util.ContrastColorUtil; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 36 | |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 37 | import java.io.FileOutputStream; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 39 | import java.util.Collections; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | |
| 42 | /** |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 43 | * Provides information about the colors of a wallpaper. |
| 44 | * <p> |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 45 | * Exposes the 3 most visually representative colors of a wallpaper. Can be either |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 46 | * {@link WallpaperColors#getPrimaryColor()}, {@link WallpaperColors#getSecondaryColor()} |
| 47 | * or {@link WallpaperColors#getTertiaryColor()}. |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 48 | */ |
| 49 | public final class WallpaperColors implements Parcelable { |
| 50 | |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 51 | private static final boolean DEBUG_DARK_PIXELS = false; |
| 52 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Specifies that dark text is preferred over the current wallpaper for best presentation. |
| 55 | * <p> |
| 56 | * eg. A launcher may set its text color to black if this flag is specified. |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 57 | * @hide |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 58 | */ |
vincentpeng | be09f28 | 2018-12-11 11:05:02 +0800 | [diff] [blame] | 59 | @SystemApi |
Lucas Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 60 | public static final int HINT_SUPPORTS_DARK_TEXT = 1 << 0; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 61 | |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Specifies that dark theme is preferred over the current wallpaper for best presentation. |
| 64 | * <p> |
| 65 | * eg. A launcher may set its drawer color to black if this flag is specified. |
| 66 | * @hide |
| 67 | */ |
vincentpeng | be09f28 | 2018-12-11 11:05:02 +0800 | [diff] [blame] | 68 | @SystemApi |
Lucas Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 69 | public static final int HINT_SUPPORTS_DARK_THEME = 1 << 1; |
| 70 | |
| 71 | /** |
| 72 | * Specifies that this object was generated by extracting colors from a bitmap. |
| 73 | * @hide |
| 74 | */ |
| 75 | public static final int HINT_FROM_BITMAP = 1 << 2; |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 76 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 77 | // Maximum size that a bitmap can have to keep our calculations sane |
| 78 | private static final int MAX_BITMAP_SIZE = 112; |
| 79 | |
| 80 | // Even though we have a maximum size, we'll mainly match bitmap sizes |
| 81 | // using the area instead. This way our comparisons are aspect ratio independent. |
| 82 | private static final int MAX_WALLPAPER_EXTRACTION_AREA = MAX_BITMAP_SIZE * MAX_BITMAP_SIZE; |
| 83 | |
| 84 | // When extracting the main colors, only consider colors |
| 85 | // present in at least MIN_COLOR_OCCURRENCE of the image |
| 86 | private static final float MIN_COLOR_OCCURRENCE = 0.05f; |
| 87 | |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 88 | // Decides when dark theme is optimal for this wallpaper |
| 89 | private static final float DARK_THEME_MEAN_LUMINANCE = 0.25f; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 90 | // Minimum mean luminosity that an image needs to have to support dark text |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 91 | private static final float BRIGHT_IMAGE_MEAN_LUMINANCE = 0.75f; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 92 | // We also check if the image has dark pixels in it, |
| 93 | // to avoid bright images with some dark spots. |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 94 | private static final float DARK_PIXEL_CONTRAST = 6f; |
| 95 | private static final float MAX_DARK_AREA = 0.025f; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 96 | |
| 97 | private final ArrayList<Color> mMainColors; |
| 98 | private int mColorHints; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 99 | |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 100 | public WallpaperColors(Parcel parcel) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 101 | mMainColors = new ArrayList<>(); |
| 102 | final int count = parcel.readInt(); |
| 103 | for (int i = 0; i < count; i++) { |
| 104 | final int colorInt = parcel.readInt(); |
| 105 | Color color = Color.valueOf(colorInt); |
| 106 | mMainColors.add(color); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 107 | } |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 108 | mColorHints = parcel.readInt(); |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 112 | * Constructs {@link WallpaperColors} from a drawable. |
| 113 | * <p> |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 114 | * Main colors will be extracted from the drawable. |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 115 | * |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 116 | * @param drawable Source where to extract from. |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 117 | */ |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 118 | public static WallpaperColors fromDrawable(Drawable drawable) { |
Lucas Dupin | ffbf37e | 2018-03-02 12:18:50 -0800 | [diff] [blame] | 119 | if (drawable == null) { |
| 120 | throw new IllegalArgumentException("Drawable cannot be null"); |
| 121 | } |
| 122 | |
| 123 | Rect initialBounds = drawable.copyBounds(); |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 124 | int width = drawable.getIntrinsicWidth(); |
| 125 | int height = drawable.getIntrinsicHeight(); |
| 126 | |
| 127 | // Some drawables do not have intrinsic dimensions |
| 128 | if (width <= 0 || height <= 0) { |
| 129 | width = MAX_BITMAP_SIZE; |
| 130 | height = MAX_BITMAP_SIZE; |
| 131 | } |
| 132 | |
| 133 | Size optimalSize = calculateOptimalSize(width, height); |
| 134 | Bitmap bitmap = Bitmap.createBitmap(optimalSize.getWidth(), optimalSize.getHeight(), |
| 135 | Bitmap.Config.ARGB_8888); |
| 136 | final Canvas bmpCanvas = new Canvas(bitmap); |
| 137 | drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); |
| 138 | drawable.draw(bmpCanvas); |
| 139 | |
| 140 | final WallpaperColors colors = WallpaperColors.fromBitmap(bitmap); |
| 141 | bitmap.recycle(); |
| 142 | |
Lucas Dupin | ffbf37e | 2018-03-02 12:18:50 -0800 | [diff] [blame] | 143 | drawable.setBounds(initialBounds); |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 144 | return colors; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 148 | * Constructs {@link WallpaperColors} from a bitmap. |
| 149 | * <p> |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 150 | * Main colors will be extracted from the bitmap. |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 151 | * |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 152 | * @param bitmap Source where to extract from. |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 153 | */ |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 154 | public static WallpaperColors fromBitmap(@NonNull Bitmap bitmap) { |
| 155 | if (bitmap == null) { |
| 156 | throw new IllegalArgumentException("Bitmap can't be null"); |
| 157 | } |
| 158 | |
| 159 | final int bitmapArea = bitmap.getWidth() * bitmap.getHeight(); |
Lucas Dupin | 42acf60 | 2017-07-13 16:32:44 -0700 | [diff] [blame] | 160 | boolean shouldRecycle = false; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 161 | if (bitmapArea > MAX_WALLPAPER_EXTRACTION_AREA) { |
Lucas Dupin | 42acf60 | 2017-07-13 16:32:44 -0700 | [diff] [blame] | 162 | shouldRecycle = true; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 163 | Size optimalSize = calculateOptimalSize(bitmap.getWidth(), bitmap.getHeight()); |
Lucas Dupin | 42acf60 | 2017-07-13 16:32:44 -0700 | [diff] [blame] | 164 | bitmap = Bitmap.createScaledBitmap(bitmap, optimalSize.getWidth(), |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 165 | optimalSize.getHeight(), true /* filter */); |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | final Palette palette = Palette |
| 169 | .from(bitmap) |
Lucas Dupin | 1d3c00d5 | 2017-06-05 08:40:39 -0700 | [diff] [blame] | 170 | .setQuantizer(new VariationalKMeansQuantizer()) |
| 171 | .maximumColorCount(5) |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 172 | .clearFilters() |
| 173 | .resizeBitmapArea(MAX_WALLPAPER_EXTRACTION_AREA) |
| 174 | .generate(); |
| 175 | |
| 176 | // Remove insignificant colors and sort swatches by population |
| 177 | final ArrayList<Palette.Swatch> swatches = new ArrayList<>(palette.getSwatches()); |
| 178 | final float minColorArea = bitmap.getWidth() * bitmap.getHeight() * MIN_COLOR_OCCURRENCE; |
| 179 | swatches.removeIf(s -> s.getPopulation() < minColorArea); |
| 180 | swatches.sort((a, b) -> b.getPopulation() - a.getPopulation()); |
| 181 | |
| 182 | final int swatchesSize = swatches.size(); |
| 183 | Color primary = null, secondary = null, tertiary = null; |
| 184 | |
| 185 | swatchLoop: |
| 186 | for (int i = 0; i < swatchesSize; i++) { |
| 187 | Color color = Color.valueOf(swatches.get(i).getRgb()); |
| 188 | switch (i) { |
| 189 | case 0: |
| 190 | primary = color; |
| 191 | break; |
| 192 | case 1: |
| 193 | secondary = color; |
| 194 | break; |
| 195 | case 2: |
| 196 | tertiary = color; |
| 197 | break; |
| 198 | default: |
| 199 | // out of bounds |
| 200 | break swatchLoop; |
| 201 | } |
| 202 | } |
| 203 | |
Lucas Dupin | b5e5053 | 2018-05-24 16:33:14 +0000 | [diff] [blame] | 204 | int hints = calculateDarkHints(bitmap); |
Lucas Dupin | 42acf60 | 2017-07-13 16:32:44 -0700 | [diff] [blame] | 205 | |
| 206 | if (shouldRecycle) { |
| 207 | bitmap.recycle(); |
| 208 | } |
| 209 | |
Lucas Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 210 | return new WallpaperColors(primary, secondary, tertiary, HINT_FROM_BITMAP | hints); |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 214 | * Constructs a new object from three colors. |
| 215 | * |
| 216 | * @param primaryColor Primary color. |
| 217 | * @param secondaryColor Secondary color. |
| 218 | * @param tertiaryColor Tertiary color. |
| 219 | * @see WallpaperColors#fromBitmap(Bitmap) |
| 220 | * @see WallpaperColors#fromDrawable(Drawable) |
| 221 | */ |
| 222 | public WallpaperColors(@NonNull Color primaryColor, @Nullable Color secondaryColor, |
| 223 | @Nullable Color tertiaryColor) { |
| 224 | this(primaryColor, secondaryColor, tertiaryColor, 0); |
Lucas Dupin | 2e7170b | 2020-03-21 17:07:17 -0700 | [diff] [blame] | 225 | |
| 226 | // Calculate dark theme support based on primary color. |
| 227 | final float[] tmpHsl = new float[3]; |
| 228 | ColorUtils.colorToHSL(primaryColor.toArgb(), tmpHsl); |
| 229 | final float luminance = tmpHsl[2]; |
| 230 | if (luminance < DARK_THEME_MEAN_LUMINANCE) { |
| 231 | mColorHints |= HINT_SUPPORTS_DARK_THEME; |
| 232 | } |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 236 | * Constructs a new object from three colors, where hints can be specified. |
| 237 | * |
| 238 | * @param primaryColor Primary color. |
| 239 | * @param secondaryColor Secondary color. |
| 240 | * @param tertiaryColor Tertiary color. |
| 241 | * @param colorHints A combination of WallpaperColor hints. |
| 242 | * @see WallpaperColors#HINT_SUPPORTS_DARK_TEXT |
| 243 | * @see WallpaperColors#fromBitmap(Bitmap) |
| 244 | * @see WallpaperColors#fromDrawable(Drawable) |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 245 | * @hide |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 246 | */ |
vincentpeng | be09f28 | 2018-12-11 11:05:02 +0800 | [diff] [blame] | 247 | @SystemApi |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 248 | public WallpaperColors(@NonNull Color primaryColor, @Nullable Color secondaryColor, |
| 249 | @Nullable Color tertiaryColor, int colorHints) { |
| 250 | |
| 251 | if (primaryColor == null) { |
| 252 | throw new IllegalArgumentException("Primary color should never be null."); |
| 253 | } |
| 254 | |
| 255 | mMainColors = new ArrayList<>(3); |
| 256 | mMainColors.add(primaryColor); |
| 257 | if (secondaryColor != null) { |
| 258 | mMainColors.add(secondaryColor); |
| 259 | } |
| 260 | if (tertiaryColor != null) { |
| 261 | if (secondaryColor == null) { |
| 262 | throw new IllegalArgumentException("tertiaryColor can't be specified when " |
| 263 | + "secondaryColor is null"); |
| 264 | } |
| 265 | mMainColors.add(tertiaryColor); |
| 266 | } |
| 267 | |
| 268 | mColorHints = colorHints; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 271 | public static final @android.annotation.NonNull Creator<WallpaperColors> CREATOR = new Creator<WallpaperColors>() { |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 272 | @Override |
| 273 | public WallpaperColors createFromParcel(Parcel in) { |
| 274 | return new WallpaperColors(in); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public WallpaperColors[] newArray(int size) { |
| 279 | return new WallpaperColors[size]; |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | @Override |
| 284 | public int describeContents() { |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void writeToParcel(Parcel dest, int flags) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 290 | List<Color> mainColors = getMainColors(); |
| 291 | int count = mainColors.size(); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 292 | dest.writeInt(count); |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 293 | for (int i = 0; i < count; i++) { |
| 294 | Color color = mainColors.get(i); |
| 295 | dest.writeInt(color.toArgb()); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 296 | } |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 297 | dest.writeInt(mColorHints); |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 301 | * Gets the most visually representative color of the wallpaper. |
| 302 | * "Visually representative" means easily noticeable in the image, |
| 303 | * probably happening at high frequency. |
| 304 | * |
| 305 | * @return A color. |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 306 | */ |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 307 | public @NonNull Color getPrimaryColor() { |
| 308 | return mMainColors.get(0); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Gets the second most preeminent color of the wallpaper. Can be null. |
| 313 | * |
| 314 | * @return A color, may be null. |
| 315 | */ |
| 316 | public @Nullable Color getSecondaryColor() { |
| 317 | return mMainColors.size() < 2 ? null : mMainColors.get(1); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Gets the third most preeminent color of the wallpaper. Can be null. |
| 322 | * |
| 323 | * @return A color, may be null. |
| 324 | */ |
| 325 | public @Nullable Color getTertiaryColor() { |
| 326 | return mMainColors.size() < 3 ? null : mMainColors.get(2); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * List of most preeminent colors, sorted by importance. |
| 331 | * |
| 332 | * @return List of colors. |
| 333 | * @hide |
| 334 | */ |
| 335 | public @NonNull List<Color> getMainColors() { |
| 336 | return Collections.unmodifiableList(mMainColors); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public boolean equals(Object o) { |
| 341 | if (o == null || getClass() != o.getClass()) { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | WallpaperColors other = (WallpaperColors) o; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 346 | return mMainColors.equals(other.mMainColors) |
| 347 | && mColorHints == other.mColorHints; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public int hashCode() { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 352 | return 31 * mMainColors.hashCode() + mColorHints; |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 356 | * Combination of WallpaperColor hints. |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 357 | * |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 358 | * @see WallpaperColors#HINT_SUPPORTS_DARK_TEXT |
| 359 | * @return True if dark text is supported. |
Lucas Dupin | bdffdd5 | 2017-06-28 09:49:47 -0700 | [diff] [blame] | 360 | * @hide |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 361 | */ |
vincentpeng | be09f28 | 2018-12-11 11:05:02 +0800 | [diff] [blame] | 362 | @SystemApi |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 363 | public int getColorHints() { |
| 364 | return mColorHints; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 367 | /** |
| 368 | * @param colorHints Combination of WallpaperColors hints. |
| 369 | * @see WallpaperColors#HINT_SUPPORTS_DARK_TEXT |
| 370 | * @hide |
| 371 | */ |
| 372 | public void setColorHints(int colorHints) { |
| 373 | mColorHints = colorHints; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Checks if image is bright and clean enough to support light text. |
| 378 | * |
| 379 | * @param source What to read. |
| 380 | * @return Whether image supports dark text or not. |
| 381 | */ |
Lucas Dupin | e2efebc | 2017-08-11 10:30:58 -0700 | [diff] [blame] | 382 | private static int calculateDarkHints(Bitmap source) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 383 | if (source == null) { |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 384 | return 0; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 387 | int[] pixels = new int[source.getWidth() * source.getHeight()]; |
| 388 | double totalLuminance = 0; |
| 389 | final int maxDarkPixels = (int) (pixels.length * MAX_DARK_AREA); |
| 390 | int darkPixels = 0; |
| 391 | source.getPixels(pixels, 0 /* offset */, source.getWidth(), 0 /* x */, 0 /* y */, |
| 392 | source.getWidth(), source.getHeight()); |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 393 | |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 394 | // This bitmap was already resized to fit the maximum allowed area. |
| 395 | // Let's just loop through the pixels, no sweat! |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 396 | float[] tmpHsl = new float[3]; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 397 | for (int i = 0; i < pixels.length; i++) { |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 398 | ColorUtils.colorToHSL(pixels[i], tmpHsl); |
| 399 | final float luminance = tmpHsl[2]; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 400 | final int alpha = Color.alpha(pixels[i]); |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 401 | // Make sure we don't have a dark pixel mass that will |
| 402 | // make text illegible. |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 403 | final boolean satisfiesTextContrast = ContrastColorUtil |
| 404 | .calculateContrast(pixels[i], Color.BLACK) > DARK_PIXEL_CONTRAST; |
| 405 | if (!satisfiesTextContrast && alpha != 0) { |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 406 | darkPixels++; |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 407 | if (DEBUG_DARK_PIXELS) { |
| 408 | pixels[i] = Color.RED; |
| 409 | } |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 410 | } |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 411 | totalLuminance += luminance; |
Lucas Dupin | ea1fb1e | 2017-04-05 17:39:44 -0700 | [diff] [blame] | 412 | } |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 413 | |
| 414 | int hints = 0; |
| 415 | double meanLuminance = totalLuminance / pixels.length; |
| 416 | if (meanLuminance > BRIGHT_IMAGE_MEAN_LUMINANCE && darkPixels < maxDarkPixels) { |
| 417 | hints |= HINT_SUPPORTS_DARK_TEXT; |
| 418 | } |
| 419 | if (meanLuminance < DARK_THEME_MEAN_LUMINANCE) { |
| 420 | hints |= HINT_SUPPORTS_DARK_THEME; |
| 421 | } |
| 422 | |
Lucas Dupin | a291d19 | 2018-06-07 13:59:42 -0700 | [diff] [blame] | 423 | if (DEBUG_DARK_PIXELS) { |
| 424 | try (FileOutputStream out = new FileOutputStream("/data/pixels.png")) { |
| 425 | source.setPixels(pixels, 0, source.getWidth(), 0, 0, source.getWidth(), |
| 426 | source.getHeight()); |
| 427 | source.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 428 | } catch (Exception e) { |
| 429 | e.printStackTrace(); |
| 430 | } |
| 431 | Log.d("WallpaperColors", "l: " + meanLuminance + ", d: " + darkPixels + |
| 432 | " maxD: " + maxDarkPixels + " numPixels: " + pixels.length); |
| 433 | } |
| 434 | |
Lucas Dupin | 4bd24f3 | 2017-06-29 12:20:29 -0700 | [diff] [blame] | 435 | return hints; |
Lucas Dupin | 84b89d9 | 2017-05-09 12:16:19 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | private static Size calculateOptimalSize(int width, int height) { |
| 439 | // Calculate how big the bitmap needs to be. |
| 440 | // This avoids unnecessary processing and allocation inside Palette. |
| 441 | final int requestedArea = width * height; |
| 442 | double scale = 1; |
| 443 | if (requestedArea > MAX_WALLPAPER_EXTRACTION_AREA) { |
| 444 | scale = Math.sqrt(MAX_WALLPAPER_EXTRACTION_AREA / (double) requestedArea); |
| 445 | } |
| 446 | int newWidth = (int) (width * scale); |
| 447 | int newHeight = (int) (height * scale); |
| 448 | // Dealing with edge cases of the drawable being too wide or too tall. |
| 449 | // Width or height would end up being 0, in this case we'll set it to 1. |
| 450 | if (newWidth == 0) { |
| 451 | newWidth = 1; |
| 452 | } |
| 453 | if (newHeight == 0) { |
| 454 | newHeight = 1; |
| 455 | } |
| 456 | |
| 457 | return new Size(newWidth, newHeight); |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 458 | } |
Lucas Dupin | 50ba991 | 2017-07-14 11:55:05 -0700 | [diff] [blame] | 459 | |
| 460 | @Override |
| 461 | public String toString() { |
| 462 | final StringBuilder colors = new StringBuilder(); |
| 463 | for (int i = 0; i < mMainColors.size(); i++) { |
| 464 | colors.append(Integer.toHexString(mMainColors.get(i).toArgb())).append(" "); |
| 465 | } |
| 466 | return "[WallpaperColors: " + colors.toString() + "h: " + mColorHints + "]"; |
| 467 | } |
Lucas Dupin | c40608c | 2017-04-14 18:33:08 -0700 | [diff] [blame] | 468 | } |