Google 致力于为黑人社区推动种族平等。查看具体举措

Palette

class Palette
kotlin.Any
   ↳ androidx.palette.graphics.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 classes

Builder class for generating Palette instances.

abstract

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

abstract

Listener to be used with generateAsync(Bitmap, PaletteAsyncListener) or generateAsync(Bitmap, int, PaletteAsyncListener)

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

Public methods
static Palette.Builder
from(@NonNull bitmap: Bitmap)

Start generating a Palette with the returned Builder instance.

static Palette
from(@NonNull swatches: MutableList<Palette.Swatch!>)

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

static Palette!
generate(bitmap: Bitmap!)

static Palette!
generate(bitmap: Bitmap!, numColors: Int)

static AsyncTask<Bitmap!, Void!, Palette!>!

static AsyncTask<Bitmap!, Void!, Palette!>!
generateAsync(bitmap: Bitmap!, numColors: Int, listener: Palette.PaletteAsyncListener!)

Int
getColorForTarget(@NonNull target: Target, @ColorInt defaultColor: Int)

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

Int
getDarkMutedColor(@ColorInt defaultColor: Int)

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

Palette.Swatch?

Returns a muted and dark swatch from the palette.

Int
getDarkVibrantColor(@ColorInt defaultColor: Int)

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

Palette.Swatch?

Returns a dark and vibrant swatch from the palette.

Int
getDominantColor(@ColorInt defaultColor: Int)

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

Palette.Swatch?

Returns the dominant swatch from the palette.

Int
getLightMutedColor(@ColorInt defaultColor: Int)

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

Palette.Swatch?

Returns a muted and light swatch from the palette.

Int
getLightVibrantColor(@ColorInt defaultColor: Int)

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

Palette.Swatch?

Returns a light and vibrant swatch from the palette.

Int
getMutedColor(@ColorInt defaultColor: Int)

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

Palette.Swatch?

Returns a muted swatch from the palette.

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.

MutableList<Palette.Swatch!>

Returns all of the swatches which make up the palette.

MutableList<Target!>

Returns the targets used to generate this palette.

Int
getVibrantColor(@ColorInt defaultColor: Int)

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

Palette.Swatch?

Returns the most vibrant swatch in the palette.

Extension functions
From androidx.palette.graphics
operator Palette.Swatch?
Palette.get(target: Target)

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

Public methods

from

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

Start generating a Palette with the returned Builder instance.

from

@NonNull static fun from(@NonNull swatches: MutableList<Palette.Swatch!>): Palette

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

static fun generate(bitmap: Bitmap!): Palette!

Deprecated: Use Builder to generate the Palette.

generate

static fun generate(
    bitmap: Bitmap!,
    numColors: Int
): Palette!

Deprecated: Use Builder to generate the Palette.

generateAsync

static fun generateAsync(
    bitmap: Bitmap!,
    listener: Palette.PaletteAsyncListener!
): AsyncTask<Bitmap!, Void!, Palette!>!

Deprecated: Use Builder to generate the Palette.

generateAsync

static fun generateAsync(
    bitmap: Bitmap!,
    numColors: Int,
    listener: Palette.PaletteAsyncListener!
): AsyncTask<Bitmap!, Void!, Palette!>!

Deprecated: Use Builder to generate the Palette.

getColorForTarget

@ColorInt fun getColorForTarget(
    @NonNull target: Target,
    @ColorInt defaultColor: Int
): Int

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

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

getDarkMutedColor

@ColorInt fun getDarkMutedColor(@ColorInt defaultColor: Int): Int

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

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

getDarkMutedSwatch

@Nullable fun getDarkMutedSwatch(): Palette.Swatch?

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

getDarkVibrantColor

@ColorInt fun getDarkVibrantColor(@ColorInt defaultColor: Int): Int

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

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

getDarkVibrantSwatch

@Nullable fun getDarkVibrantSwatch(): Palette.Swatch?

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

getDominantColor

@ColorInt fun getDominantColor(@ColorInt defaultColor: Int): Int

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

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

getDominantSwatch

@Nullable fun getDominantSwatch(): Palette.Swatch?

Returns the dominant swatch from the palette.

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

getLightMutedColor

@ColorInt fun getLightMutedColor(@ColorInt defaultColor: Int): Int

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

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

getLightMutedSwatch

@Nullable fun getLightMutedSwatch(): Palette.Swatch?

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

getLightVibrantColor

@ColorInt fun getLightVibrantColor(@ColorInt defaultColor: Int): Int

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

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

getLightVibrantSwatch

@Nullable fun getLightVibrantSwatch(): Palette.Swatch?

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

getMutedColor

@ColorInt fun getMutedColor(@ColorInt defaultColor: Int): Int

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

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

getMutedSwatch

@Nullable fun getMutedSwatch(): Palette.Swatch?

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

See Also

getSwatchForTarget

@Nullable fun getSwatchForTarget(@NonNull target: Target): Palette.Swatch?

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

getSwatches

@NonNull fun getSwatches(): MutableList<Palette.Swatch!>

Returns all of the swatches which make up the palette.

getTargets

@NonNull fun getTargets(): MutableList<Target!>

Returns the targets used to generate this palette.

getVibrantColor

@ColorInt fun getVibrantColor(@ColorInt defaultColor: Int): Int

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

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

getVibrantSwatch

@Nullable fun getVibrantSwatch(): Palette.Swatch?

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

See Also