/* * Copyright 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.support.v7.graphics; import android.graphics.Bitmap; import android.graphics.Color; import android.os.AsyncTask; import android.support.annotation.ColorInt; import android.support.annotation.Nullable; import android.support.v4.graphics.ColorUtils; import android.support.v4.os.AsyncTaskCompat; import android.util.TimingLogger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * A helper class to extract prominent colors from an image. *
* A number of colors with different profiles are extracted from the image: *
* Instances are created with a {@link Builder} which supports several options to tweak the * generated Palette. See that class' documentation for more information. *
* Generation should always be completed on a background thread, ideally the one in * which you load your image on. {@link Builder} supports both synchronous and asynchronous * generation: * *
* // Synchronous * Palette p = Palette.from(bitmap).generate(); * * // Asynchronous * Palette.from(bitmap).generate(new PaletteAsyncListener() { * public void onGenerated(Palette p) { * // Use generated instance * } * }); **/ public final class Palette { /** * Listener to be used with {@link #generateAsync(Bitmap, PaletteAsyncListener)} or * {@link #generateAsync(Bitmap, int, PaletteAsyncListener)} */ public interface PaletteAsyncListener { /** * Called when the {@link Palette} has been generated. */ void onGenerated(Palette palette); } private static final int DEFAULT_RESIZE_BITMAP_MAX_DIMENSION = 192; private static final int DEFAULT_CALCULATE_NUMBER_COLORS = 16; private static final float MIN_CONTRAST_TITLE_TEXT = 3.0f; private static final float MIN_CONTRAST_BODY_TEXT = 4.5f; private static final String LOG_TAG = "Palette"; private static final boolean LOG_TIMINGS = false; /** * Start generating a {@link Palette} with the returned {@link Builder} instance. */ public static Builder from(Bitmap bitmap) { return new Builder(bitmap); } /** * Generate a {@link Palette} from the pre-generated list of {@link Palette.Swatch} swatches. * This is useful for testing, or if you want to resurrect a {@link Palette} instance from a * list of swatches. Will return null if the {@code swatches} is null. */ public static Palette from(List
* Good values for depend on the source image type. For landscapes, good values are in * the range 10-16. For images which are largely made up of people's faces then this * value should be increased to ~24. */ public Builder maximumColorCount(int colors) { mMaxColors = colors; return this; } /** * Set the resize value when using a {@link android.graphics.Bitmap} as the source. * If the bitmap's largest dimension is greater than the value specified, then the bitmap * will be resized so that it's largest dimension matches {@code maxDimension}. If the * bitmap is smaller or equal, the original is used as-is. *
* This value has a large effect on the processing time. The larger the resized image is,
* the greater time it will take to generate the palette. The smaller the image is, the
* more detail is lost in the resulting image and thus less precision for color selection.
*/
public Builder resizeBitmapSize(int maxDimension) {
mResizeMaxDimension = maxDimension;
return this;
}
/**
* Clear all added filters. This includes any default filters added automatically by
* {@link Palette}.
*/
public Builder clearFilters() {
mFilters.clear();
return this;
}
/**
* Add a filter to be able to have fine grained controlled over the colors which are
* allowed in the resulting palette.
*
* @param filter filter to add.
*/
public Builder addFilter(Filter filter) {
if (filter != null) {
mFilters.add(filter);
}
return this;
}
/**
* Generate and return the {@link Palette} synchronously.
*/
public Palette generate() {
final TimingLogger logger = LOG_TIMINGS
? new TimingLogger(LOG_TAG, "Generation")
: null;
List
* This method will probably be called on a background thread.
*/
public abstract void generate(List