Extracting Colors from an Image Using the Palette API Android

Ngenge Senior
Kt. Academy
Published in
3 min readNov 2, 2022

--

Photo by Lucas Kapla on Unsplash

Extracting colors from an image is not new but is used extensively in theming apps if you have noticed some media players change their theme(toolbar, toolbar title text color, status bar color, and other properties) based on the artwork of the current playing media item. How is this done or how can this be done?

Create Android Project and Add Dependency

For this, we will start by opening Android Studio IDE and creating a new Android project that extends from Material2 or Material3. The next thing to do is to add the palette API dependency in your app-level build.gradle file as seen below;

implementation "androidx.palette:palette-ktx:1.0.0"

The next thing for us to do is to add 6 toolbars in our activity_main.xml file and a FAB that will be used in picking an image from the user’s gallery. Below is the code for activity_main.xml.

The file activity_main.xml has 6 MaterialToolbars, a FAB, and an ImageView.When the FAB will be clicked, we will select an image from the gallery and set the ImageView’s bitmap using the selected image and we will go further to create what is known as a palette from the bitmap. We will use the palette’s different swatches to set the background colors of the various MaterialToolbars.

Swatches

A swatch represents a color swatch generated from an image’s palette. A swatch has three important properties which we are interested the RGB value of the swatch, the title text color, and the body text color. These three properties have getter methods that we will use.

There are six color profiles(swatches) that we are interested in and they include vibrant, light vibrant, dark vibrant, muted, light muted, and dark muted swatches. Each of our toolbar’s backgrounds will be set to a particular color, the title will be the name plus the hex value of the color of the background and the title color will be the title color of the swatch.

--

--