When you have RGB input but need a gray result. - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

When you have RGB input but need a gray result.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int red = 2;
        int green = 2;
        int blue = 2;
        System.out.println(blueOrRgbToGray(red, green, blue));
    }/* w ww.j a  v  a 2s  .  co m*/

    /** When you have RGB input but need a gray result.
     *
     * If only the blue is set use that, otherwise do normal color transform.
     * Based on the perceived contribution to the brightness.
     *  */
    static public int blueOrRgbToGray(int red, int green, int blue) {
        if (0 == red && 0 == green)
            return blue;
        return (30 * red + 59 * green + 11 * blue) / 100;
    }
}

Related Tutorials