将像素值转换为RGB格式

3

我有一个像素的红色、绿色和蓝色值。如何将它们转换为RGB格式以创建新图像?基本上,我需要一个相反的过程:

int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
2个回答

5
int rgb = ((r << 16) | ((g << 8) | b);

假设这是RGB888格式。请确保r,g和b的值都在0-255的范围内。

0

使用 java.awt.Color 类是一个很好的实践。
这将使事情变得更简单更容易。在你的情况下,应该是:

Color myColor = new Color(red, green, blue); //Construct the color
myColor.getRGB(); //Get the RGB of the constructed color

或者反过来:

Color myColor = new Color(rgb); //Construct the color with the RGB value
myColor.getRed(); //Get the separate components of the constructed color
myColor.getGreen();
myColor.getBlue();

如需更多信息,请参阅Javadocs


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接