From b0e930b09829f44d3c8ec009a824b8774ea10c97 Mon Sep 17 00:00:00 2001 From: Brian Yencho Date: Mon, 4 Dec 2023 11:25:08 -0600 Subject: [PATCH] BIT-1204: Use WCAG 1.0 values when determining overlay colors (#309) --- .../ui/platform/base/util/ColorExtensions.kt | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/platform/base/util/ColorExtensions.kt b/app/src/main/java/com/x8bit/bitwarden/ui/platform/base/util/ColorExtensions.kt index 51db1400e..a592175f9 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/platform/base/util/ColorExtensions.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/platform/base/util/ColorExtensions.kt @@ -2,6 +2,7 @@ package com.x8bit.bitwarden.ui.platform.base.util import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable +import androidx.compose.runtime.Stable import androidx.compose.ui.graphics.Color 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 * overlay when high contrast is important. */ +@Stable 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