Java Color Brighten brighten(final Color color, final double percentage)

Here you can find the source of brighten(final Color color, final double percentage)

Description

Returns a new color that is percentage brighter than the original.

License

Open Source License

Parameter

Parameter Description
color The color to brighten.
percentage How much brighter the new color must be. The value must be between [0.01 and 1.00] inclusive. Example: a value of 0.50 means a new color 50% brighter than the original color, so, if the original color is RGB(150, 70, 60), the new one will be RGB(255, 140, 120). Be aware that if the brighten rate surpasses a channel max value (= 255) this channel will bew cut down to this max, changing this way the color gama (deforming the color).

Return

The new, brighten color.

Declaration

public static Color brighten(final Color color, final double percentage) 

Method Source Code


//package com.java2s;
/*// www.  j  ava 2 s.c o  m
 * Copyright (C) 2013 Marcius da Silva da Fonseca.
 *
 * 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 Street, Fifth Floor, Boston,
 * MA 02110-1301 USA
 */

import java.awt.Color;

public class Main {
    /**
     * Returns a new color that is <tt>percentage</tt> brighter than the original.
     * <p/>
     * The original color is not modified in the process.
     *
     * @param color      The color to brighten.
     * @param percentage How much brighter the new color must be.
     *                   The value must be between [0.01 and 1.00] inclusive.
     *                   Example: a value of 0.50 means a new color 50% brighter than the original color, so,
     *                   if the original color is RGB(150, 70, 60), the new one will be RGB(255, 140, 120).
     *                   Be aware that if the brighten rate surpasses a channel max value (= 255) this channel
     *                   will bew cut down to this max, changing this way the color gama (deforming the color).
     * @return The new, brighten color.
     */
    public static Color brighten(final Color color, final double percentage) {
        if (percentage < 0.01 || percentage > 1.00) {
            throw new IllegalArgumentException("Percentage must be between [0.01 - 1.00]");
        }
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();

        r = Math.min((int) (r * (1.00 + percentage)), 255);
        g = Math.min((int) (g * (1.00 + percentage)), 255);
        b = Math.min((int) (b * (1.00 + percentage)), 255);
        return new Color(r, g, b, color.getAlpha());
    }
}

Related

  1. adjustBrightness(Color c, float difference)
  2. adjustColorBrightness(Color base, float brightness)
  3. adjustHSB(Color inputColor, float hue, float saturation, float brightness)
  4. brighten(Color c, double f)
  5. brighten(Color color)
  6. brighten(final int color)
  7. brighten(final int cValue, double colorBrigthnessFactor)
  8. brighten(float[] rgb, float luminosity)
  9. brightenColor(Color color, double factor)