Java Color Brighten adjustBrightness(Color c, float difference)

Here you can find the source of adjustBrightness(Color c, float difference)

Description

Increases/decreases brightness of the given color by the specified difference.

License

Open Source License

Parameter

Parameter Description
c color to adjust
difference value to be added to the current brightness

Exception

Parameter Description
IllegalArgumentException if difference is outside of the range -1.0 to 1.0, inclusive

Return

a new Color instance with increased/decreased brightness by specified difference

Declaration

public static Color adjustBrightness(Color c, float difference) 

Method Source Code

//package com.java2s;
/**//from  w ww .  ja v  a  2 s .  c om
 * License Agreement.
 *
 *  JBoss RichFaces - Ajax4jsf Component Library
 *
 * Copyright (C) 2007  Exadel, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

import java.awt.Color;

public class Main {
    /**
     * Increases/decreases brightness of the given color by the specified <code>difference</code>.
     * <p>
     * The <code>difference</code> values in the range (-1.0, 1.0): 1.0 - the brightest value; -1.0 - the dimmest value.
     *
     * @param c color to adjust
     * @param difference value to be added to the current brightness
     *
     * @return a new <code>Color</code> instance with increased/decreased brightness by specified <code>difference</code>
     * @throws IllegalArgumentException if difference is outside of the range -1.0 to 1.0, inclusive
     */
    public static Color adjustBrightness(Color c, float difference) {
        if (difference < -1.0 || difference > 1.0) {
            throw new IllegalArgumentException("Difference parameter outside of expected range: "
                    + "Difference parameter should be floating-point values between -1 and 1");
        }

        Color retVal = null;
        if (c != null) {
            float[] hsb = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
            float brightness = Math.min(1.0f, Math.max(0.0f, hsb[2] + difference));
            retVal = new Color(Color.HSBtoRGB(hsb[0], hsb[1], brightness));
        }

        return retVal;
    }
}

Related

  1. adjustColorBrightness(Color base, float brightness)
  2. adjustHSB(Color inputColor, float hue, float saturation, float brightness)
  3. brighten(Color c, double f)
  4. brighten(Color color)