简体   繁体   中英

How to detect if a RGB is fully transparent?

In java, I want to make a fully transparent RGBA, and I do that by using

public static int getTransparentRGB() {
    int r = 0;
    int g = 0;
    int b = 0;
    int a = 0;
    int new_pixel = (a << 24) | (r << 16) | (g << 8) | b;
    return new_pixel;
}

    Color color = new Color(getTransparentRGB());
    System.out.println(color.getAlpha()); // -> 255  ?!

I purposely keep all rgba values 0. However after I create the Color object with the rgba value as the constructor, if I call .getAlpha() , I get 255 even though I made the rgb value with a 0 alpha. If it returns 255 , how could I tell the difference between a Color object that wasn't transparent, because that would also have a 255 alpha.

I expect the color object to return a 0 alpha based on the function above.

Does anyone know whats going on?

Thanks

You need to use the constructor Color(int, boolean) to supply the alpha value for your color. The constructor you are using changes the alpha value to 255.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM