Android APIs
public final class

Palette

extends Object
java.lang.Object
   ↳ android.support.v7.graphics.Palette

Class Overview

A helper class to extract prominent colors from an image.

A number of colors with different profiles are extracted from the image:

  • Vibrant
  • Vibrant Dark
  • Vibrant Light
  • Muted
  • Muted Dark
  • Muted Light
These can be retrieved from the appropriate getter method.

Instances can be created with the synchronous factory methods generate(Bitmap) and generate(Bitmap, int).

These should be called on a background thread, ideally the one in which you load your images on. Sometimes that is not possible, so asynchronous factory methods have also been provided: generateAsync(Bitmap, PaletteAsyncListener) and generateAsync(Bitmap, int, PaletteAsyncListener). These can be used as so:

 Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
     public void onGenerated(Palette palette) {
         // Do something with colors...
     }
 });
 

Summary

Nested Classes
interface Palette.PaletteAsyncListener Listener to be used with generateAsync(Bitmap, PaletteAsyncListener) or generateAsync(Bitmap, int, PaletteAsyncListener)  
class Palette.Swatch Represents a color swatch generated from an image's palette. 
Public Methods
boolean equals(Object o)
Compares this instance with the specified object and indicates if they are equal.
static Palette generate(Bitmap bitmap)
Generate a Palette from a Bitmap using the default number of colors.
static Palette generate(Bitmap bitmap, int numColors)
Generate a Palette from a Bitmap using the specified numColors.
static AsyncTask<BitmapVoidPalette> generateAsync(Bitmap bitmap, int numColors, Palette.PaletteAsyncListener listener)
Generate a Palette asynchronously.
static AsyncTask<BitmapVoidPalette> generateAsync(Bitmap bitmap, Palette.PaletteAsyncListener listener)
Generate a Palette asynchronously.
int getDarkMutedColor(int defaultColor)
Returns a muted and dark color from the palette as an RGB packed int.
Palette.Swatch getDarkMutedSwatch()
Returns a muted and dark swatch from the palette.
int getDarkVibrantColor(int defaultColor)
Returns a dark and vibrant color from the palette as an RGB packed int.
Palette.Swatch getDarkVibrantSwatch()
Returns a dark and vibrant swatch from the palette.
int getLightMutedColor(int defaultColor)
Returns a muted and light color from the palette as an RGB packed int.
Palette.Swatch getLightMutedSwatch()
Returns a muted and light swatch from the palette.
int getLightVibrantColor(int defaultColor)
Returns a light and vibrant color from the palette as an RGB packed int.
Palette.Swatch getLightVibrantSwatch()
Returns a light and vibrant swatch from the palette.
int getMutedColor(int defaultColor)
Returns a muted color from the palette as an RGB packed int.
Palette.Swatch getMutedSwatch()
Returns a muted swatch from the palette.
List<Palette.Swatch> getSwatches()
Returns all of the swatches which make up the palette.
int getVibrantColor(int defaultColor)
Returns the most vibrant color in the palette as an RGB packed int.
Palette.Swatch getVibrantSwatch()
Returns the most vibrant swatch in the palette.
int hashCode()
Returns an integer hash code for this object.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public boolean equals (Object o)

Compares this instance with the specified object and indicates if they are equal. In order to be equal, o must represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.

The default implementation returns true only if this == o. See Writing a correct equals method if you intend implementing your own equals method.

The general contract for the equals and hashCode() methods is that if equals returns true for any two objects, then hashCode() must return the same value for these objects. This means that subclasses of Object usually override either both methods or neither of them.

Parameters
o the object to compare this instance with.
Returns
  • true if the specified object is equal to this Object; false otherwise.

public static Palette generate (Bitmap bitmap)

Generate a Palette from a Bitmap using the default number of colors.

public static Palette generate (Bitmap bitmap, int numColors)

Generate a Palette from a Bitmap using the specified numColors. Good values for numColors depend on the source image type. For landscapes, a good values are in the range 12-16. For images which are largely made up of people's faces then this value should be increased to 24-32.

Parameters
numColors The maximum number of colors in the generated palette. Increasing this number will increase the time needed to compute the values.

public static AsyncTask<BitmapVoidPalette> generateAsync (Bitmap bitmap, int numColors, Palette.PaletteAsyncListener listener)

Generate a Palette asynchronously. onGenerated(Palette) will be called with the created instance. The resulting Palette is the same as what would be created by calling generate(Bitmap, int).

Parameters
listener Listener to be invoked when the Palette has been generated.
Returns
  • the AsyncTask used to asynchronously generate the instance.

public static AsyncTask<BitmapVoidPalette> generateAsync (Bitmap bitmap, Palette.PaletteAsyncListener listener)

Generate a Palette asynchronously. onGenerated(Palette) will be called with the created instance. The resulting Palette is the same as what would be created by calling generate(Bitmap).

Parameters
listener Listener to be invoked when the Palette has been generated.
Returns
  • the AsyncTask used to asynchronously generate the instance.

public int getDarkMutedColor (int defaultColor)

Returns a muted and dark color from the palette as an RGB packed int.

Parameters
defaultColor value to return if the swatch isn't available

public Palette.Swatch getDarkMutedSwatch ()

Returns a muted and dark swatch from the palette. Might be null.

public int getDarkVibrantColor (int defaultColor)

Returns a dark and vibrant color from the palette as an RGB packed int.

Parameters
defaultColor value to return if the swatch isn't available

public Palette.Swatch getDarkVibrantSwatch ()

Returns a dark and vibrant swatch from the palette. Might be null.

public int getLightMutedColor (int defaultColor)

Returns a muted and light color from the palette as an RGB packed int.

Parameters
defaultColor value to return if the swatch isn't available

public Palette.Swatch getLightMutedSwatch ()

Returns a muted and light swatch from the palette. Might be null.

public int getLightVibrantColor (int defaultColor)

Returns a light and vibrant color from the palette as an RGB packed int.

Parameters
defaultColor value to return if the swatch isn't available

public Palette.Swatch getLightVibrantSwatch ()

Returns a light and vibrant swatch from the palette. Might be null.

public int getMutedColor (int defaultColor)

Returns a muted color from the palette as an RGB packed int.

Parameters
defaultColor value to return if the swatch isn't available

public Palette.Swatch getMutedSwatch ()

Returns a muted swatch from the palette. Might be null.

public List<Palette.Swatch> getSwatches ()

Returns all of the swatches which make up the palette.

public int getVibrantColor (int defaultColor)

Returns the most vibrant color in the palette as an RGB packed int.

Parameters
defaultColor value to return if the swatch isn't available

public Palette.Swatch getVibrantSwatch ()

Returns the most vibrant swatch in the palette. Might be null.

public int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
  • this object's hash code.