Skip to content

Instantly share code, notes, and snippets.

@KKorvin
Last active August 3, 2021 16:07
Show Gist options
  • Save KKorvin/219555d4d3ee1828d7b0e808aad82930 to your computer and use it in GitHub Desktop.
Save KKorvin/219555d4d3ee1828d7b0e808aad82930 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