Extract colors from image(bitmap) using Palette in Android

When open the profile page of any person or group in Whatsapp, you will find that the color of the toolbar at top uses the color from the DP of the user or group. It looks good when you use extracted colors from image in your layout. Your layout will blend with the image and other content.

So here we can extract different colors from any image(bitmap) used in our application and use those colors effectively in our app throughout.

Main Color Profiles

First of all there are main 6 type of color profiles in the image that we can extract.

  • Vibrant
  • Vibrant Dark
  • Vibrant Light
  • Muted
  • Muted Dark
  • Muted Light

Most used profile from above are Vibrant and Vibrant Dark. The reason is, vibrant colors are pure, bright, intense, saturated and high chroma color. They stand out in the image and easy to point out. Now if we use those colors in the layout along with the image, it will give a great look. Whatsapp is the best example of it as we discussed earlier.

Every profile(swatch) gives us following information that we can use in the application.

  • population of that color in image
  • hue, saturation, lightness
  • body text color with sufficient contrast with the main color
  • title text color with sufficient contrast with the main color

Now we look at the code to get the Palette from bitmap.

Bitmap b = null;
b = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
Palette p = Palette.from(b).generate();

Here we get the Palette from image(bitmap). Now we extract main 6 different kind of color profile(swatch) from it.

Palette.Swatch psVibrant = p.getVibrantSwatch();
Palette.Swatch psVibrantLight = p.getLightVibrantSwatch();
Palette.Swatch psVibrantDark = p.getDarkVibrantSwatch();
Palette.Swatch psMuted = p.getMutedSwatch();
Palette.Swatch psMutedLight = p.getLightMutedSwatch();
Palette.Swatch psMutedDark = p.getDarkMutedSwatch();

Now we will extract 4 different values from these swatch as described earlier in the article. Here we will take psVibrant as an example.

int color = psVibrant.getRgb();
int population = psVibrant.getPopulation();
float[] hsl = psVibrant.getHsl();
int bodyTextColor = psVibrant.getBodyTextColor();
int titleTextColor = psVibrant.getTitleTextColor();

You can also get other colors from image other than above 6 by following code snippet.

List<Palette.Swatch> pss;
pss = p.getSwatches();
for(int j = 0; j < pss.size(); j++) {
  Palette.Swatch ps = pss.get(j);
  int color = ps.getRgb();
  int population = ps.getPopulation();
  float[] hsl = ps.getHsl();
  int bodyTextColor = ps.getBodyTextColor();
  int titleTextColor = ps.getTitleTextColor();
Other Color Profiles

9 thoughts on “Extract colors from image(bitmap) using Palette in Android

  1. Nisa95 December 9, 2017 / 4:35 pm

    Do you have this full source code .. ? I want to know the android manifest and also the layout.xml ? if you can share your code … im still new in android 🙂

    • dakshbhatt April 18, 2018 / 6:30 am

      It is very simple, try it out yourself!

  2. Mürsel May 2, 2018 / 1:18 am

    how can download this application ?

    • dakshbhatt May 2, 2018 / 2:00 am

      You can create new project in Android Studio and copy paste this code.

  3. shanmukh kask May 23, 2018 / 11:52 am

    I have a doubt.
    For example if any image does not contain any of the swatch. If we apply that color to our background the application unfortunately stopped. Then how to best color based that image and apply to background automatically.

    • dakshbhatt May 30, 2018 / 6:41 am

      yes it will crash the app, you have to give some pre-defined color in case the app crash or you can use some random color. but you probably find at least 1 or 2 swatchs

  4. shanmukh kask May 23, 2018 / 11:54 am

    Is it possible to use if() condition to Palette for best color picked up.

  5. vinay September 23, 2020 / 6:59 pm

    How to get this hashcode like #f6c500

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s