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

Java examples for 2D Graphics:Color RGB

Description

Get the 32 bit grayscale rgb value of gray (0-255) ignoring alpha

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int gray = 2;
        System.out.println(getGrayRGB(gray));
    }//from w w  w . j a  va  2s . c  o  m

    /**
     * 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