Search code examples
androidandroid-music-player

How to extract the most prominent colour from an image?


I am planning to develop a music player app and I want to make the ui such that the buttons change colour based on the most prominent colour in the album art of the album that is being viewed and in other cases either a preselected colour or the prominent colour of the album of the song that is currently being played.

I would like to know if anybody knows of a method to extract the most prominent colour from an image.


Solution

  • There is a nice API which allows you to easily do this called palette. It allows you to get a selection of colors from a Bitmap that you provide, like so:

    Palette palette = Palette.generate(myBitmap);
    int vibrant = palette.getVibrantColor(0x000000);
    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);
    

    The dependency is 'com.android.support:palette-v7:21.0.0'

    I think this will perfectly suite your needs. Click here for a full guide on how to implement it.