如何在 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;
将提取颜色分量.但是,快速浏览一下 docs 说你可以做
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屋!