Java Color Brighten setBrightness(Color color, float brightness)

Here you can find the source of setBrightness(Color color, float brightness)

Description

Modifies the passed in color by changing it's brightness using HSB calculations.

License

Open Source License

Parameter

Parameter Description
color the color to modify
brightness the brightness to use in the new color

Return

the new Color

Declaration

public static Color setBrightness(Color color, float brightness) 

Method Source Code


//package com.java2s;
/*// www .  j a v a 2  s  .c om
 * $Id: ColorUtil.java,v 1.6 2006/04/26 19:45:31 joshy Exp $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * 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
 */

import java.awt.Color;

public class Main {
    /**
     * 
     * Modifies the passed in color by changing it's brightness using HSB
     * calculations. The brightness must be a float between 0 and 1. If 0 the
     * resulting color will be black. If 1 the resulting color will be the brightest
     * possible form of the passed in color.
     * 
     * @param color the color to modify
     * @param brightness the brightness to use in the new color
     * @return the new Color
     */
    public static Color setBrightness(Color color, float brightness) {
        int alpha = color.getAlpha();

        float[] cols = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        cols[2] = brightness;
        Color c2 = color.getHSBColor(cols[0], cols[1], cols[2]);

        return setAlpha(c2, alpha);
    }

    /**
     * Modifies the passed in color by setting a new alpha channel (transparency)
     * and returns the new color. 
     *
     * @param col the color to modify
     * @param alpha the new alpha (transparency) level. Must be an int between 0 and 255
     *
     * @return the new Color
     */
    public static Color setAlpha(Color col, int alpha) {
        return new Color(col.getRed(), col.getGreen(), col.getBlue(), alpha);
    }
}

Related

  1. HSBtoRGB(float parHue, float parSaturation, float parBrightness)
  2. increaseBrightness(Image image)
  3. makeColorBrighter(Color color)
  4. modifyBrightness(Color c, float brightness)
  5. perceivedBrightness(Color c)
  6. setBrightness(Color color, float brightness)
  7. setColorBrightness(Color c, double y)
  8. shiftHSB(java.awt.Color color, double hueDelta, double saturationDelta, double brightnessDelta)
  9. slightlyBrighter(Color color)