Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java color opacity

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

like image 583
jpo Avatar asked Dec 03 '22 01:12

jpo


2 Answers

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.

like image 155
Ray Toal Avatar answered Dec 07 '22 22:12

Ray Toal


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.

like image 41
user6412105 Avatar answered Dec 07 '22 22:12

user6412105