我有一个像素的红色、绿色和蓝色值。如何将它们转换为RGB格式以创建新图像?基本上,我需要一个相反的过程:
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
我有一个像素的红色、绿色和蓝色值。如何将它们转换为RGB格式以创建新图像?基本上,我需要一个相反的过程:
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
int rgb = ((r << 16) | ((g << 8) | b);
r,g和b
的值都在0-255的范围内。使用 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。