如何使用Java将hex转换为rgb?

如何在Java中将hex颜色转换为RGB代码? 大多数在谷歌,样品是如何从RGB转换为hex。

我想这应该做到这一点:

/** * * @param colorStr eg "#FFFFFF" * @return */ public static Color hex2Rgb(String colorStr) { return new Color( Integer.valueOf( colorStr.substring( 1, 3 ), 16 ), Integer.valueOf( colorStr.substring( 3, 5 ), 16 ), Integer.valueOf( colorStr.substring( 5, 7 ), 16 ) ); } 

当我想找点别的东西的时候,我偶然发现了这个post。 实际上,这样做有一个更简单的方法:

 Color.decode("#FFCCEE"); 
 public static void main(String[] args) { int hex = 0x123456; int r = (hex & 0xFF0000) >> 16; int g = (hex & 0xFF00) >> 8; int b = (hex & 0xFF); } 

对于Android开发,我使用:

 int color = Color.parseColor("#123456"); 

hex颜色代码是#RRGGBB

RR,GG,BB是hex值,范围从0-255

让我们调用RR XY,其中X和Y是hex字符0-9A-F,A = 10,F = 15

十进制值是X * 16 + Y

如果RR = B7,B的小数点是11,那么值是11 * 16 + 7 = 183

 public int[] getRGB(String rgb){ int[] ret = new int[3]; for(int i=0; i<3; i++){ ret[i] = hexToInt(rgb.charAt(i*2), rgb.charAt(i*2+1)); } return ret; } public int hexToInt(char a, char b){ int x = a < 65 ? a-48 : a-55; int y = b < 65 ? b-48 : b-55; return x*16+y; } 

你可以做到这一点,如下所示:

  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; } 

例如

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

这是处理RGB和RGBA版本的版本:

 /** * Converts a hex string to a color. If it can't be converted null is returned. * @param hex (ie #CCCCCCFF or CCCCCC) * @return Color */ public static Color HexToColor(String hex) { hex = hex.replace("#", ""); switch (hex.length()) { case 6: return new Color( Integer.valueOf(hex.substring(0, 2), 16), Integer.valueOf(hex.substring(2, 4), 16), Integer.valueOf(hex.substring(4, 6), 16)); case 8: return new Color( Integer.valueOf(hex.substring(0, 2), 16), Integer.valueOf(hex.substring(2, 4), 16), Integer.valueOf(hex.substring(4, 6), 16), Integer.valueOf(hex.substring(6, 8), 16)); } return null; } 

将其转换为整数,然后根据原始hexstring的长度(分别为3,6,9或12)将其分成两次16,256,4096或65536两次。

hex颜色代码已经是rgb。 格式是#RRGGBB

许多这些解决scheme的工作,但这是一个替代scheme。

 String hex="#00FF00"; // green long thisCol=Long.decode(hex)+4278190080L; int useColour=(int)thisCol; 

如果不添加4278190080(#FF000000),则颜色的Alpha值为0,不会显示。

为了详细说明@xhh提供的答案,可以在返回之前附加红色,绿色和蓝色来将string格式化为“rgb(0,0,0)”。

 /** * * @param colorStr eg "#FFFFFF" * @return String - formatted "rgb(0,0,0)" */ public static String hex2Rgb(String colorStr) { Color c = new Color( Integer.valueOf(hexString.substring(1, 3), 16), Integer.valueOf(hexString.substring(3, 5), 16), Integer.valueOf(hexString.substring(5, 7), 16)); StringBuffer sb = new StringBuffer(); sb.append("rgb("); sb.append(c.getRed()); sb.append(","); sb.append(c.getGreen()); sb.append(","); sb.append(c.getBlue()); sb.append(")"); return sb.toString(); } 

有一天,我一直在解决类似的问题,发现hex颜色string转换为int数组[alpha,r,g,b]方便:

  /** * Hex color string to int[] array converter * * @param hexARGB should be color hex string: #AARRGGBB or #RRGGBB * @return int[] array: [alpha, r, g, b] * @throws IllegalArgumentException */ public static int[] hexStringToARGB(String hexARGB) throws IllegalArgumentException { if (!hexARGB.startsWith("#") || !(hexARGB.length() == 7 || hexARGB.length() == 9)) { throw new IllegalArgumentException("Hex color string is incorrect!"); } int[] intARGB = new int[4]; if (hexARGB.length() == 9) { intARGB[0] = Integer.valueOf(hexARGB.substring(1, 3), 16); // alpha intARGB[1] = Integer.valueOf(hexARGB.substring(3, 5), 16); // red intARGB[2] = Integer.valueOf(hexARGB.substring(5, 7), 16); // green intARGB[3] = Integer.valueOf(hexARGB.substring(7), 16); // blue } else hexStringToARGB("#FF" + hexARGB.substring(1)); return intARGB; }