Android convert color int to hexa String

前端 未结 3 1963
慢半拍i
慢半拍i 2020-12-30 18:27
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 he

相关标签:
3条回答
  • 2020-12-30 19:01

    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));
    
    0 讨论(0)
  • 2020-12-30 19:10

    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)
    
    0 讨论(0)
  • 2020-12-30 19:18

    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);
    
    0 讨论(0)
提交回复
热议问题