Palette


public final class Palette


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 are created with a 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. 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
    }
});

Summary

Nested types

public final class Palette.Builder

Builder class for generating Palette instances.

public interface Palette.Filter

A Filter provides a mechanism for exercising fine-grained control over which colors are valid within a resulting Palette.

Listener to be used with generateAsync or generateAsync

public final class Palette.Swatch

Represents a color swatch generated from an image's palette.

Public methods

static @NonNull Palette.Builder
from(@NonNull Bitmap bitmap)

Start generating a Palette with the returned Builder instance.

static @NonNull Palette

Generate a Palette from the pre-generated list of Palette.Swatch swatches.

static Palette
generate(Bitmap bitmap)

This method is deprecated.

Use Builder to generate the Palette.

static Palette
generate(Bitmap bitmap, int numColors)

This method is deprecated.

Use Builder to generate the Palette.

static AsyncTask<BitmapVoidPalette>

This method is deprecated.

Use Builder to generate the Palette.

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

This method is deprecated.

Use Builder to generate the Palette.

@ColorInt int
getColorForTarget(@NonNull Target target, @ColorInt int defaultColor)

Returns the selected color for the given target from the palette as an RGB packed int.

@ColorInt int
getDarkMutedColor(@ColorInt int defaultColor)

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

@Nullable Palette.Swatch

Returns a muted and dark swatch from the palette.

@ColorInt int
getDarkVibrantColor(@ColorInt int defaultColor)

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

@Nullable Palette.Swatch

Returns a dark and vibrant swatch from the palette.

@ColorInt int
getDominantColor(@ColorInt int defaultColor)

Returns the color of the dominant swatch from the palette, as an RGB packed int.

@Nullable Palette.Swatch

Returns the dominant swatch from the palette.

@ColorInt int
getLightMutedColor(@ColorInt int defaultColor)

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

@Nullable Palette.Swatch

Returns a muted and light swatch from the palette.

@ColorInt int
getLightVibrantColor(@ColorInt int defaultColor)

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

@Nullable Palette.Swatch

Returns a light and vibrant swatch from the palette.

@ColorInt int
getMutedColor(@ColorInt int defaultColor)

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

@Nullable Palette.Swatch

Returns a muted swatch from the palette.

@Nullable Palette.Swatch

Returns the selected swatch for the given target from the palette, or null if one could not be found.

@NonNull List<Palette.Swatch>

Returns all of the swatches which make up the palette.

@NonNull List<Target>

Returns the targets used to generate this palette.

@ColorInt int
getVibrantColor(@ColorInt int defaultColor)

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

@Nullable Palette.Swatch

Returns the most vibrant swatch in the palette.

Extension functions

final Palette.Swatch
PaletteKt.get(@NonNull Palette receiver, @NonNull Target target)

Returns the selected swatch for the given target from the palette, or null if one could not be found.

Public methods

from

Added in 1.0.0
public static @NonNull Palette.Builder from(@NonNull Bitmap bitmap)

Start generating a Palette with the returned Builder instance.

from

Added in 1.0.0
public static @NonNull Palette from(@NonNull List<Palette.Swatch> swatches)

Generate a Palette from the pre-generated list of Palette.Swatch swatches. This is useful for testing, or if you want to resurrect a Palette instance from a list of swatches. Will return null if the swatches is null.

generate

Added in 1.0.0
Deprecated in 1.0.0
public static Palette generate(Bitmap bitmap)

generate

Added in 1.0.0
Deprecated in 1.0.0
public static Palette generate(Bitmap bitmap, int numColors)

generateAsync

Added in 1.0.0
Deprecated in 1.0.0
public static AsyncTask<BitmapVoidPalettegenerateAsync(Bitmap bitmap, Palette.PaletteAsyncListener listener)

generateAsync

Added in 1.0.0
Deprecated in 1.0.0
public static AsyncTask<BitmapVoidPalettegenerateAsync(
    Bitmap bitmap,
    int numColors,
    Palette.PaletteAsyncListener listener
)

getColorForTarget

Added in 1.0.0
public @ColorInt int getColorForTarget(@NonNull Target target, @ColorInt int defaultColor)

Returns the selected color for the given target from the palette as an RGB packed int.

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

getDarkMutedColor

Added in 1.0.0
public @ColorInt int getDarkMutedColor(@ColorInt int defaultColor)

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

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

getDarkMutedSwatch

Added in 1.0.0
public @Nullable Palette.Swatch getDarkMutedSwatch()

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

See also
DARK_MUTED

getDarkVibrantColor

Added in 1.0.0
public @ColorInt int getDarkVibrantColor(@ColorInt int defaultColor)

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

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

getDarkVibrantSwatch

Added in 1.0.0
public @Nullable Palette.Swatch getDarkVibrantSwatch()

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

See also
DARK_VIBRANT

getDominantColor

Added in 1.0.0
public @ColorInt int getDominantColor(@ColorInt int defaultColor)

Returns the color of the dominant swatch from the palette, as an RGB packed int.

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

getDominantSwatch

Added in 1.0.0
public @Nullable Palette.Swatch getDominantSwatch()

Returns the dominant swatch from the palette.

The dominant swatch is defined as the swatch with the greatest population (frequency) within the palette.

getLightMutedColor

Added in 1.0.0
public @ColorInt int getLightMutedColor(@ColorInt int defaultColor)

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

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

getLightMutedSwatch

Added in 1.0.0
public @Nullable Palette.Swatch getLightMutedSwatch()

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

See also
LIGHT_MUTED

getLightVibrantColor

Added in 1.0.0
public @ColorInt int getLightVibrantColor(@ColorInt int defaultColor)

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

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

getLightVibrantSwatch

Added in 1.0.0
public @Nullable Palette.Swatch getLightVibrantSwatch()

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

See also
LIGHT_VIBRANT

getMutedColor

Added in 1.0.0
public @ColorInt int getMutedColor(@ColorInt int defaultColor)

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

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

See also
getMutedSwatch

getMutedSwatch

Added in 1.0.0
public @Nullable Palette.Swatch getMutedSwatch()

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

See also
MUTED

getSwatchForTarget

Added in 1.0.0
public @Nullable Palette.Swatch getSwatchForTarget(@NonNull Target target)

Returns the selected swatch for the given target from the palette, or null if one could not be found.

getSwatches

Added in 1.0.0
public @NonNull List<Palette.SwatchgetSwatches()

Returns all of the swatches which make up the palette.

getTargets

Added in 1.0.0
public @NonNull List<TargetgetTargets()

Returns the targets used to generate this palette.

getVibrantColor

Added in 1.0.0
public @ColorInt int getVibrantColor(@ColorInt int defaultColor)

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

Parameters
@ColorInt int defaultColor

value to return if the swatch isn't available

See also
getVibrantSwatch

getVibrantSwatch

Added in 1.0.0
public @Nullable Palette.Swatch getVibrantSwatch()

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

See also
VIBRANT

Extension functions

PaletteKt.get

public final Palette.Swatch PaletteKt.get(@NonNull Palette receiver, @NonNull Target target)

Returns the selected swatch for the given target from the palette, or null if one could not be found.