Shades a colour by the given amount (0-1). - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Shades a colour by the given amount (0-1).

Demo Code

/*//from   w  w w . jav  a 2 s .  co m
 * This file is part of Creative Application Framework (CAF).
 *
 * The Core Application Framework 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 3 of the
 * License, or (at your option) any later version.
 *
 * The Core Application Framework 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 Core Application Framework.  If not, 
 * see <http://www.gnu.org/licenses/>.
 * 
 */
import java.awt.Color;
import org.apache.log4j.Logger;

public class Main{
    /**
     * Shades a colour by the given amount (0-1). A positive value will lighten the colour whilst a negative value
     * will darken the colour.The colour is transform from RGB to HSB where the amount is added to the brightness and
     * subtracted the saturation
     * @param color The color to shade
     * @param amount The amount to shade by
     * @return
     */
    public static Color shade(final Color color, final float amount) {

        float[] hsb = Color.RGBtoHSB(color.getRed(), color.getGreen(),
                color.getBlue(), null);

        return Color.getHSBColor(hsb[0], Math.max(0f, hsb[1] - amount), // decrease saturation
                Math.min(1f, hsb[2] + amount)); // increase brightness

    }
}

Related Tutorials