Talk about alpha values in Color

  java

Order

This article mainly introduces the alpha value in java Color object.

alpha

java/awt/Color.java

/**
     * Creates an opaque sRGB color with the specified red, green,
     * and blue values in the range (0 - 255).
     * The actual color used in rendering depends
     * on finding the best match given the color space
     * available for a given output device.
     * Alpha is defaulted to 255.
     *
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>
     *        or <code>b</code> are outside of the range
     *        0 to 255, inclusive
     * @param r the red component
     * @param g the green component
     * @param b the blue component
     * @see #getRed
     * @see #getGreen
     * @see #getBlue
     * @see #getRGB
     */
    public Color(int r, int g, int b) {
        this(r, g, b, 255);
    }

    /**
     * Creates an sRGB color with the specified red, green, blue, and alpha
     * values in the range (0 - 255).
     *
     * @throws IllegalArgumentException if <code>r</code>, <code>g</code>,
     *        <code>b</code> or <code>a</code> are outside of the range
     *        0 to 255, inclusive
     * @param r the red component
     * @param g the green component
     * @param b the blue component
     * @param a the alpha component
     * @see #getRed
     * @see #getGreen
     * @see #getBlue
     * @see #getAlpha
     * @see #getRGB
     */
    @ConstructorProperties({"red", "green", "blue", "alpha"})
    public Color(int r, int g, int b, int a) {
        value = ((a & 0xFF) << 24) |
                ((r & 0xFF) << 16) |
                ((g & 0xFF) << 8)  |
                ((b & 0xFF) << 0);
        testColorValueRange(r,g,b,a);
    }

If color in java does not specify alpha, the default value is 255, which means there is no transparency.

opacity

Alpha in a color object actually refers to opacity, and its value range is 0-255. The larger the value, the more opaque it is.
It usually corresponds to opacity, which is the opacity of the semantic expression of a word. Its value range is [0,1.0f]. The larger the value, the more opaque it is.

Mapping of opacity to alpha

The main relationships between opacity and alpha are listed below.

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

How does this escape, as follows

int alpha = Math.round(opacity * 255);

The int is then output as a hexadecimal representation

        String hex = Integer.toHexString(alpha).toUpperCase();
        if (hex.length() == 1){
            hex = "0" + hex;
        }

Less than two forward not zero

doc