feat: Better At-a-Glance perceptive wallpaper colour luminance detection
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
package app.lawnchair.util
|
||||
|
||||
import android.app.WallpaperColors
|
||||
import android.content.Context
|
||||
import android.graphics.Color
|
||||
import androidx.core.graphics.ColorUtils
|
||||
import app.lawnchair.wallpaper.WallpaperColorsCompat
|
||||
import app.lawnchair.wallpaper.WallpaperManagerCompat
|
||||
|
||||
fun isWallpaperDark(context: Context): Boolean {
|
||||
val wallpaperManager = WallpaperManagerCompat.INSTANCE.get(context)
|
||||
val colors: WallpaperColorsCompat? = wallpaperManager.wallpaperColors
|
||||
return colors?.primaryColor?.let { argb ->
|
||||
val darkness =
|
||||
1 - (0.299 * Color.red(argb) + 0.587 * Color.green(argb) + 0.114 * Color.blue(argb)) / 255
|
||||
darkness >= 0.5
|
||||
} ?: false
|
||||
if (colors != null) {
|
||||
val hints = colors.colorHints
|
||||
if ((hints and WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0) {
|
||||
return false
|
||||
}
|
||||
if ((hints and WallpaperColors.HINT_SUPPORTS_DARK_THEME) != 0) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
val primaryColor = colors?.primaryColor ?: return false
|
||||
return ColorUtils.calculateLuminance(primaryColor) < 0.5
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user