Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wax911 opened this issue Jan 29, 2023 · 1 comment
Labels
🐛 bug Something isn't working
Milestone

Comments

@wax911
Copy link
Member

wax911 commented Jan 29, 2023

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.

/** Checks if a color has enough contrast */
fun Int.hasEnoughContrastFor(
@ColorInt backgroundColor: Int
) = ColorUtils.calculateContrast(
this,
backgroundColor
) > 1.8f
/** Decreases the lightness of the color by the [factor] */
fun Int.increaseContrastBy(factor: Float): Int {
val hsl = FloatArray(3)
ColorUtils.colorToHSL(this, hsl)
hsl[2] = hsl[2] * factor
return ColorUtils.HSLToColor(hsl)
}

@wax911 wax911 added the 🐛 bug Something isn't working label Jan 29, 2023
@wax911 wax911 added this to the v2.0.0-alpha milestone Jan 29, 2023
@wax911
Copy link
Member Author

wax911 commented Jan 29, 2023

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
Status: 📋 Backlog
Development

No branches or pull requests

1 participant