Last active
August 3, 2021 16:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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