From 0586edb592f69e14b1d4f77ddcee5fe170a4bc91 Mon Sep 17 00:00:00 2001 From: David Perez Date: Tue, 28 Apr 2026 13:45:03 -0500 Subject: [PATCH] PM-35925: bug: Update 'hexToColor' function to handle default names (#6841) --- .../ui/platform/base/util/StringExtensions.kt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ui/src/main/kotlin/com/bitwarden/ui/platform/base/util/StringExtensions.kt b/ui/src/main/kotlin/com/bitwarden/ui/platform/base/util/StringExtensions.kt index 6e697196ad..c151366649 100644 --- a/ui/src/main/kotlin/com/bitwarden/ui/platform/base/util/StringExtensions.kt +++ b/ui/src/main/kotlin/com/bitwarden/ui/platform/base/util/StringExtensions.kt @@ -11,6 +11,7 @@ import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.input.VisualTransformation import androidx.compose.ui.text.rememberTextMeasurer import androidx.core.graphics.toColorInt +import timber.log.Timber import java.net.URI import java.net.URISyntaxException import java.text.Normalizer @@ -169,11 +170,16 @@ fun String.toAnnotatedString(): AnnotatedString = AnnotatedString(text = this) * Supported formats: * - "rrggbb" / "#rrggbb" * - "aarrggbb" / "#aarrggbb" + * Support for some default color names per the [toColorInt] function. */ -fun String.hexToColor(): Color = if (startsWith("#")) { - Color(toColorInt()) -} else { - Color("#$this".toColorInt()) +fun String.hexToColor(): Color { + val colorString = if (Regex("^[0-9A-Fa-f]+$").matches(this)) "#$this" else this + return try { + Color(colorString.toColorInt()) + } catch (e: IllegalArgumentException) { + Timber.e(e, "Failed to parse color: $this") + Color.Black + } } /**