0

I am having a problem using a method from com.esri.arcgis.display.IColor. The method is IColor.getRGB() which returns an int.

IColor symbolColor = symbol.getColor();
int colorInt = symbolColor.getRGB();
//TODO: get RGB values from colorInt

From the documentation:

RGB returns the Long (ASCII) number calculated from the Red, Green and Blue color attributes.

I need to get the individual RGB values (standard 0-255) from this int somehow but I have been unable to find almost any information on converting from an ASCII number to RGB values.

randers
  • 5,031
  • 5
  • 37
  • 64
GregH
  • 5,125
  • 8
  • 55
  • 109
  • 2
    Possible duplicate of [Bit Shift and Bitwise operations to encode RGB values](http://stackoverflow.com/questions/19277010/bit-shift-and-bitwise-operations-to-encode-rgb-values) – Andy Turner Oct 14 '15 at 22:26
  • An int is an int is an int. You might look at `colorInt` formatted as a decimal, say _10760562_, and it seems to make no sense as an RGB value, but if you format and print it as a hex value it will be `0xRRGGBB` — _10760562 == 0xA43172_, which clearly contains the individual red, green, and blue values. – Stephen P Oct 14 '15 at 23:50

2 Answers2

0

Have solved by converting the int to hex, and then decoding the hex string using Color.decode() to obtain a Color. Using Color instance we can grab the individual RGB values.

IColor symbolColor = symbol.getColor();
int colorInt = symbolColor.getRGB();
String hexColor = Integer.toHexString(colorInt);
Color color = Color.decode("#"+hexColor);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int alpha = color.getAlpha();`
Kamran Ali
  • 5,904
  • 2
  • 26
  • 36
GregH
  • 5,125
  • 8
  • 55
  • 109
-1

It is possible to convert these values by transferring their RGB integer.

int rgb = symbol.getColor().getRGB();
Color color = new Color(rgb);
int red = color.getRed();
int blue = color.getBlue();
int green = color.getGreen();
int alpha = color.getAlpha();

You might need to further investigate wether that rgb variable also contains the alpha values, in which case you want to use new Color(rgb, true) instead.

randers
  • 5,031
  • 5
  • 37
  • 64
  • Using bit & operation and shift would be more straightforward and faster as shown by Andy Turner's link. – dragon66 Oct 14 '15 at 22:48
  • @dragon66 I would personally prefer a library usage over bitwise operations because I find them very unreadable. That's why I answered with this possibility. – randers Oct 14 '15 at 22:50