Java Color Brighten setBrightness(Color color, float brightness)

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

Description

Returns a new color equal to the old one, except the brightness is set to the new value.

License

Open Source License

Parameter

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

Exception

Parameter Description
IllegalArgumentException if brightness is not between 0 and 1 inclusive
NullPointerException if color is null

Return

a new brightness-applied Color

Declaration

public static Color setBrightness(Color color, float brightness) 

Method Source Code

//package com.java2s;
/*/*  w ww.  j a  v  a  2s  .c o m*/
 * #%L
 * The AIBench Plugin Manager Plugin
 * %%
 * Copyright (C) 2006 - 2016 Daniel Glez-Pe?a and Florentino Fdez-Riverola
 * %%
 * This program 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.
 * 
 * This program 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.awt.Color;

public class Main {
    /**
     * Returns a new color equal to the old one, except the brightness is set to
     * the new value. The new color will have the same alpha (transparency) as
     * the original color.
     * <p>
     * The color is modified 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 a new brightness-applied {@code Color}
     * @throws IllegalArgumentException
     *             if {@code brightness} is not between 0 and 1 inclusive
     * @throws NullPointerException
     *             if {@code color} is {@code null}
     */
    public static Color setBrightness(Color color, float brightness) {
        if (brightness < 0f || brightness > 1f) {
            throw new IllegalArgumentException("invalid brightness value");
        }

        int alpha = color.getAlpha();

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

        return setAlpha(c, alpha);
    }

    /**
     * Returns a new color equal to the old one, except alpha (transparency)
     * channel is set to the new value.
     * 
     * @param color
     *            the color to modify
     * @param alpha
     *            the new alpha (transparency) level. Must be an int between 0
     *            and 255
     * @return a new alpha-applied {@code Color}
     * @throws IllegalArgumentException
     *             if {@code alpha} is not between 0 and 255 inclusive
     * @throws NullPointerException
     *             if {@code color} is {@code null}
     */
    public static Color setAlpha(Color color, int alpha) {
        if (alpha < 0 || alpha > 255) {
            throw new IllegalArgumentException("invalid alpha value");
        }

        return new Color(color.getRed(), color.getGreen(), color.getBlue(),
                alpha);
    }
}

Related

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