How to convert hex to rgb using Java?

后端 未结 19 2087
-上瘾入骨i
-上瘾入骨i 2020-11-27 13:46

How can I convert hex color to RGB code in Java? Mostly in Google, samples are on how to convert from RGB to hex.

19条回答
  •  无人及你
    2020-11-27 14:10

    you can do it simply as below:

     public static int[] getRGB(final String rgb)
    {
        final int[] ret = new int[3];
        for (int i = 0; i < 3; i++)
        {
            ret[i] = Integer.parseInt(rgb.substring(i * 2, i * 2 + 2), 16);
        }
        return ret;
    }
    

    For Example

    getRGB("444444") = 68,68,68   
    getRGB("FFFFFF") = 255,255,255
    

提交回复
热议问题