本文介绍了如何在Java中将get.rgb(x,y)整数像素转换为Color(r,g,b,a)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从get.rgb(x,y)的整数像素,但我没有任何线索如何将其转换为RGBA。例如,-16726016应为颜色(0,200,0,255)。任何提示?

I have the integer pixel i got from get.rgb(x,y), but i dont have any clue about how to convert it to RGBA. 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 r = (argb)&0xFF;
int g = (argb>>8)&0xFF;
int b = (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.

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;

这篇关于如何在Java中将get.rgb(x,y)整数像素转换为Color(r,g,b,a)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:29