Gets a color composed of the specified channels. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Gets a color composed of the specified channels.

Demo Code

/**/*from   ww w  .j av  a2  s  .  c  o m*/
 * Utility class to deal with ARGB color format
 *
 * @author Aritz Lopez
 * @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int alpha = 2;
        int red = 2;
        int green = 2;
        int blue = 2;
        System.out.println(getColor(alpha, red, green, blue));
    }

    /**
     * The shifting used to put the alpha component in position
     */
    public static final int ALPHA_SHIFT = 24;
    /**
     * The shifting used to put the red component in position
     */
    public static final int RED_SHIFT = 16;
    /**
     * The shifting used to put the green component in position
     */
    public static final int GREEN_SHIFT = 8;

    /**
     * Gets a color composed of the specified channels. All values must be between 0 and 255, both inclusive
     *
     * @param alpha The alpha channel [0-255]
     * @param red   The red channel [0-255]
     * @param green The green channel [0-255]
     * @param blue  The blue channel [0-255]
     * @return The color composed of the specified channels
     */
    public static int getColor(final int alpha, final int red,
            final int green, final int blue) {
        return (alpha << ALPHA_SHIFT) | (red << RED_SHIFT)
                | (green << GREEN_SHIFT) | blue;
    }
}

Related Tutorials