Skip to content

Commit

Permalink
[app] light up nav icon
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Dec 26, 2021
1 parent b4d69df commit 4d7b1b4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

}

This file was deleted.

0 comments on commit 4d7b1b4

Please sign in to comment.