chrrissoft
chrrissoft

Reputation: 189

how to convert rgb color to hex color - kotlin

This is what I do

val color = String.format("0XFF%02x%02x%02x", r, g, b)

this returns me a string "0XFFhexcode" I want to convert it to long, to store it and then use it as a color

color.toLong()

I have the following error

java.lang.NumberFormatException: For input string: 0XFFhexcode

Upvotes: 3

Views: 5022

Answers (2)

Jagar
Jagar

Reputation: 872

You can do it simply like the following:

val rgb =  Color.rgb(red, green, blue)
val hex = String.format("#%06X", 0xFFFFFF and rgb)

Upvotes: 0

Siddharth Kumar
Siddharth Kumar

Reputation: 168

This code might help you:

Integer.toHexString(Color.rgb(r, g, b))

Upvotes: 1

Related Questions