如果我有一个 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/