Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

image - How to convert getRGB(x,y) integer pixel to Color(r,g,b,a) in Java?

I have the integer pixel I got from getRGB(x,y), but I don't have any clue about how to convert it to RGBA format. For example, -16726016 should be Color(0,200,0,255). Any tips?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If I'm guessing right, what you get back is an unsigned integer of the form 0xAARRGGBB, so

int b = (argb)&0xFF;
int g = (argb>>8)&0xFF;
int r = (argb>>16)&0xFF;
int a = (argb>>24)&0xFF;

would extract the color components. However, a quick look at the docs says that you can just do

Color c = new Color(argb);

or

Color c = new Color(argb, true);

if you want the alpha component in the Color as well.

UPDATE

Red and Blue components are inverted in original answer, so the right answer will be:

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

updated also in the first piece of code


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...