kotlin convert color hex to rgba
To convert a color hex to RGBA in Kotlin, you can use the following code:
fun hexToRGBA(hex: String): String {
val color = java.awt.Color.decode(hex)
val red = color.red
val green = color.green
val blue = color.blue
val alpha = color.alpha
return "rgba($red, $green, $blue, $alpha)"
}
Explanation:
1. Define a function called hexToRGBA
that takes a hexadecimal color value as a parameter.
2. Use java.awt.Color.decode(hex)
to convert the hexadecimal color value to a Java Color object.
3. Retrieve the red, green, blue, and alpha components of the color using color.red
, color.green
, color.blue
, and color.alpha
respectively.
4. Return a string in the RGBA format using the retrieved color components.