Java RGB Color Convert To rgbToGrey(byte[] rgb)

Here you can find the source of rgbToGrey(byte[] rgb)

Description

Return a grey RGB triplet corresponding to the luminance of the incoming color RGB triplet.

License

Open Source License

Parameter

Parameter Description
rgb a parameter

Declaration

public static byte[] rgbToGrey(byte[] rgb) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  .j  a  va  2  s.com
  ColorMapUtils.java
    
  (c) 2011-2013 Edward Swartz
    
  All rights reserved. This program and the accompanying materials
  are made available under the terms of the Eclipse Public License v1.0
  which accompanies this distribution, and is available at
  http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Return a grey RGB triplet corresponding to the luminance
     * of the incoming color RGB triplet.
     * @param rgb
     * @return
     */
    public static byte[] rgbToGrey(byte[] rgb) {
        byte[] g = new byte[3];
        int lum = getRGBLum(rgb);
        g[0] = g[1] = g[2] = (byte) lum;
        return g;
    }

    public static void rgbToGrey(int[] rgb, int[] g) {
        int lum = getRGBLum(rgb);
        g[0] = g[1] = g[2] = lum;
    }

    public static void rgbToGrey(byte[] rgb, byte[] g) {
        int lum = getRGBLum(rgb);
        g[0] = g[1] = g[2] = (byte) lum;
    }

    /**
     * @param rgb
     * @return
     */
    public static int getRGBLum(byte[] rgb) {
        int plum = (299 * (rgb[0] & 0xff) + 587 * (rgb[1] & 0xff) + 114 * (rgb[2] & 0xff)) / 1000;
        return plum;
    }

    /**
     * @param prgb
     * @return
     */
    public static int getRGBLum(int[] prgb) {
        int lum = (299 * (prgb[0] & 0xff) + 587 * (prgb[1] & 0xff) + 114 * (prgb[2] & 0xff)) / 1000;
        return lum;
    }
}

Related

  1. rgbToColor(int r, int g, int b)
  2. rgbToDecimal(int r, int g, int b)
  3. rgbToGray(int colorIn)
  4. rgbToGray(int pixels)
  5. RgbToGray(int r, int g, int b)
  6. rgbToHex(final int r, final int g, final int b)
  7. rgbToHex(int r, int g, int b)
  8. RGBtoHex(int r, int g, int b)
  9. rgbToHex(int[] rgb)