Skip to content

Instantly share code, notes, and snippets.

@vzool
Forked from KKorvin/PickDominantColor.java
Created March 22, 2018 11:34
Show Gist options
  • Save vzool/da1fd53579f9db20cd675856508b9c1c to your computer and use it in GitHub Desktop.
Save vzool/da1fd53579f9db20cd675856508b9c1c 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