Java Color Darker darken(final Color color, final double percentage)

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

Description

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

License

Open Source License

Parameter

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).

Return

The new, darken color.

Declaration

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

Method Source Code


//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());
    }
}

Related

  1. darken(Color c, double f)
  2. darken(Color col)
  3. darken(final Color color, final int amount)
  4. darkenColor(Color color)
  5. darker(Color c)
  6. darker(Color c)