Get the gray value by taking the average R, G and B values grey = (red + green + blue)/3 - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Get the gray value by taking the average R, G and B values grey = (red + green + blue)/3

Demo Code


//package com.java2s;

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

    /**
     * Get the gray value by taking the average R, G and B values
     * grey = (red + green + blue)/3
     * 
     * @param rgb
     * @return
     */
    public static int getGrayAvg(int rgb) {
        int r = (rgb >> 16) & 255;
        int g = (rgb >> 8) & 255;
        int b = rgb & 255;
        return (r + g + b) / 3;
    }
}

Related Tutorials