Jetpack Compose Color class Cheat Sheet

Here are some useful codes for handling androidx.compose.ui.graphics.Color class in Jetpack Compose

Predefined colors that you can use

var red = Color.Red
var black = Color.Black
var blue = Color.Blue
var cyan = Color.Cyan
var darkGray = Color.DarkGray
var gray = Color.Gray
var green = Color.Green
var lightGray = Color.LightGray
var magenta = Color.Magenta
var white = Color.White
var yellow = Color.Yellow

Create a color from RGB

// Create color from RGB
val red = 255
val green = 255
val blue = 255

val color = Color(red, green, blue)

Create a color from RGBA

// Create color from RGBA
val red = 255
val green = 255
val blue = 255
val alpha = 255 // 0: transparent, 255: solid

val color = Color(red, green, blue, alpha)

Create a color from hexadecimal RGB string

// Create from hexadecimal
val hexadecimal = "#AABBCC" // AA: Green, BB: Red, CC: Blue
val color  = Color(hexadecimal.toColorInt())

Create a color from hexadecimal RGBA string

// Create from hexadecimal
// FF: Alpha (00 transparent FF Solid) AA: Green, BB: Red, CC: Blue
val hexadecimal = "#FFAABBCC"
val color  = Color(hexadecimal.toColorInt())

Create color from name

val red = "red"
val color = Color(red.toColorInt())

/* Valid strings: red, blue, green, black, white,
    gray, cyan, magenta, yellow, lightgray, darkgray,
    grey, lightgrey, darkgrey, aqua, fuchsia, lime,
    maroon, navy, olive, purple, silver, teal. */

Create color from hexadecimal number

// FF: Alpha (00 transparent FF Solid) AA: Green, BB: Red, CC: Blue
val hex = 0xFFAABBCC
val color = Color(hex)

Represent color as an integer

val red = Color.Red
val intColor = red.toArgb()

// You can create the color with intColor
val redColor = Color(intColor)

Represent color as an String

val red = Color.Red

// #FF0000
val hex = "#" + Integer.toHexString(red.toArgb()).uppercase()
// You can create the color with hex
val redColor = Color(hex.toColorInt())

Leave a Reply

Your email address will not be published. Required fields are marked *