[英]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. 我有从
getRGB(x,y)
获得的整数像素,但是我对如何将其转换为RGBA格式一无所知。 For example, -16726016
should be Color(0,200,0,255)
. 例如
-16726016
应该是Color(0,200,0,255)
。 Any tips? 有小费吗?
If I'm guessing right, what you get back is an unsigned integer of the form 0xAARRGGBB
, so 如果我猜对了,您得到的是形式为
0xAARRGGBB
的无符号整数,因此
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. 如果您也想要颜色中的alpha分量。
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 在第一段代码中也进行了更新
Color c = new Color(-16726016, true);
System.out.println(c.getRed());
System.out.println(c.getGreen());
System.out.println(c.getBlue());
System.out.println(c.getAlpha());
prints out: 打印出:
0
200
0
255
Is that what you mean? 你是这个意思吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.