From 4d7b1b492a34efda4770a9647628f02005044b24 Mon Sep 17 00:00:00 2001 From: tornaco Date: Sun, 26 Dec 2021 15:19:14 +0800 Subject: [PATCH] [app] light up nav icon --- .../common/CommonDataBindingAdapters.java | 7 +- .../tornaco/android/thanos/util/ColorUtils.kt | 98 +++++++++++++++++++ .../thanos/util/PaletteColorUtils.java | 21 ---- 3 files changed, 104 insertions(+), 22 deletions(-) create mode 100644 android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/ColorUtils.kt delete mode 100755 android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/PaletteColorUtils.java diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/CommonDataBindingAdapters.java b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/CommonDataBindingAdapters.java index f1b83faf6..4e12e23fa 100644 --- a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/CommonDataBindingAdapters.java +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/CommonDataBindingAdapters.java @@ -24,6 +24,7 @@ import github.tornaco.android.thanos.core.pm.AppInfo; import github.tornaco.android.thanos.module.common.R; import github.tornaco.android.thanos.theme.AppThemePreferences; +import github.tornaco.android.thanos.util.ColorUtils; import github.tornaco.android.thanos.util.GlideApp; import github.tornaco.android.thanos.util.GlideRequest; import github.tornaco.android.thanos.widget.GrayscaleTransformation; @@ -43,7 +44,11 @@ public static void setCircleBgTint(ImageView imageView, @ColorRes int res) { if (res == 0) return; Drawable bg = AppCompatResources.getDrawable(imageView.getContext(), R.drawable.module_common_circle_bg_blue); if (bg == null) return; - bg.setColorFilter(new PorterDuffColorFilter(imageView.getContext().getColor(res), PorterDuff.Mode.SRC_IN)); + int iconColor = imageView.getContext().getColor(res); + int lColor = ColorUtils.INSTANCE.lightenColor(iconColor, 0.5f); + + imageView.setColorFilter(iconColor); + bg.setColorFilter(new PorterDuffColorFilter(lColor, PorterDuff.Mode.SRC_IN)); imageView.setBackground(bg); } diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/ColorUtils.kt b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/ColorUtils.kt new file mode 100644 index 000000000..b75032af6 --- /dev/null +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/ColorUtils.kt @@ -0,0 +1,98 @@ +package github.tornaco.android.thanos.util + +import android.graphics.Color +import androidx.annotation.ColorInt +import androidx.annotation.Size +import kotlin.math.max +import kotlin.math.min + +object ColorUtils { + + @ColorInt + fun darkenColor( + @ColorInt color: Int, + value: Float, + ): Int { + val hsl = colorToHSL(color) + hsl[2] -= value + hsl[2] = max(0f, min(hsl[2], 1f)) + return hslToColor(hsl) + } + + @ColorInt + fun lightenColor( + @ColorInt color: Int, + value: Float, + ): Int { + val hsl = colorToHSL(color) + hsl[2] += value + hsl[2] = max(0f, min(hsl[2], 1f)) + return hslToColor(hsl) + } + + @Size(3) + private fun colorToHSL( + @ColorInt color: Int, + @Size(3) hsl: FloatArray = FloatArray(3), + ): FloatArray { + val r = Color.red(color) / 255f + val g = Color.green(color) / 255f + val b = Color.blue(color) / 255f + + val max = max(r, max(g, b)) + val min = min(r, min(g, b)) + hsl[2] = (max + min) / 2 + + if (max == min) { + hsl[1] = 0f + hsl[0] = hsl[1] + + } else { + val d = max - min + + hsl[1] = if (hsl[2] > 0.5f) d / (2f - max - min) else d / (max + min) + when (max) { + r -> hsl[0] = (g - b) / d + (if (g < b) 6 else 0) + g -> hsl[0] = (b - r) / d + 2 + b -> hsl[0] = (r - g) / d + 4 + } + hsl[0] /= 6f + } + return hsl + } + + @ColorInt + private fun hslToColor(@Size(3) hsl: FloatArray): Int { + val r: Float + val g: Float + val b: Float + + val h = hsl[0] + val s = hsl[1] + val l = hsl[2] + + if (s == 0f) { + b = l + g = b + r = g + } else { + val q = if (l < 0.5f) l * (1 + s) else l + s - l * s + val p = 2 * l - q + r = hue2rgb(p, q, h + 1f / 3) + g = hue2rgb(p, q, h) + b = hue2rgb(p, q, h - 1f / 3) + } + + return Color.rgb((r * 255).toInt(), (g * 255).toInt(), (b * 255).toInt()) + } + + private fun hue2rgb(p: Float, q: Float, t: Float): Float { + var valueT = t + if (valueT < 0) valueT += 1f + if (valueT > 1) valueT -= 1f + if (valueT < 1f / 6) return p + (q - p) * 6f * valueT + if (valueT < 1f / 2) return q + return if (valueT < 2f / 3) p + (q - p) * (2f / 3 - valueT) * 6f else p + } + +} \ No newline at end of file diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/PaletteColorUtils.java b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/PaletteColorUtils.java deleted file mode 100755 index 33a843725..000000000 --- a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/util/PaletteColorUtils.java +++ /dev/null @@ -1,21 +0,0 @@ -package github.tornaco.android.thanos.util; - -import android.content.Context; - -/** - * Created by guohao4 on 2017/10/28. - * Email: Tornaco@163.com - */ - -public abstract class PaletteColorUtils { - - public interface PickReceiver { - void onColorReady(int color); - } - - // TODO - public static void pickPrimaryColor(Context context, final PickReceiver receiver, - String pkg, final int defColor) { - receiver.onColorReady(defColor); - } -}