Get the gray value using the luminance(brightness) formula intensity = 0.2989*red + 0.5870*green + 0.1140*blue - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Get the gray value using the luminance(brightness) formula intensity = 0.2989*red + 0.5870*green + 0.1140*blue

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int rgb = 2;
        System.out.println(getGray(rgb));
    }//  w  w w . j  av a 2  s . co  m

    /**
     * Get the gray value using the luminance(brightness) formula
     * intensity = 0.2989*red + 0.5870*green + 0.1140*blue
     * 
     * @param rgb
     * @return
     */
    public static int getGray(int rgb) {
        int r = (int) (((rgb >> 16) & 255) * 0.2989);
        int g = (int) (((rgb >> 8) & 255) * 0.5870);
        int b = (int) ((rgb & 255) * 0.1140);
        return (r + g + b);
    }
}

Related Tutorials