2

Possible Duplicate:
How to convert a color integer to a hex String in Android?

int c = bitmap.getPixel(x, y);

returns a 7 or 8 digit number like -14438067 for green for example.

How do I convert it to hex or something meaningful? I tried parseColor but I get an exception "not a color..."

Community
  • 1
  • 1
124697
  • 22,097
  • 68
  • 188
  • 315
  • From http://developer.android.com/reference/android/graphics/Color.html, "Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue...The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution". – mbeckish Sep 13 '12 at 16:51

1 Answers1

1

You can use the static methods in the Color class. (Found here: http://developer.android.com/reference/android/graphics/Color.html)

You can extract individual components for red, blue, green, and alpha individually. (with the methods Color.red(int color), Color.blue(int color), Color.green(int color), and Color.alpha(int Color) respectively)

Using Integer.toString(color, 16) on individual component values will get a hex string representation of that component.

Brad S
  • 2,112
  • 1
  • 13
  • 4
  • You can also use bit shifting as the components are specified as (alpha << 24) | (red << 16) | (green << 8) | blue. (color >> 8) & 0xFF gets green as an example. I just happen to find the methods more clear to use. – Brad S Sep 13 '12 at 16:48
  • I have already got that number from getPixel() – 124697 Sep 13 '12 at 16:53
  • @user521180 If you have the individual components, then you may use Integer.toString(c, 16) to convert each to a hex string as suggested in Lukas's answer. – Brad S Sep 13 '12 at 16:56