Java RGB Color Convert To rgbToGray(int colorIn)

Here you can find the source of rgbToGray(int colorIn)

Description

Change an RGB color to a gray value.

License

Open Source License

Declaration

static public int rgbToGray(int colorIn) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static final int BLUE_MASK = 0xff;
    public static final int GREEN_MASK = 0xff00;
    public static final int RED_MASK = 0xff0000;
    public static final int GREEN_OFFSET = 8;
    public static final int RED_OFFSET = 16;

    /** Change an RGB color to a gray value.
     * // w  w  w  .j av  a2  s .  co m
     * Based on the perceived contribution to the brightness. 
     *  */
    static public int rgbToGray(int colorIn) {
        int red = (colorIn & RED_MASK) >> RED_OFFSET; //red
        int green = (colorIn & GREEN_MASK) >> GREEN_OFFSET; //green
        int blue = colorIn & BLUE_MASK; //blue
        double brightness = 0.3 * red + 0.59 * green + 0.11 * blue;
        return (int) brightness;
    }
}

Related

  1. rgbToBgr(final byte[] pixels)
  2. RGBtoBGR(String color)
  3. rgbToBlackWhite(int pix, int threshold)
  4. rgbToColor(int r, int g, int b)
  5. rgbToDecimal(int r, int g, int b)
  6. rgbToGray(int pixels)
  7. RgbToGray(int r, int g, int b)
  8. rgbToGrey(byte[] rgb)
  9. rgbToHex(final int r, final int g, final int b)