AniTrend / anitrend-v2

Rewrite project for AniTrend 🔥
GNU General Public License v3.0
45 stars 6 forks source link

[android-core] Calculate contrast colour against dynamic app background correctly #241

Open wax911 opened 1 year ago

wax911 commented 1 year ago

AniTrend Issue Guidelines

Before opening a new issue, please take a moment to review our community guidelines to make the contribution process easy and effective for everyone involved.

You may find an answer in already closed issues: https://github.com/AniTrend/anitrend-v2/issues?q=is%3Aissue+is%3Aclosed

Description of Bug

Contrast adjustments required for a few widgets that dynamically use the accent colour from a media image. The problem comes in when we have a colour that does not have enough contrast with a dark background, e.g. when you switch to night mode.

Reproduction Steps

Screenshots/Videos

List (dark) Detail (dark) Detail (light)

Additional Context

See co.anitrend.core.android.helpers.color.ColorExtensions which is what we use to infer and shift colours that would typically not contrast enough with the background colour of the app at any given point in time. https://github.com/AniTrend/anitrend-v2/blob/ff08b5d1df39e5527beed9435de72de4e16e7e32/android-core/src/main/kotlin/co/anitrend/core/android/helpers/color/ColorExtensions.kt#L29-L43

wax911 commented 1 year ago

A follow up to this would most likely introduce something like the following code sample, given this is a very rough draft

fun Int.adjustColorForContrast(@ColorInt backgroundColor: Int): Int {
   val color = // convert `this` to `Color`
    val background = // convert `backgroundColor` to `Color`
    val colorLuminance = 0.2126 * color.red + 0.7152 * color.green + 0.0722 * color.blue
    val backgroundLuminance = 0.2126 * background.red + 0.7152 * background.green + 0.0722 * background.blue
    val contrastRatio = (colorLuminance + 0.05) / (backgroundLuminance + 0.05)
    val threshold = 4.5
    if (contrastRatio < threshold) {
        return color.inv() // Invert the color
    }
    return color
}