PM-35925: bug: Update 'hexToColor' function to handle default names (#6841)

This commit is contained in:
David Perez
2026-04-28 13:45:03 -05:00
committed by GitHub
parent 909d999186
commit 0586edb592

View File

@@ -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
}
}
/**