Java Color Brightness Get brightness(int red, int green, int blue)

Here you can find the source of brightness(int red, int green, int blue)

Description

brightness

License

Open Source License

Declaration

static int brightness(int red, int green, int blue) 

Method Source Code

//package com.java2s;
/*/*from  www .  j  a  va  2  s.com*/
 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

public class Main {
    static final float XmRED_LUMINOSITY = 0.30f;
    static final float XmGREEN_LUMINOSITY = 0.59f;
    static final float XmBLUE_LUMINOSITY = 0.11f;
    static final int XmINTENSITY_FACTOR = 75;
    static final int XmLIGHT_FACTOR = 0;
    static final int XmLUMINOSITY_FACTOR = 25;

    static int brightness(int red, int green, int blue) {
        float brightness;
        float intensity;
        float light;
        float luminosity, maxprimary, minprimary;

        // To mimix Motif logic, we need to convert to 16 bit color values.

        red = red << 8;
        green = green << 8;
        blue = blue << 8;

        intensity = (red + green + blue) / 3;

        /*
         * The casting nonsense below is to try to control the point at
         * the truncation occurs.
         */

        luminosity = (int) ((XmRED_LUMINOSITY * (float) red)
                + (XmGREEN_LUMINOSITY * (float) green) + (XmBLUE_LUMINOSITY * (float) blue));

        maxprimary = ((red > green) ? ((red > blue) ? red : blue)
                : ((green > blue) ? green : blue));

        minprimary = ((red < green) ? ((red < blue) ? red : blue)
                : ((green < blue) ? green : blue));

        light = (minprimary + maxprimary) / 2;

        brightness = ((intensity * XmINTENSITY_FACTOR)
                + (light * XmLIGHT_FACTOR) + (luminosity * XmLUMINOSITY_FACTOR)) / 100;
        return Math.round(brightness);
    }
}

Related

  1. brightness(int color)
  2. brightness(int rgb)