Adapt Wallpaper accent for better readability

This commit is contained in:
Prathamesh More 2022-03-15 22:41:32 +05:30
parent 6a7fa40efa
commit 1d7203cbec
4 changed files with 76 additions and 8 deletions

View file

@ -59,8 +59,26 @@ private constructor(private val mContext: Context) : ThemeStorePrefKeys, ThemeSt
return this
}
override fun wallpaperColor(color: Int): ThemeStore {
mEditor.putInt(ThemeStorePrefKeys.KEY_WALLPAPER_COLOR, color)
override fun wallpaperColor(context: Context, color: Int): ThemeStore {
if (ColorUtil.isColorLight(color)) {
mEditor.putInt(ThemeStorePrefKeys.KEY_WALLPAPER_COLOR_DARK, color)
mEditor.putInt(
ThemeStorePrefKeys.KEY_WALLPAPER_COLOR_LIGHT,
ColorUtil.getReadableColorLight(
color,
Color.WHITE
)
)
} else {
mEditor.putInt(ThemeStorePrefKeys.KEY_WALLPAPER_COLOR_LIGHT, color)
mEditor.putInt(
ThemeStorePrefKeys.KEY_WALLPAPER_COLOR_DARK,
ColorUtil.getReadableColorDark(
color,
Color.parseColor("#202124")
)
)
}
return this
}
@ -217,7 +235,7 @@ private constructor(private val mContext: Context) : ThemeStorePrefKeys, ThemeSt
}
val desaturatedColor = prefs(context).getBoolean("desaturated_color", false)
val color = if (isWallpaperAccentEnabled(context)) {
wallpaperColor(context)
wallpaperColor(context, isWindowBackgroundDark(context))
} else {
prefs(context).getInt(
ThemeStorePrefKeys.KEY_ACCENT_COLOR,
@ -232,9 +250,9 @@ private constructor(private val mContext: Context) : ThemeStorePrefKeys, ThemeSt
@CheckResult
@ColorInt
fun wallpaperColor(context: Context): Int {
fun wallpaperColor(context: Context, isDarkMode: Boolean): Int {
return prefs(context).getInt(
ThemeStorePrefKeys.KEY_WALLPAPER_COLOR,
if (isDarkMode) ThemeStorePrefKeys.KEY_WALLPAPER_COLOR_DARK else ThemeStorePrefKeys.KEY_WALLPAPER_COLOR_LIGHT,
resolveColor(context, R.attr.colorAccent, Color.parseColor("#263238"))
)
}

View file

@ -1,5 +1,6 @@
package code.name.monkey.appthemehelper
import android.content.Context
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
@ -34,7 +35,7 @@ internal interface ThemeStoreInterface {
fun accentColor(@ColorInt color: Int): ThemeStore
fun wallpaperColor(@ColorInt color: Int): ThemeStore
fun wallpaperColor(context: Context, color: Int): ThemeStore
fun accentColorRes(@ColorRes colorRes: Int): ThemeStore

View file

@ -29,6 +29,7 @@ internal interface ThemeStorePrefKeys {
const val KEY_AUTO_GENERATE_PRIMARYDARK = "auto_generate_primarydark"
const val KEY_MATERIAL_YOU = "material_you"
const val KEY_WALLPAPER_COLOR = "wallpaper_color"
const val KEY_WALLPAPER_COLOR_LIGHT = "wallpaper_color_light"
const val KEY_WALLPAPER_COLOR_DARK = "wallpaper_color_dark"
}
}

View file

@ -3,7 +3,11 @@ package code.name.monkey.appthemehelper.util
import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import kotlin.math.*
import androidx.core.graphics.ColorUtils
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
object ColorUtil {
fun desaturateColor(color: Int, ratio: Float): Int {
@ -44,6 +48,50 @@ object ColorUtil {
return shiftColor(color, 1.1f)
}
@ColorInt
fun lightenColor(
@ColorInt color: Int,
value: Float
): Int {
val hsl = FloatArray(3)
ColorUtils.colorToHSL(color, hsl)
hsl[2] += value
hsl[2] = hsl[2].coerceIn(0f, 1f)
return ColorUtils.HSLToColor(hsl)
}
@ColorInt
fun darkenColor(
@ColorInt color: Int,
value: Float
): Int {
val hsl = FloatArray(3)
ColorUtils.colorToHSL(color, hsl)
hsl[2] -= value
hsl[2] = hsl[2].coerceIn(0f, 1f)
return ColorUtils.HSLToColor(hsl)
}
@ColorInt
fun getReadableColorLight(@ColorInt color: Int, @ColorInt bgColor: Int): Int {
var foregroundColor = color
while (ColorUtils.calculateContrast(foregroundColor, bgColor) <= 3.0
) {
foregroundColor = darkenColor(foregroundColor, 0.1F)
}
return foregroundColor
}
@ColorInt
fun getReadableColorDark(@ColorInt color: Int, @ColorInt bgColor: Int): Int {
var foregroundColor = color
while (ColorUtils.calculateContrast(foregroundColor, bgColor) <= 3.0
) {
foregroundColor = lightenColor(foregroundColor, 0.1F)
}
return foregroundColor
}
fun isColorLight(@ColorInt color: Int): Boolean {
val darkness =
1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255