# Palette.Builder

Artifact: [androidx.palette:palette](https://developer.android.com/jetpack/androidx/releases/palette) [View Source](https://cs.android.com/search?q=file:androidx/palette/graphics/Palette.java+class:androidx.palette.graphics.Palette.Builder) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

*** ** * ** ***


```
public final class Palette.Builder
```

<br />

*** ** * ** ***

Builder class for generating `https://developer.android.com/reference/androidx/palette/graphics/Palette` instances.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#Builder(android.graphics.Bitmap)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/graphics/Bitmap.html bitmap)` Construct a new `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` using a source `https://developer.android.com/reference/android/graphics/Bitmap.html` |
| `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#Builder(java.util.List<androidx.palette.graphics.Palette.Swatch>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/palette/graphics/Palette.Swatch> swatches)` Construct a new `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` using a list of `https://developer.android.com/reference/androidx/palette/graphics/Palette.Swatch` instances. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#addFilter(androidx.palette.graphics.Palette.Filter)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Filter filter)` Add a filter to be able to have fine grained control over which colors are allowed in the resulting palette. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#addTarget(androidx.palette.graphics.Target)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Target target)` Add a target profile to be generated in the palette. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#clearFilters()()` Clear all added filters. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#clearRegion()()` Clear any previously region set via `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#setRegion(int,int,int,int)`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#clearTargets()()` Clear all added targets. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#generate()()` Generate and return the `https://developer.android.com/reference/androidx/palette/graphics/Palette` synchronously. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/AsyncTask.html<https://developer.android.com/reference/android/graphics/Bitmap.html, https://developer.android.com/reference/java/lang/Void.html, https://developer.android.com/reference/androidx/palette/graphics/Palette>` | `[generate](https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#generate(androidx.palette.graphics.Palette.PaletteAsyncListener))(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.PaletteAsyncListener listener)` **This method is deprecated.** Use the standard `java.util.concurrent` or [Kotlin concurrency utilities](https://developer.android.com/topic/libraries/architecture/coroutines) to call `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#generate()` instead. <br /> |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#maximumColorCount(int)(int colors)` Set the maximum number of colors to use in the quantization step when using a `https://developer.android.com/reference/android/graphics/Bitmap.html` as the source. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#resizeBitmapArea(int)(int area)` Set the resize value when using a `https://developer.android.com/reference/android/graphics/Bitmap.html` as the source. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `[resizeBitmapSize](https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#resizeBitmapSize(int))(int maxDimension)` **This method is deprecated.** Using `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#resizeBitmapArea(int)` is preferred since it can handle abnormal aspect ratios more gracefully. <br /> |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` | `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#setRegion(int,int,int,int)(@https://developer.android.com/reference/androidx/annotation/Px int left, @https://developer.android.com/reference/androidx/annotation/Px int top, @https://developer.android.com/reference/androidx/annotation/Px int right, @https://developer.android.com/reference/androidx/annotation/Px int bottom)` Set a region of the bitmap to be used exclusively when calculating the palette. |

## Public constructors

### Builder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public Builder(@NonNull Bitmap bitmap)
```

Construct a new `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` using a source `https://developer.android.com/reference/android/graphics/Bitmap.html`

### Builder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public Builder(@NonNull List<Palette.Swatch> swatches)
```

Construct a new `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder` using a list of `https://developer.android.com/reference/androidx/palette/graphics/Palette.Swatch` instances. Typically only used for testing.

## Public methods

### addFilter

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder addFilter(@NonNull Palette.Filter filter)
```

Add a filter to be able to have fine grained control over which colors are allowed in the resulting palette.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/palette/graphics/Palette.Filter filter` | filter to add. |

### addTarget

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder addTarget(@NonNull Target target)
```

Add a target profile to be generated in the palette.

You can retrieve the result via `https://developer.android.com/reference/androidx/palette/graphics/Palette#getSwatchForTarget(androidx.palette.graphics.Target)`.

### clearFilters

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder clearFilters()
```

Clear all added filters. This includes any default filters added automatically by `https://developer.android.com/reference/androidx/palette/graphics/Palette`.

### clearRegion

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder clearRegion()
```

Clear any previously region set via `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#setRegion(int,int,int,int)`.

### clearTargets

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder clearTargets()
```

Clear all added targets. This includes any default targets added automatically by `https://developer.android.com/reference/androidx/palette/graphics/Palette`.

### generate

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette generate()
```

Generate and return the `https://developer.android.com/reference/androidx/palette/graphics/Palette` synchronously.

### generate

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/palette#1.1.0)

```
public @NonNull AsyncTask<Bitmap, Void, Palette> [generate](https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#generate(androidx.palette.graphics.Palette.PaletteAsyncListener))(@NonNull Palette.PaletteAsyncListener listener)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Use the standard `java.util.concurrent` or [Kotlin concurrency utilities](https://developer.android.com/topic/libraries/architecture/coroutines) to call `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#generate()` instead.

Generate the `https://developer.android.com/reference/androidx/palette/graphics/Palette` asynchronously. The provided listener's `https://developer.android.com/reference/androidx/palette/graphics/Palette.PaletteAsyncListener#onGenerated(androidx.palette.graphics.Palette)` method will be called with the palette when generated.

### maximumColorCount

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder maximumColorCount(int colors)
```

Set the maximum number of colors to use in the quantization step when using a `https://developer.android.com/reference/android/graphics/Bitmap.html` as the source.

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.

### resizeBitmapArea

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder resizeBitmapArea(int area)
```

Set the resize value when using a `https://developer.android.com/reference/android/graphics/Bitmap.html` as the source. If the bitmap's area is greater than the value specified, then the bitmap will be resized so that its area matches `area`. 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.

| Parameters |
|---|---|
| `int area` | the number of pixels that the intermediary scaled down Bitmap should cover, or any value \<= 0 to disable resizing. |

### resizeBitmapSize

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder [resizeBitmapSize](https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#resizeBitmapSize(int))(int maxDimension)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Using `https://developer.android.com/reference/androidx/palette/graphics/Palette.Builder#resizeBitmapArea(int)` is preferred since it can handle abnormal aspect ratios more gracefully.

Set the resize value when using a `https://developer.android.com/reference/android/graphics/Bitmap.html` as the source. If the bitmap's largest dimension is greater than the value specified, then the bitmap will be resized so that its largest dimension matches `maxDimension`. If the bitmap is smaller or equal, the original is used as-is.

| Parameters |
|---|---|
| `int maxDimension` | the number of pixels that the max dimension should be scaled down to, or any value \<= 0 to disable resizing. |

### setRegion

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/palette#1.0.0)

```
public @NonNull Palette.Builder setRegion(@Px int left, @Px int top, @Px int right, @Px int bottom)
```

Set a region of the bitmap to be used exclusively when calculating the palette.

This only works when the original input is a `https://developer.android.com/reference/android/graphics/Bitmap.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Px int left` | The left side of the rectangle used for the region. |
| `@https://developer.android.com/reference/androidx/annotation/Px int top` | The top of the rectangle used for the region. |
| `@https://developer.android.com/reference/androidx/annotation/Px int right` | The right side of the rectangle used for the region. |
| `@https://developer.android.com/reference/androidx/annotation/Px int bottom` | The bottom of the rectangle used for the region. |