Skip to content

Instantly share code, notes, and snippets.

@animsh
Forked from KKorvin/PickDominantColor.java
Created December 19, 2020 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save animsh/09d5a586cd2e2c0529631cae13c8821b to your computer and use it in GitHub Desktop.
Save animsh/09d5a586cd2e2c0529631cae13c8821b to your computer and use it in GitHub Desktop.
How to pick dominant color from image on Android. Palette Google library used.
// Don't forget to add Palette library to your build.gradle
// compile 'com.android.support:palette-v7:+'
public static int getDominantColor(Bitmap bitmap) {
List<Palette.Swatch> swatchesTemp = Palette.from(bitmap).generate().getSwatches();
List<Palette.Swatch> swatches = new ArrayList<Palette.Swatch>(swatchesTemp);
Collections.sort(swatches, new Comparator<Palette.Swatch>() {
@Override
public int compare(Palette.Swatch swatch1, Palette.Swatch swatch2) {
return swatch2.getPopulation() - swatch1.getPopulation();
}
});
return swatches.size() > 0 ? swatches.get(0).getRgb() : getRandomColor();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment