58

You can understand why I'm trying to find the dominant color in an image if you use Windows 7. When your mouse over a program in the taskbar, the background of that particular program changes based on the most dominant color in the icon. I have noticed this technique used in other programs as well, but can't remember them off the top of my head.

I can see this being helpful in a number of UI techniques that I'm using to develop an application, and I was wondering how finding the most common color would be achieved from an Android drawable resource.

Tyler
  • 19,113
  • 19
  • 94
  • 151
  • A new API was added with Lollipop that helps extract prominent colors from a Bitmap. See [my answer below](http://stackoverflow.com/a/28145358/1956632) for details. Since the Palette class mentioned is in the support7 library, it should work in older versions of Android as well. – Tony Wickham Jan 26 '15 at 06:01
  • android v7 palette support lib does that for us.Anyone looking for demo http://code2concept.blogspot.in/2015/10/android-support-v7-palette-demo.html – Nitesh Tiwari Oct 23 '15 at 07:57
  • oops, blog does not exist anymore :c – Ezequiel Adrian Dec 07 '20 at 14:32
  • I know that some Android launchers can sort apps by the color of their icons, That's another example. – Omar Hemaia Nov 24 '22 at 10:25

8 Answers8

99

In Android 5.0 Lollipop, a class was added to help extract useful colors from a Bitmap. The Palette class, found in android.support.v7.graphics, can extract the following colors:

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

This Android training page gives all the details you need to use the class (I tried it myself in Android Studio and it was very straightforward): http://developer.android.com/training/material/drawables.html#ColorExtract

To quote:

The Android Support Library r21 and above includes the Palette class, which lets you extract prominent colors from an image. To extract these colors, pass a Bitmap object to the Palette.generate() static method in the background thread where you load your images. If you can't use that thread, call the Palette.generateAsync() method and provide a listener instead.*

You can retrieve the prominent colors from the image using the getter methods in the Palette class, such as Palette.getVibrantColor.

To use the Palette class in your project, add the following Gradle dependency to your app's module:

dependencies {
    ...
    implementation 'com.android.support:palette-v7:21.0.+'
}

Or if you're using androidx:

implementation 'androidx.palette:palette:1.0.0'

If you need to use generateAsync(), here's how:

Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
    public void onGenerated(Palette palette) {
        // Do something with colors...
    }
});

EDIT: Since the question asks how to extract colors from a drawable resource, you'd first have to convert the drawable to a bitmap to use the technique I've described. Luckily, that is quite simple using BitmapFactory:

Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                                       R.drawable.icon_resource);`
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
Tony Wickham
  • 4,706
  • 3
  • 29
  • 35
  • If you want to experiment with what colors the Palette class extracts, check out my app on the Play Store: https://play.google.com/store/apps/details?id=com.tonyw.sampleapps.palettecolorextraction. You can find the source code for it on GitHub: https://github.com/tony-w/PaletteColorExtraction – Tony Wickham Mar 01 '15 at 07:26
  • 2
    Excellent answer. This makes most of the available libraries and methods redundant. – Baz Mar 20 '15 at 10:14
  • 3
    Palette.generateAsync method has been deprecated. Use Palette.from(bitmap).generate(paletteListener) instead – A.J. May 11 '17 at 21:12
  • 1
    @TonyWickham The `generateAsync` it is now deprecated. Do you have any idea how to get the color of the favicon from url ? – TheCoderGuy May 25 '19 at 18:13
  • 3
    @Spritzig use from: `Palette.from(bitmap).generate(new PaletteAsyncListener() { public void onGenerated(Palette p) { // Use generated instance } });` – Ken Nichols Jun 23 '19 at 20:34
47

There is also an another solution, it's more approximative but if you don't want to have long delay for searching color, it can do the job.

public static int getDominantColor(Bitmap bitmap) {
    Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
    final int color = newBitmap.getPixel(0, 0);
    newBitmap.recycle();
    return color;
}
Matt Raines
  • 4,149
  • 8
  • 31
  • 34
Pdroid
  • 867
  • 9
  • 13
14

To find the Dominant / Vibrant / Muted color from an image, use Palette:

import:

implementation 'androidx.palette:palette:1.0.0'

usage:

    val bitmap = BitmapFactory.decodeResource(resources, R.drawable.image)

    Palette.Builder(bitmap).generate { it?.let {  palette ->
        val dominantColor = palette.getDominantColor(ContextCompat.getColor(context!!, R.color.defaultColor))

        // TODO: use dominant color

    } }
Francis
  • 6,788
  • 5
  • 47
  • 64
9

This class iterates through a Bitmap and returns the most dominate colour. Feel free to clean up the code where necessary.

public class ImageColour {

String colour;


public ImageColour(Bitmap image) throws Exception {

     int height = image.getHeight();
     int width = image.getWidth();

     Map m = new HashMap();

        for(int i=0; i < width ; i++){

            for(int j=0; j < height ; j++){

                int rgb = image.getPixel(i, j);
                int[] rgbArr = getRGBArr(rgb);                

                if (!isGray(rgbArr)) {   

                        Integer counter = (Integer) m.get(rgb);   
                        if (counter == null)
                            counter = 0;
                        counter++;                                
                        m.put(rgb, counter);       

                }                
            }
        }        

        String colourHex = getMostCommonColour(m);
    }



    public static String getMostCommonColour(Map map) {

        List list = new LinkedList(map.entrySet());
        Collections.sort(list, new Comparator() {
              public int compare(Object o1, Object o2) {

                return ((Comparable) ((Map.Entry) (o1)).getValue())
                  .compareTo(((Map.Entry) (o2)).getValue());

              }

        });    

        Map.Entry me = (Map.Entry )list.get(list.size()-1);
        int[] rgb= getRGBArr((Integer)me.getKey());

        return Integer.toHexString(rgb[0])+" "+Integer.toHexString(rgb[1])+" "+Integer.toHexString(rgb[2]);        
    }    


    public static int[] getRGBArr(int pixel) {

        int red = (pixel >> 16) & 0xff;
        int green = (pixel >> 8) & 0xff;
        int blue = (pixel) & 0xff;

        return new int[]{red,green,blue};

  }

    public static boolean isGray(int[] rgbArr) {

        int rgDiff = rgbArr[0] - rgbArr[1];
        int rbDiff = rgbArr[0] - rgbArr[2];

        int tolerance = 10;

        if (rgDiff > tolerance || rgDiff < -tolerance) 
            if (rbDiff > tolerance || rbDiff < -tolerance) { 

                return false;

            }                

        return true;
    }


public String returnColour() {

    if (colour.length() == 6) {
        return colour.replaceAll("\\s", "");
    } else {
        return "ffffff";
    }
}

to get the hex simply call returnColour();

  • I suggest to whoever uses this approach to play with the tolerance variable. Depending on the value you set it the algorithm runs faster or slower. – Thiago M Rocha Aug 19 '14 at 14:36
8

Add to dependencies

implementation 'androidx.palette:palette:1.0.0'

and..

 AppCompatImageView imageView = findViewById(R.id.image_view);

 Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
 Palette.from(bitmap).generate(palette -> {
      int vibrant = palette.getVibrantColor(0x000000); // <=== color you want
      int vibrantLight = palette.getLightVibrantColor(0x000000);
      int vibrantDark = palette.getDarkVibrantColor(0x000000);
      int muted = palette.getMutedColor(0x000000);
      int mutedLight = palette.getLightMutedColor(0x000000);
      int mutedDark = palette.getDarkMutedColor(0x000000);
 });
Williaan Lopes
  • 1,177
  • 15
  • 11
  • `java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable` – Dr.jacky Sep 08 '22 at 20:46
4

I wrote my own methods to get dominant color:

Method 1 (My technique)

  1. Reduce to ARGB_4444 color space
  2. Compute the maximum occurrence of individual RGB elements and obtaining 3 distinctive maximum values
  3. Combining maximum values to dominant RGB color

    public int getDominantColor1(Bitmap bitmap) {
    
    if (bitmap == null)
        throw new NullPointerException();
    
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int size = width * height;
    int pixels[] = new int[size];
    
    Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false);
    
    bitmap2.getPixels(pixels, 0, width, 0, 0, width, height);
    
    final List<HashMap<Integer, Integer>> colorMap = new ArrayList<HashMap<Integer, Integer>>();
    colorMap.add(new HashMap<Integer, Integer>());
    colorMap.add(new HashMap<Integer, Integer>());
    colorMap.add(new HashMap<Integer, Integer>());
    
    int color = 0;
    int r = 0;
    int g = 0;
    int b = 0;
    Integer rC, gC, bC;
    for (int i = 0; i < pixels.length; i++) {
        color = pixels[i];
    
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
    
        rC = colorMap.get(0).get(r);
        if (rC == null)
            rC = 0;
        colorMap.get(0).put(r, ++rC);
    
        gC = colorMap.get(1).get(g);
        if (gC == null)
            gC = 0;
        colorMap.get(1).put(g, ++gC);
    
        bC = colorMap.get(2).get(b);
        if (bC == null)
            bC = 0;
        colorMap.get(2).put(b, ++bC);
    }
    
    int[] rgb = new int[3];
    for (int i = 0; i < 3; i++) {
        int max = 0;
        int val = 0;
        for (Map.Entry<Integer, Integer> entry : colorMap.get(i).entrySet()) {
            if (entry.getValue() > max) {
                max = entry.getValue();
                val = entry.getKey();
            }
        }
        rgb[i] = val;
    }
    
    int dominantColor = Color.rgb(rgb[0], rgb[1], rgb[2]);
    
    return dominantColor;
     }
    

Method 2 (Old technique)

  1. Reduce to ARGB_4444 color space
  2. Compute the occurrence of each color and finding the maximum one as dominant color

    public int getDominantColor2(Bitmap bitmap) {
    if (bitmap == null)
        throw new NullPointerException();
    
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int size = width * height;
    int pixels[] = new int[size];
    
    Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false);
    
    bitmap2.getPixels(pixels, 0, width, 0, 0, width, height);
    
    HashMap<Integer, Integer> colorMap = new HashMap<Integer, Integer>();
    
    int color = 0;
    Integer count = 0;
    for (int i = 0; i < pixels.length; i++) {
        color = pixels[i];
        count = colorMap.get(color);
        if (count == null)
            count = 0;
        colorMap.put(color, ++count);
    }
    
    int dominantColor = 0;
    int max = 0;
    for (Map.Entry<Integer, Integer> entry : colorMap.entrySet()) {
        if (entry.getValue() > max) {
            max = entry.getValue();
            dominantColor = entry.getKey();
        }
    }
    return dominantColor;
    }
    
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90
2

Loop through all the pixel's color data and average the color values, ignore anything that is a shade of grey or transparent. I believe that is what Microsoft does in Windows 7 based on a recent blog post.

edit
The blog post: http://blogs.msdn.com/b/oldnewthing/archive/2011/12/06/10244432.aspx

This link showing how Chrome picks the dominant color may also be helpful. http://www.quora.com/Google-Chrome/How-does-Chrome-pick-the-color-for-the-stripes-on-the-Most-visited-page-thumbnails

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • I was hoping there would be an API function buried deep somewhere. This is good info – Tyler Jan 09 '12 at 16:44
  • 3
    I found a simple hack: copy as a 1x1 bitmap and get the color: http://aerilys.fr/blog/?p=1341 – radley Sep 27 '14 at 16:54
0

None of the other answers did the job for me, and I didn't rule out the cause of the problem.

This is what I ended up using:

public static int getDominantColor(Bitmap bitmap) {
    if (bitmap == null) {
        return Color.TRANSPARENT;
    }
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int size = width * height;
    int pixels[] = new int[size];
    //Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_4444, false);
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    int color;
    int r = 0;
    int g = 0;
    int b = 0;
    int a;
    int count = 0;
    for (int i = 0; i < pixels.length; i++) {
        color = pixels[i];
        a = Color.alpha(color);
        if (a > 0) {
            r += Color.red(color);
            g += Color.green(color);
            b += Color.blue(color);
            count++;
        }
    }
    r /= count;
    g /= count;
    b /= count;
    r = (r << 16) & 0x00FF0000;
    g = (g << 8) & 0x0000FF00;
    b = b & 0x000000FF;
    color = 0xFF000000 | r | g | b;
    return color;
}
rraallvv
  • 2,875
  • 6
  • 30
  • 67