IllegalArgumentException: More than one component per pixel

Ich tried to get the alpha-value from a certain pixel of a
BufferedImage with:



int alpha = drawLayer.getColorModel().getAlpha(rgb);	

drawLayer is the BufferedImage and rgb the int value of
the actual pixel.
But then

appears.
How can I get the alpha value properly ?
At first I tried to get it with


int alpha = (new Color(rgb)).getAlpha();

but that returns always the same value, independent on
the actual alpha value which is in the BufferedImage.

I don’t know whether it qualifies as “properly”, but

(rgb >> 24) & 0xff

If you wondered why
int alpha = (new Color(rgb)).getAlpha();
didn’t work, either read the javadocs or use
int alpha = (new Color(rgb, true)).getAlpha();

Thank you Riven,
my program works fine now :slight_smile:
The second one


int alpha = (new Color(rgb, true)).getAlpha();

did the job.
I already looked into the javadocs, but
what I found was only
that


int alpha = (new Color(rgb)).getAlpha();

isn’t right and the ColorModel as I wrote earlier…
@ pjt33: Thank you, too, but
I’m not really into the hexadecimal stuff and shifts, so
the other solution is the better one for me :wink:
Still, I will keep that in mind.

what pjt33 said is exactly the same as what my example code does under the hood, except that his code is a zillion times more efficient.

That is what methods are for ;D
Doing things the easy way without knowing what they really do…
I think the performance will not dramatically decrease when I use
the method, but it’s always good to know :slight_smile: