How can I convert hex color to RGB code in Java? Mostly in Google, samples are on how to convert from RGB to hex.
Here is a version that handles both RGB and RGBA versions:
/**
* 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;
}
A hex color code is #RRGGBB
RR, GG, BB are hex values ranging from 0-255
Let's call RR XY where X and Y are hex character 0-9A-F, A=10, F=15
The decimal value is X*16+Y
If RR = B7, the decimal for B is 11, so value is 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;
}
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.
Here is another faster version that handles RGBA versions:
public static int hexToIntColor(String hex){
int Alpha = Integer.valueOf(hex.substring(0, 2), 16);
int Red = Integer.valueOf(hex.substring(2, 4), 16);
int Green = Integer.valueOf(hex.substring(4, 6), 16);
int Blue = Integer.valueOf(hex.substring(6, 8), 16);
Alpha = (Alpha << 24) & 0xFF000000;
Red = (Red << 16) & 0x00FF0000;
Green = (Green << 8) & 0x0000FF00;
Blue = Blue & 0x000000FF;
return Alpha | Red | Green | Blue;
}
The easiest way:
// 0000FF
public static Color hex2Rgb(String colorStr) {
return new Color(Integer.valueOf(colorStr, 16));
}
The other day I'd been solving the similar issue and found convenient to convert hex color string to int array [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;
}