Convert hex color value ( #ffffff ) to integer value

前端 未结 9 1062
孤城傲影
孤城傲影 2020-12-13 01:26

I am receiving hex color values from a server (in this form, #xxxxxx , example #000000 for black)

How do I convert this to an integer value

9条回答
  •  别那么骄傲
    2020-12-13 02:10

    Based on CQM's answer and on ovokerie-ogbeta's answer to another question I've come up with this solution:

    if (colorAsString.length() == 4) { // #XXX
        colorAsString = colorAsString.replaceAll("#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])", "#$1$1$2$2$3$3");
    }
    
    int color = Color.parseColor(colorAsString);
    

提交回复
热议问题