51
public static int RGB(float[] hsv) {
    return Color.HSVToColor(hsv);
}

this function add an int, froma color. how can i convert that int to a hexa string: #efefef

lacas
  • 13,928
  • 30
  • 109
  • 183
  • Possible duplicate of [How to convert a color integer to a hex String in Android?](http://stackoverflow.com/questions/6539879/how-to-convert-a-color-integer-to-a-hex-string-in-android) – avalancha Oct 22 '15 at 09:19

4 Answers4

185

The answer of st0le is not correct with respect to colors. It does not work if first color components are 0. So toHexString is useless.

However this code will work as expected:

String strColor = String.format("#%06X", 0xFFFFFF & intColor);
Dmitry Kochin
  • 3,830
  • 4
  • 22
  • 14
22

Here are 2 ways to convert Integer to Hex Strings...

    int  n = 123456;
    System.out.println(String.format("#%X", n)); //use lower case x for lowercase hex
    System.out.println("#"+Integer.toHexString(n));
st0le
  • 33,375
  • 8
  • 89
  • 89
1

If you want to convert to javascript format:

val hexColor = String.format("%06X", 0xFFFFFFFF.and(R.color.text.toColorInt(context).toLong()))

val javascriptHexColor = "#" + hexColor.substring(2) + hexColor.substring(0, 2)
jc12
  • 1,411
  • 1
  • 18
  • 24
0

Use this way

Java:

 String hexColor = "#" + Integer.toHexString(ContextCompat.getColor(context, R.color.colorTest))

Kotlin:

var hexColor = "#${Integer.toHexString(ContextCompat.getColor(context, R.color.colorTest))}"
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78