How to convert hex to rgb using Java?

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


我想这应该做到这一点:

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

实际上,这样做有一个更简单(内置)的方法:

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);
}
链接地址: http://www.djcxy.com/p/87792.html

上一篇: 在红色和绿色之间为功率计生成颜色?

下一篇: 如何使用Java将hex转换为rgb?