PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/util/ColorUtil.java

58 lines
1.8 KiB
Java
Raw Normal View History

2020-08-31 12:30:07 +00:00
package code.name.monkey.retromusic.util;
import android.graphics.Bitmap;
2020-08-31 12:30:07 +00:00
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.palette.graphics.Palette;
2020-08-31 12:30:07 +00:00
import java.util.Collections;
import java.util.Comparator;
public class ColorUtil {
2020-10-06 08:46:04 +00:00
@Nullable
public static Palette generatePalette(Bitmap bitmap) {
if (bitmap == null) return null;
return Palette.from(bitmap).generate();
}
@ColorInt
public static int getColor(@Nullable Palette palette, int fallback) {
if (palette != null) {
if (palette.getVibrantSwatch() != null) {
return palette.getVibrantSwatch().getRgb();
} else if (palette.getMutedSwatch() != null) {
return palette.getMutedSwatch().getRgb();
} else if (palette.getDarkVibrantSwatch() != null) {
return palette.getDarkVibrantSwatch().getRgb();
} else if (palette.getDarkMutedSwatch() != null) {
return palette.getDarkMutedSwatch().getRgb();
} else if (palette.getLightVibrantSwatch() != null) {
return palette.getLightVibrantSwatch().getRgb();
} else if (palette.getLightMutedSwatch() != null) {
return palette.getLightMutedSwatch().getRgb();
} else if (!palette.getSwatches().isEmpty()) {
return Collections.max(palette.getSwatches(), SwatchComparator.getInstance()).getRgb();
}
2020-08-31 12:30:07 +00:00
}
2020-10-06 08:46:04 +00:00
return fallback;
}
2020-08-31 12:30:07 +00:00
2020-10-06 08:46:04 +00:00
private static class SwatchComparator implements Comparator<Palette.Swatch> {
private static SwatchComparator sInstance;
2020-08-31 12:30:07 +00:00
2020-10-06 08:46:04 +00:00
static SwatchComparator getInstance() {
if (sInstance == null) {
sInstance = new SwatchComparator();
}
return sInstance;
2020-08-31 12:30:07 +00:00
}
2020-10-06 08:46:04 +00:00
@Override
public int compare(Palette.Swatch lhs, Palette.Swatch rhs) {
return lhs.getPopulation() - rhs.getPopulation();
}
}
}