Java color opacity

42,290

Solution 1

public Color color(double val) {
    double H = val * 0.3; 
    double S = 0.9; 
    double B = 0.9; 
    int rgb = Color.HSBtoRGB((float)H, (float)S, (float)B);
    int red = (rgb >> 16) & 0xFF;
    int green = (rgb >> 8) & 0xFF;
    int blue = rgb & 0xFF;
    Color color = new Color(red, green, blue, 0x33);
    return color;
}

Solution 2

The easiest way is to specify R, G, B, and A directly, using this constructor:

public Color(float r, float g, float b, float a).

I know you have HSB, but you can convert to RGB easily enough.

Solution 3

I know this is old, but all I do when I want opacity made with the Java built-in Graphics Object, is new Color(RRR, GGG, BBB, AAA).

Used in context:

g.setColor(new Color(255, 255, 255, 80));

Last value is alpha channel/opacity, higher number means more opaque.

Solution 4

Use the Color constructor that takes a value with an alpha channel. Specifically, first you convert your color coordinates to RGB space:

int rgba = Color.HSBtoRGB(H, S, B);

and then add the desired amount of transparency,

rgba = (rgba & 0xffffff) | (alpha << 24);

where alpha is an integer between 0 (fully transparent) and 255 (fully opaque), inclusive. This value you can pass to the Color constructor, making sure to give true for the second argument.

Color c = new Color(rgba, true);
Share:
42,290
jpo
Author by

jpo

Updated on December 01, 2020

Comments

  • jpo
    jpo over 3 years

    I have a method to determine a color based on some value. The method is as such:

    public Color color(double val) {
        double H = val * 0.3; 
        double S = 0.9; 
        double B = 0.9; 
        return Color.getHSBColor((float)H, (float)S, (float)B);
    }
    

    I also want to make the color created trasparent. How can I do this? Thanks

  • jpo
    jpo almost 13 years
    Thanks, but what is rgba |= alpha << 24?
  • David Z
    David Z almost 13 years
    I just realized I misread the documentation. I've edited my answer accordingly. Anyway: the argument you pass to the color constructor combines four bytes into one integer: blue, green, red, and alpha (transparency), in that order. The HSBtoRGB method gives you an integer with the red, green, and blue bytes set correctly for the color you want, but the transparency byte will be zero (fully transparent). To make a partially transparent color, you need to set the last byte to the amount of transparency you want. alpha & 0xff does that.
  • David Z
    David Z almost 13 years
    (cont.) actually, to be more precise: alpha & 0xff takes your desired amount of transparency and then zeros everything but the last 8 bits, and rgba |= ... uses the OR operation to put those bits into the integer. For the (incorrect) code I had originally, << 24 shifts the last 8 bits over to the left by 24 bits, so that they become the first 8 bits, and then rgba |= ... again ORs those bits into the integer.
  • Mars
    Mars over 7 years
    Part of this method is illustrated in jpo's answer, but yours is the only one so far that gives crucial information about what higher/lower values of alpha mean. I upvoted because of this, but then realized that it's incorrect: a larger alpha value means the color is more opaque. I hope you'll fix this.
  • Don Hatch
    Don Hatch over 3 years
    Assuming alpha is an int in 0..255, I think your original code was correct, putting the alpha into the high-order byte: rgba |= alpha<<24. Your new version is putting the alpha into the lower-order byte, which isn't right. docs.oracle.com/en/java/javase/11/docs/api/java.desktop/java‌​/…
  • David Z
    David Z over 3 years
    Huh, thanks. Let me test it to confirm and then I'll edit accordingly.
  • David Z
    David Z over 3 years
    @DonHatch Actually it looks like neither version was right, since the alpha byte would default to 0xff, and I wasn't resetting those bits to zero before or'ing in the desired alpha value. Can you confirm if it looks correct now?
  • Don Hatch
    Don Hatch over 3 years
    Ah, you're right. I find the names of these functions unfortunate (HSBtoRGB and toRGB), seems like HSBtoRGBA and toRGBA would be clearer / less surprising. And it doesn't help that HSBtoRGB's doc says simply "in the same format used by the method of getRGB", which is technically correct but hides the surprise under layers of links that the user may not follow until there's a bug. Yes, your latest edit looks correct now, thanks!
  • Axel Carré
    Axel Carré about 3 years
    Higher number means more opaque.