Search code examples
javacolorstransparencyopacity

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


Solution

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