BIT-1204: Use WCAG 1.0 values when determining overlay colors (#309)

This commit is contained in:
Brian Yencho 2023-12-04 11:25:08 -06:00 committed by Álison Fernandes
parent d3a813b647
commit b0e930b098

View file

@ -2,6 +2,7 @@ package com.x8bit.bitwarden.ui.platform.base.util
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance import androidx.compose.ui.graphics.luminance
@ -15,8 +16,20 @@ private const val DARK_OVERLAY_LUMINANCE_THRESHOLD = 0.65f
* Returns `true` if the given [Color] would require a light color to be used in any kind of * Returns `true` if the given [Color] would require a light color to be used in any kind of
* overlay when high contrast is important. * overlay when high contrast is important.
*/ */
@Stable
val Color.isLightOverlayRequired: Boolean val Color.isLightOverlayRequired: Boolean
get() = this.luminance() < DARK_OVERLAY_LUMINANCE_THRESHOLD get() = this.luminanceWcag1 < DARK_OVERLAY_LUMINANCE_THRESHOLD
/**
* Returns the luminance of the given color based on the
* [WCAG 1.0 guidelines of 2000](https://www.w3.org/TR/AERT/#color-contrast), which give more
* weight to the red and blue components relative to more recent WCAG 2.0 values used by
* [Color.luminance].
*/
@Suppress("MagicNumber")
@Stable
private val Color.luminanceWcag1: Float
get() = (red * 0.299f) + (green * 0.587f) + (blue * 0.114f)
/** /**
* Returns a [Color] within the current theme that can safely be overlaid on top of the given * Returns a [Color] within the current theme that can safely be overlaid on top of the given