3

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
chrrissoft
  • 149
  • 1
  • 10
  • https://stackoverflow.com/questions/19519468/how-to-convert-string-to-long-in-kotlin – obama Apr 23 '22 at 21:19
  • 2
    You're probably wanting `.toLong(16)` so it converts from a hexadecimal value to a `Long` value. You need to get rid of the leading `0X` to do that, though. – MFazio23 Apr 23 '22 at 21:21
  • that works, but what I want is to store the value of a color. – chrrissoft Apr 23 '22 at 21:55

2 Answers2

1

This code might help you:

Integer.toHexString(Color.rgb(r, g, b))
Siddharth Kumar
  • 168
  • 1
  • 2
  • 13
0

You can do it simply like the following:

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