Java Color Brighten changeBrightness(Color c, int percent)

Here you can find the source of changeBrightness(Color c, int percent)

Description

Construct a Color that is a brighter or darker version of a given color.

License

GNU General Public License

Parameter

Parameter Description
c The original color.
percent The desired percentage change in brightness.

Return

The new color.

Declaration

public static Color changeBrightness(Color c, int percent) 

Method Source Code

//package com.java2s;
/*/*from   w  w w  . j a v a  2 s . c  o m*/
 * Copyright (C) 2015  University of Oregon
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Apache License, as specified in the LICENSE file.
 *
 * For more information, see the LICENSE file.
 */

import java.awt.*;

public class Main {
    /**
     * Construct a Color that is a brighter or darker version of a given
     * color.
     * Positive percent changes make the color brighter, unless it
     * is already as bright as it can get without changing the hue.
     * A percent change of -100 will always give black.
     * @param c The original color.
     * @param percent The desired percentage change in brightness.
     * @return The new color.
     */
    public static Color changeBrightness(Color c, int percent) {
        if (c == null) {
            return c;
        }
        int r = c.getRed();
        int g = c.getGreen();
        int b = c.getBlue();
        float[] hsb = Color.RGBtoHSB(r, g, b, null);
        float fraction = (float) percent / 100;
        float remainder = (float) 0;

        hsb[2] = hsb[2] * (1 + fraction);
        if (hsb[2] < 0) {
            remainder = hsb[2];
            hsb[2] = 0;
        } else if (hsb[2] > 1) {
            // Can't make it bright enough with Brightness alone,
            // decrease Saturation.
            remainder = hsb[2] - 1;
            hsb[2] = 1;
            hsb[1] = hsb[1] * (1 - 5 * remainder);
            if (hsb[1] < 0) {
                hsb[1] = 0;
            }
        }
        return Color.getHSBColor(hsb[0], hsb[1], hsb[2]);
    }
}

Related

  1. brighterColor(Color c)
  2. brighterColor(String hexValue)
  3. brightness(Color c, float scale)
  4. brightness(java.awt.Color c)
  5. brightnessToAlpha(Image image, float alpha)
  6. changeColorBrightness(final Color color, final double delta)
  7. deriveByBrightness(Color original, Color brightnessSource)
  8. deriveColorHSB(Color base, float hue, float saturation, float brightness)
  9. getBrightness(Color c)