Get the 32 bit grayscale ARGB value of gray (0-255) - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Get the 32 bit grayscale ARGB value of gray (0-255)

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int gray = 2;
        int a = 2;
        System.out.println(getGrayARGB(gray, a));
    }/*  w ww.  jav a  2  s .c om*/

    /**
     * Get the 32 bit grayscale ARGB value of gray (0-255)
     * 
     * @param gray
     * @return
     */
    public static int getGrayARGB(int gray, int a) {
        return (a << 24) + getGrayRGB(gray);
    }

    /**
     * Get the 32 bit grayscale rgb value of gray (0-255) ignoring alpha
     * 
     * @param gray
     * @return
     */
    public static int getGrayRGB(int gray) {
        return (gray << 16) + (gray << 8) + gray;
    }

    /**
     * Get the 32 bit grayscale rgb value of gray (0-1.0)
     * 
     * @param gray
     * @return
     */
    public static int getGrayRGB(double gray) {
        int val = (int) (gray * 255);
        return (val << 16) + (val << 8) + val;
    }
}

Related Tutorials