2019-03-03 09:29:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Hemanth Savarala.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
package code.name.monkey.retromusic.util;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.Color;
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2018-08-30 09:29:30 +00:00
|
|
|
import androidx.annotation.ColorInt;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2021-10-05 06:36:57 +00:00
|
|
|
import androidx.core.content.ContextCompat;
|
2018-08-30 09:29:30 +00:00
|
|
|
import androidx.palette.graphics.Palette;
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2021-10-05 06:36:57 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2021-10-05 06:36:57 +00:00
|
|
|
import code.name.monkey.appthemehelper.ThemeStore;
|
2021-09-08 18:30:20 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.ColorUtil;
|
2021-10-05 06:36:57 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.VersionUtils;
|
|
|
|
import code.name.monkey.retromusic.R;
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2018-07-27 13:07:33 +00:00
|
|
|
public class RetroColorUtil {
|
2020-10-06 08:46:04 +00:00
|
|
|
public static int desaturateColor(int color, float ratio) {
|
|
|
|
float[] hsv = new float[3];
|
|
|
|
Color.colorToHSV(color, hsv);
|
2019-10-10 18:02:59 +00:00
|
|
|
|
2021-11-28 16:55:18 +00:00
|
|
|
hsv[1] = (hsv[1] * ratio) + (0.2f * (1.0f - ratio));
|
2019-10-10 18:02:59 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
return Color.HSVToColor(hsv);
|
|
|
|
}
|
2019-12-01 15:27:01 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@Nullable
|
|
|
|
public static Palette generatePalette(@Nullable Bitmap bitmap) {
|
|
|
|
return bitmap == null ? null : Palette.from(bitmap).clearFilters().generate();
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public static int getTextColor(@Nullable Palette palette) {
|
|
|
|
if (palette == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
int inverse = -1;
|
|
|
|
if (palette.getVibrantSwatch() != null) {
|
|
|
|
inverse = palette.getVibrantSwatch().getRgb();
|
|
|
|
} else if (palette.getLightVibrantSwatch() != null) {
|
|
|
|
inverse = palette.getLightVibrantSwatch().getRgb();
|
|
|
|
} else if (palette.getDarkVibrantSwatch() != null) {
|
|
|
|
inverse = palette.getDarkVibrantSwatch().getRgb();
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
int background = getSwatch(palette).getRgb();
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
if (inverse != -1) {
|
|
|
|
return ColorUtil.INSTANCE.getReadableText(inverse, background, 150);
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
return ColorUtil.INSTANCE.stripAlpha(getSwatch(palette).getTitleTextColor());
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@NonNull
|
|
|
|
public static Palette.Swatch getSwatch(@Nullable Palette palette) {
|
|
|
|
if (palette == null) {
|
|
|
|
return new Palette.Swatch(Color.WHITE, 1);
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
return getBestPaletteSwatchFrom(palette.getSwatches());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getMatColor(Context context, String typeColor) {
|
|
|
|
int returnColor = Color.BLACK;
|
|
|
|
int arrayId =
|
|
|
|
context
|
|
|
|
.getResources()
|
|
|
|
.getIdentifier(
|
|
|
|
"md_" + typeColor, "array", context.getApplicationContext().getPackageName());
|
|
|
|
|
|
|
|
if (arrayId != 0) {
|
|
|
|
TypedArray colors = context.getResources().obtainTypedArray(arrayId);
|
|
|
|
int index = (int) (Math.random() * colors.length());
|
|
|
|
returnColor = colors.getColor(index, Color.BLACK);
|
|
|
|
colors.recycle();
|
|
|
|
}
|
|
|
|
return returnColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ColorInt
|
|
|
|
public static int getColor(@Nullable Palette palette, int fallback) {
|
|
|
|
if (palette != null) {
|
|
|
|
if (palette.getVibrantSwatch() != null) {
|
|
|
|
return palette.getVibrantSwatch().getRgb();
|
|
|
|
} else if (palette.getDarkVibrantSwatch() != null) {
|
|
|
|
return palette.getDarkVibrantSwatch().getRgb();
|
|
|
|
} else if (palette.getLightVibrantSwatch() != null) {
|
|
|
|
return palette.getLightVibrantSwatch().getRgb();
|
|
|
|
} else if (palette.getMutedSwatch() != null) {
|
|
|
|
return palette.getMutedSwatch().getRgb();
|
|
|
|
} else if (palette.getLightMutedSwatch() != null) {
|
|
|
|
return palette.getLightMutedSwatch().getRgb();
|
|
|
|
} else if (palette.getDarkMutedSwatch() != null) {
|
|
|
|
return palette.getDarkMutedSwatch().getRgb();
|
|
|
|
} else if (!palette.getSwatches().isEmpty()) {
|
|
|
|
return Collections.max(palette.getSwatches(), SwatchComparator.getInstance()).getRgb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fallback;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
private static Palette.Swatch getTextSwatch(@Nullable Palette palette) {
|
|
|
|
if (palette == null) {
|
|
|
|
return new Palette.Swatch(Color.BLACK, 1);
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
if (palette.getVibrantSwatch() != null) {
|
|
|
|
return palette.getVibrantSwatch();
|
|
|
|
} else {
|
|
|
|
return new Palette.Swatch(Color.BLACK, 1);
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@ColorInt
|
|
|
|
public static int getBackgroundColor(@Nullable Palette palette) {
|
|
|
|
return getProperBackgroundSwatch(palette).getRgb();
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
private static Palette.Swatch getProperBackgroundSwatch(@Nullable Palette palette) {
|
|
|
|
if (palette == null) {
|
|
|
|
return new Palette.Swatch(Color.BLACK, 1);
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
if (palette.getDarkMutedSwatch() != null) {
|
|
|
|
return palette.getDarkMutedSwatch();
|
|
|
|
} else if (palette.getMutedSwatch() != null) {
|
|
|
|
return palette.getMutedSwatch();
|
|
|
|
} else if (palette.getLightMutedSwatch() != null) {
|
|
|
|
return palette.getLightMutedSwatch();
|
|
|
|
} else {
|
|
|
|
return new Palette.Swatch(Color.BLACK, 1);
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static Palette.Swatch getBestPaletteSwatchFrom(Palette palette) {
|
|
|
|
if (palette != null) {
|
|
|
|
if (palette.getVibrantSwatch() != null) {
|
|
|
|
return palette.getVibrantSwatch();
|
|
|
|
} else if (palette.getMutedSwatch() != null) {
|
|
|
|
return palette.getMutedSwatch();
|
|
|
|
} else if (palette.getDarkVibrantSwatch() != null) {
|
|
|
|
return palette.getDarkVibrantSwatch();
|
|
|
|
} else if (palette.getDarkMutedSwatch() != null) {
|
|
|
|
return palette.getDarkMutedSwatch();
|
|
|
|
} else if (palette.getLightVibrantSwatch() != null) {
|
|
|
|
return palette.getLightVibrantSwatch();
|
|
|
|
} else if (palette.getLightMutedSwatch() != null) {
|
|
|
|
return palette.getLightMutedSwatch();
|
|
|
|
} else if (!palette.getSwatches().isEmpty()) {
|
|
|
|
return getBestPaletteSwatchFrom(palette.getSwatches());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
private static Palette.Swatch getBestPaletteSwatchFrom(List<Palette.Swatch> swatches) {
|
|
|
|
if (swatches == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return Collections.max(
|
|
|
|
swatches,
|
|
|
|
(opt1, opt2) -> {
|
|
|
|
int a = opt1 == null ? 0 : opt1.getPopulation();
|
|
|
|
int b = opt2 == null ? 0 : opt2.getPopulation();
|
|
|
|
return a - b;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getDominantColor(Bitmap bitmap, int defaultFooterColor) {
|
|
|
|
List<Palette.Swatch> swatchesTemp = Palette.from(bitmap).generate().getSwatches();
|
2021-11-27 08:36:49 +00:00
|
|
|
List<Palette.Swatch> swatches = new ArrayList<>(swatchesTemp);
|
2020-10-06 08:46:04 +00:00
|
|
|
Collections.sort(
|
|
|
|
swatches, (swatch1, swatch2) -> swatch2.getPopulation() - swatch1.getPopulation());
|
|
|
|
return swatches.size() > 0 ? swatches.get(0).getRgb() : defaultFooterColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ColorInt
|
|
|
|
public static int shiftBackgroundColorForLightText(@ColorInt int backgroundColor) {
|
|
|
|
while (ColorUtil.INSTANCE.isColorLight(backgroundColor)) {
|
|
|
|
backgroundColor = ColorUtil.INSTANCE.darkenColor(backgroundColor);
|
2019-07-22 15:26:37 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
return backgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ColorInt
|
|
|
|
public static int shiftBackgroundColorForDarkText(@ColorInt int backgroundColor) {
|
|
|
|
int color = backgroundColor;
|
|
|
|
while (!ColorUtil.INSTANCE.isColorLight(backgroundColor)) {
|
|
|
|
color = ColorUtil.INSTANCE.lightenColor(backgroundColor);
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2021-09-11 11:22:47 +00:00
|
|
|
@ColorInt
|
|
|
|
public static int shiftBackgroundColor(@ColorInt int backgroundColor) {
|
|
|
|
int color = backgroundColor;
|
|
|
|
if (ColorUtil.INSTANCE.isColorLight(color)) {
|
|
|
|
color = ColorUtil.INSTANCE.shiftColor(color, 0.5F);
|
|
|
|
} else {
|
|
|
|
color = ColorUtil.INSTANCE.shiftColor(color, 1.5F);
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2021-10-05 06:36:57 +00:00
|
|
|
public static int getMD3AccentColor(@NotNull Context context) {
|
|
|
|
if (VersionUtils.hasS()) {
|
|
|
|
return ContextCompat.getColor(context, R.color.m3_accent_color);
|
|
|
|
} else {
|
|
|
|
return ThemeStore.Companion.accentColor(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
private static class SwatchComparator implements Comparator<Palette.Swatch> {
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
private static SwatchComparator sInstance;
|
2018-07-27 13:07:33 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
static SwatchComparator getInstance() {
|
|
|
|
if (sInstance == null) {
|
|
|
|
sInstance = new SwatchComparator();
|
|
|
|
}
|
|
|
|
return sInstance;
|
|
|
|
}
|
2018-07-27 13:07:33 +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();
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
}
|
2018-07-27 13:07:33 +00:00
|
|
|
}
|