Here you can find the source of changeBrightness(Color c, int percent)
Parameter | Description |
---|---|
c | The original color. |
percent | The desired percentage change in brightness. |
public static Color changeBrightness(Color c, int percent)
//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]); } }