Skip to content

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

@wax911

Description

@wax911

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    🐛 bugSomething isn't working

    Type

    Projects

    Status

    📋 Backlog

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions