gpt4 book ai didi

java - 从 RGB 创建颜色可提供与原始 RGB 不同的颜色

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:30 27 4
gpt4 key购买 nike

我需要根据像素的 RGB 颜色创建一个 Color 对象(读取 PNG 文件的 BufferedImage 对象,BufferedImage 颜色空间为 BufferedImage.TYPE_4BYTE_ABGR)。但是,某些颜色存在问题,请参阅下面的代码和输出。我猜这是某种色彩空间问题,但我不知道如何解决它):感谢您的任何提示!

// imagine some great loop for x,y around the following code
int color = myImage.getRGB(x, y);

Color c = new Color(myImage.getRGB(x, y));

if(c.getRGB() != color)
System.out.println("fail " + color + " vs " + c.getRGB());

输出:

fail -116782582 vs -16119286
fail 0 vs -16777216
fail 117440511 vs -1
fail -1090519040 vs -16777216
fail 1488435127 vs -4737097
fail -1090453247 vs -16711423

还有更多。如果有帮助:例如透明度变成黑色。我猜新的 Color 对象使用的是 BufferedImage 之外的另一个颜色空间,但我不知道如何为新的 Color 对象设置颜色空间?或者 RGB 信息不足以重新创建颜色?

最佳答案

我认为问题在于你丢弃了颜色中的 alpha 信息。我认为您需要执行以下操作:

int color = myImage.getRGB(x, y);
ColorModel model = myImage.getColorModel();
Color c = new Color(color, model.hasAlpha());

关于java - 从 RGB 创建颜色可提供与原始 RGB 不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10503955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com