What would be most optimal way to convert hex colo...
# compose
k
What would be most optimal way to convert hex color to a Compose Color, and to avoid recreation of an object on recomposition? Colors are mainly used inside
LazyColumn
. Only thing I could think of is using
remember
, but I'd still would have to use
remember
inside
LazyColumn
.
a
The (pseudo) constructors of
Color
are marked with
@Stable
so the runtime should have already done the optimization (i.e. only calling the function again if parameters changed).
k
Would setting a
@Stable
annotation to something like this optimize function on recomposition?
Copy code
fun String?.toComposeColor(alpha: Int = 0xFF, defaultColor: Color = Color.Transparent): Color {
    if (this.isNullOrEmpty()) return defaultColor
    val color = android.graphics.Color.parseColor(this)
    return Color(red = color.red, green = color.green, blue = color.blue, alpha = alpha)
}
a
Yep.
a
Color is an inline class so there is no object creation involved, you don’t need to optimize that. using remember would be even less efficient as in order to be remembered the inline class would have to be boxed