java 将 int 颜色转换为 int 组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4360592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:02:32  来源:igfitidea点击:

convert int color to int components

javacolors

提问by Kamil

I obtain pixel color by

我通过以下方式获得像素颜色

int color = image.getRGB(x,y);

then i want to acquire red, green, blue components separately. How to do that? Maybe using some bitmask?

然后我想分别获取红色,绿色,蓝色组件。怎么做?也许使用一些位掩码?

int green = color&0x00ff00;

apparently not working... :(

显然不起作用... :(

回答by Alex Kucherenko

To get color components you can use:

要获取颜色组件,您可以使用:

import android.graphics.Color;

        int r = Color.red(intColor);
        int g = Color.green(intColor);
        int b = Color.blue(intColor);
        int a = Color.alpha(intColor);

回答by zezba9000

int value = image.getRGB(x,y);
R = (byte)(value & 0x000000FF);
G = (byte)((value & 0x0000FF00) >> 8);
B = (byte)((value & 0x00FF0000) >> 16);
A = (byte)((value & 0xFF000000) >> 24);

May need to flip the R, A, or B around.

可能需要翻转 R、A 或 B。

回答by codymanix

You forgot to shift the byte to the right:

您忘记将字节向右移动:

int green = (color & 0x00ff00) >> 8;

回答by khachik

You can use Colorconstructorand pass the given integer and hasalpha=true:

您可以使用Color构造函数并传递给定的整数和hasalpha=true

Color color = new Color(image.getRGB(x,y), true);

getRGBreturns the color of type TYPE_INT_ARGBwhich means it has an alpha channel.

getRGB返回TYPE_INT_ARGB类型的颜色,这意味着它有一个 alpha 通道。