如何更改预定义的十六进制颜色的alpha? [英] How would one change the alpha of a predefined hexadecimal color?

查看:181
本文介绍了如何更改预定义的十六进制颜色的alpha?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个人如何改变颜色的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;
}


解决方案

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

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


I'm wondering how a person could change the alpha transparency of a color, if given the hex color code. For example if given Color.red.getRGB() how could I change it's alpha to 0x80?

To put this in context, I'm working on a static method to tint a BufferedImage, by creating a graphics device from the given image, and rendering a half transparent mask with that, disposing of the graphics, and returning the image. It works, but you have to define the alpha yourself in the given hex color code. I want to give a Color object, and double between 0 and 1.0 to determine the intensity of the tinting. Here's my code so far:

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;
}

解决方案

You can construct a new Color from the old one with the lower alpha.

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

Using the Color(int r, int g, int b, int a) constructor.

这篇关于如何更改预定义的十六进制颜色的alpha?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆