如何在Java中将getRGB(x,y)整数像素转换为Color(r,g,b,a)? [英] How to convert getRGB(x,y) integer pixel to Color(r,g,b,a) in Java?
问题描述
我有从getRGB(x,y)
获得的整数像素,但是我不知道如何将其转换为RGBA格式.例如,-16726016
应该为Color(0,200,0,255)
.有提示吗?
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?
推荐答案
如果我猜对了,您得到的是形式为0xAARRGGBB
的无符号整数,因此
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);
或
Color c = new Color(argb, true);
如果您还要在颜色中使用alpha分量.
if you want the alpha component in the Color as well.
更新
红色和蓝色成分在原始答案中是倒置的,因此正确答案将是:
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;
在第一段代码中也进行了更新
这篇关于如何在Java中将getRGB(x,y)整数像素转换为Color(r,g,b,a)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!