如何使用Java将十六进制转换为RGB?

122

我该如何在Java中将十六进制颜色转换为RGB代码?在Google上,大多数示例都是关于如何从RGB转换为十六进制。


1
你能举个例子说明你想从什么转换成什么吗?不太清楚你具体想做什么。 - kkress
000000 将转换为黑色 RGB。 - user236501
21个回答

360

实际上,有一种更简单(内置)的方法可以做到这一点:

Color.decode("#FFCCEE");

5
很遗憾,那就是AWT :/ - wuppi
7
我认为这是好消息,因为AWT在JDK中。这其中有什么不幸之处吗? - Dmitry Avtonomov
22
被接受的解决方案也使用了AWT。AWT对于原问题的提问者来说不是问题。这应该是被接受的解决方案。 - jewbix.cube
9
在安卓手机上:Color.parseColor() 函数用于解析颜色字符串并返回对应的颜色值。 - Gelldur

178

我猜这应该可以解决问题:

/**
 * 
 * @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 ) );
}

1
对于那些想要3个字符版本的人,请注意在3个字符的情况下,每个值都必须是* 255/16。我用“000”、“aaa”和“fff”进行了测试,它们现在都可以正常工作。 - Andrew

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

29

我在进行Android开发时使用以下工具:

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

1
只需用“0x”替换“#”即可。 - Julian Os
2
Color.parseColor不支持像这样的三位数字颜色代码:#fff。 - neoexpert
你可以尝试以下代码 #fff int red = colorString.charAt(1) == '0' ? 0 : 255; int blue = colorString.charAt(2) == '0' ? 0 : 255; int green = colorString.charAt(3) == '0' ? 0 : 255; Color.rgb(red, green,blue); - GTID

13

这里有一个能够处理RGB和RGBA版本的版本:

/**
 * Converts a hex string to a color. If it can't be converted null is returned.
 * @param hex (i.e. #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;
}

1
这对我很有用,因为Integer.toHexString支持alpha通道,但是Integer.decode或Color.decode似乎不支持它。 - Ted

5
您可以简单地按如下操作进行:

 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

太棒了!正是我所需要的!谢谢! - ONE

4

一个十六进制颜色代码是 #RRGGBB

RR、GG、BB 是十六进制值,范围从 0-255

我们将 RR 称为 XY,其中 X 和 Y 是十六进制字符 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;
}

4

关于 JavaFX

import javafx.scene.paint.Color;

.

Color whiteColor = Color.valueOf("#ffffff");

1
许多这样的解决方案都是有效的,但这是一种替代方法。
String hex="#00FF00"; // green
long thisCol=Long.decode(hex)+4278190080L;
int useColour=(int)thisCol;

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


1

将其转换为整数,然后根据原始十六进制字符串的长度(分别为3、6、9或12),将其两次divmod分别除以16、256、4096或65536。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接