如何使用 Java 将十六进制转换为 rgb?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4129666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert hex to rgb using Java?
提问by user236501
How can I convert hex color to RGB code in Java? Mostly in Google, samples are on how to convert from RGB to hex.
如何在 Java 中将十六进制颜色转换为 RGB 代码?大多数在 Google 中,示例是关于如何从 RGB 转换为十六进制。
采纳答案by xhh
I guess this should do it:
我想这应该这样做:
/**
*
* @param colorStr e.g. "#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 ) );
}
回答by Ignacio Vazquez-Abrams
Convert it to an integer, then divmod it twice by 16, 256, 4096, or 65536 depending on the length of the original hex string (3, 6, 9, or 12 respectively).
将其转换为整数,然后根据原始十六进制字符串的长度(分别为 3、6、9 或 12)对它进行两次 16、256、4096 或 65536 的 divmod 运算。
回答by Samuel
Hexidecimal color codes are already rgb. The format is #RRGGBB
十六进制颜色代码已经是 rgb。格式为#RRGGBB
回答by Andrew Beck
public static void main(String[] args) {
int hex = 0x123456;
int r = (hex & 0xFF0000) >> 16;
int g = (hex & 0xFF00) >> 8;
int b = (hex & 0xFF);
}
回答by MattRS
A hex color code is #RRGGBB
十六进制颜色代码是#RRGGBB
RR, GG, BB are hex values ranging from 0-255
RR、GG、BB 是 0-255 范围内的十六进制值
Let's call RR XY where X and Y are hex character 0-9A-F, A=10, F=15
让我们调用 RR XY,其中 X 和 Y 是十六进制字符 0-9A-F,A=10,F=15
The decimal value is X*16+Y
十进制值为 X*16+Y
If RR = B7, the decimal for B is 11, so value is 11*16 + 7 = 183
如果 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;
}
回答by Ben Hoskins
Actually, there's an easier (built in) way of doing this:
实际上,有一种更简单(内置)的方法可以做到这一点:
Color.decode("#FFCCEE");
回答by Todd Davies
For Androiddevelopment, I use:
对于Android开发,我使用:
int color = Color.parseColor("#123456");
回答by dragunfli
To elaborate on the answer @xhh provided, you can append the red, green, and blue to format your string as "rgb(0,0,0)" before returning it.
要详细说明@xhh 提供的答案,您可以在返回字符串之前附加红色、绿色和蓝色以将字符串格式化为“rgb(0,0,0)”。
/**
*
* @param colorStr e.g. "#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();
}
回答by Naveen
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
回答by Rich S
Lots of these solutions work, but this is an alternative.
许多这些解决方案都有效,但这是另一种选择。
String hex="#00FF00"; // green
long thisCol=Long.decode(hex)+4278190080L;
int useColour=(int)thisCol;
If you don't add 4278190080 (#FF000000) the colour has an Alpha of 0 and won't show.
如果您不添加 4278190080 (#FF000000),则颜色的 Alpha 为 0 并且不会显示。