[英]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)
。 有小費嗎?
如果我猜對了,您得到的是形式為0xAARRGGBB
的無符號整數,因此
int b = (argb)&0xFF;
int g = (argb>>8)&0xFF;
int r = (argb>>16)&0xFF;
int a = (argb>>24)&0xFF;
將提取顏色分量。 但是,快速瀏覽一下文檔說,您可以
Color c = new Color(argb);
要么
Color c = new Color(argb, true);
如果您也想要顏色中的alpha分量。
更新
紅色和藍色成分在原始答案中是相反的,因此正確答案將是:
int r = (argb>>16)&0xFF;
int g = (argb>>8)&0xFF;
int b = (argb>>0)&0xFF;
在第一段代碼中也進行了更新
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());
打印出:
0
200
0
255
你是這個意思嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.