Return a greyscale rgb value 0-FF using NTSC color luminance algorithm the alpha component is set to 0xFF. - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Return a greyscale rgb value 0-FF using NTSC color luminance algorithm the alpha component is set to 0xFF.

Demo Code

/* $RCSfile$//from  ww w.  jav  a 2 s. co m
 * $Author: nicove $
 * $Date: 2007-03-25 06:09:49 -0500 (Sun, 25 Mar 2007) $
 * $Revision: 7221 $
 *
 * Copyright (C) 2000-2005  The Jmol Development Team
 *
 * Contact: jmol-developers@lists.sf.net
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int rgb = 2;
        System.out.println(calcGreyscaleRgbFromRgb(rgb));
    }

    /**
     * Return a greyscale rgb value 0-FF using NTSC color luminance algorithm
     *<p>
     * the alpha component is set to 0xFF. If you want a value in the
     * range 0-255 then & the result with 0xFF;
     *
     * @param rgb the rgb value
     * @return a grayscale value in the range 0 - 255 decimal
     */
    public static int calcGreyscaleRgbFromRgb(int rgb) {
        int grey = (((2989 * ((rgb >> 16) & 0xFF))
                + (5870 * ((rgb >> 8) & 0xFF)) + (1140 * (rgb & 0xFF)) + 5000) / 10000) & 0xFFFFFF;
        return rgb(grey, grey, grey);
    }

    public static int rgb(int red, int grn, int blu) {
        return 0xFF000000 | (red << 16) | (grn << 8) | blu;
    }
}

Related Tutorials