user1173951
user1173951

Reputation: 69

Getting RGB value out of negative int value

i had to convert from rgb to hsb so as to perform histogram equalization on an image. I have converted it back into rgb and i am getting a negative value like -158435. Can anyone please help me understand how to convert this into a colour so i can set it to my pixel? Thanks

Upvotes: 0

Views: 4071

Answers (2)

SQL Police
SQL Police

Reputation: 4196

The negative value appears because you are storing colors as ARGB (Alpha Red Green Blue). The alpha channel is then often just 100% opaque, which is 255 = 0xFF.

Therefore, when the color is converted to an 32 bit int, it appears negative.

Example: Opaque Black = ARGB(255, 0, 0, 0) = 0xFF000000 = -16777216

Upvotes: 0

Martijn Courteaux
Martijn Courteaux

Reputation: 68857

Simply make use of the bit-shifting. It works.

int rgb = 0x00F15D49;

int r = (rgb >>> 16) & 0xFF;
int g = (rgb >>>  8) & 0xFF;
int b = rgb & 0xFF;

Then use this method Color.RGBtoHSB(int r, int g, int b, float[] hsbvals); like this:

float[] hsb = Color.RGBtoHSB(r, g, b, null);

To convert it back, simply use the other method (edited, you were right):

int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
System.out.println(Integer.toHexString(rgb));

Upvotes: 1

Related Questions