Convert a color to an RGBA value. - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Convert a color to an RGBA value.

Demo Code

// The MIT License(MIT)

public class Main{
    /**// w w  w .  j a v a2  s  . com
     * <p>
     * Convert a color to an RGBA value.
     * </p>
     *
     * <p>
     * The resulting value contains the red, green, blue, and alpha component.
     * Bits 0-7 are blue, bits 8-15 are green, bits 16-23 are red, and bits 24-31 are alpha.
     * </p>
     *
     * @param color The color to convert.
     * @return The {@code color} represented as a single integer.
     */
    public static int colorToARGB(Color color) {
        int r = Math.round(255 * color.getRed());
        int g = Math.round(255 * color.getGreen());
        int b = Math.round(255 * color.getBlue());
        int a = Math.round(255 * color.getAlpha());

        a = (a << 25) & 0xFF000000;
        r = (r << 16) & 0x00FF0000;
        g = (g << 8) & 0x0000FF00;
        b = b & 0x000000FF;

        return a | r | g | b;
    }
}

Related Tutorials