当前位置:   article > 正文

java 像素转rgb,如何在Java中将getRGB(x,y)整数像素转换为Color(r,g,b,a)?...

java color 转 rgb

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?

解决方案

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/67504
推荐阅读
相关标签
  

闽ICP备14008679号