0

我想知道如果给定十六进制颜色代码,一个人如何改变颜色的 alpha 透明度。例如,如果给定 Color.red.getRGB() 我如何将它的 alpha 更改为 0x80?

为了把它放在上下文中,我正在研究一种为 BufferedImage 着色的静态方法,方法是从给定的图像创建一个图形设备,并用它渲染一个半透明的蒙版,处理图形,然后返回图像。它可以工作,但您必须自己在给定的十六进制颜色代码中定义 alpha。我想给一个 Color 对象,并在 0 和 1.0 之间加倍以确定着色的强度。到目前为止,这是我的代码:

public static Image tintImage(Image loadImg, int color) {
    Image gImage = loadImg;
    Graphics2D g = gImage.image.createGraphics();
    Image image = new Image(new BufferedImage(loadImg.width, loadImg.height, BufferedImage.TYPE_INT_ARGB));
    for(int x = 0; x < loadImg.width; x++) {
        for(int y = 0; y < loadImg.height; y++) {
            if(loadImg.image.getRGB(x, y) >> 24 != 0x00) {
                image.image.setRGB(x, y, color);
            }
        }
    }
    g.drawImage(image.image, 0, 0, null);
    g.dispose();

    return gImage;
}
4

1 回答 1

2

您可以从具有较低 alpha 的旧颜色构造新颜色。

Color cNew = new Color(cOld.getRed(), cOld.getGreen(), cOld.getBlue(), 0x80);

使用Color(int r, int g, int b, int a)构造函数。

于 2013-10-04T23:16:00.197 回答