Here you can find the source of darken(final Color color, final double percentage)
Parameter | Description |
---|---|
color | The color to darken. |
percentage | How much darker 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% darker than the original color, so, if the original color is RGB(200, 100, 80), the new one will be RGB(100, 500, 40). |
public static Color darken(final Color color, final double percentage)
//package com.java2s; /*// w w w.j a v a 2s. 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> darker than the original. * <p/> * The original color is not modified in the process. * * @param color The color to darken. * @param percentage How much darker 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% darker than the original color, so, * if the original color is RGB(200, 100, 80), the new one will be RGB(100, 500, 40). * @return The new, darken color. */ public static Color darken(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 = (int) (r * (1.00 - percentage)); g = (int) (g * (1.00 - percentage)); b = (int) (b * (1.00 - percentage)); return new Color(r, g, b, color.getAlpha()); } }