如果我有一个Color
对象,如何将其RGB值转换为十六进制整数?我一直在寻找年龄,而我发现的只是“十六进制到RGB”,或者它不返回整数值,或者其他我不想要的东西。
我希望它以十六进制形式返回int
值,而不是字符串或其他形式。有人可以帮忙吗?
这是我的代码,我需要使用某人的答案尝试将颜色转换为十六进制:
public static void loadImageGraphics(BufferedImage image, int x, int y, int width, int height) {
for(int yy = 0; yy < height; yy++) {
for(int xx = 0; xx < width; xx++) {
Color c = new Color(image.getRGB(xx, yy));
pixels[x + y * width] = c.getRed() * (0xFF)^2 + c.getGreen() * 0xFF + c.getBlue();
}
}
}
谢谢!
最佳答案
这个实用程序功能对我来说很好:
public static String convertColorToHexadeimal(Color color)
{
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
if(hex.length() < 6)
{
if(hex.length()==5)
hex = "0" + hex;
if(hex.length()==4)
hex = "00" + hex;
if(hex.length()==3)
hex = "000" + hex;
}
hex = "#" + hex;
return hex;
}
关于java - 将RGB转换为十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21227759/