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

Java examples for 2D Graphics:Color RGB

Description

Convert a color to an RGB value.

Demo Code

// The MIT License(MIT)

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

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

        return 0xFF000000 | r | g | b;
    }
}

Related Tutorials