Java Color Darker darker(Color c, float factor)

Here you can find the source of darker(Color c, float factor)

Description

Returns a darker version of the given color by the given factor.

License

Open Source License

Parameter

Parameter Description
c the subject color
factor a number between 0 (no change) and 1 (resulting color will be black) inclusive

Return

a darker (closer to black) version of the given color

Declaration

public static Color darker(Color c, float factor) 

Method Source Code


//package com.java2s;
/*/*  w  ww. j a v a 2  s.com*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.awt.*;

public class Main {
    /**
     * Returns a darker version of the given color by the given factor.
     * <p>
     * Compared with {@code Color.darker()}, this method provides finer
     * control over how much to darken the given color.
     *
     * @param       c
     *          the subject color
     *
     * @param       factor
     *          a number between 0 (no change) and 1 (resulting color will
     *          be black) inclusive
     *
     * @return       a darker (closer to black) version of the given color
     *
     * @exception   IllegalArgumentException
     *          if factor is not betwee 0 and 1 inclusive
     */
    public static Color darker(Color c, float factor) {
        return blend(c, Color.BLACK, factor);
    }

    /**
     * Blends the given colors by the given factor.
     *
     * @param       cFrom
     *          the color to blend with cTo
     *
     * @param       cTo
     *          the color to blend with cFrom
     *
     * @param       factor
     *          a number between 0 (resulting color will be {@code cFrom})
     *          and 1 (resulting color will be {@code cTo}) inclusive, with
     *          .5 indicating an even blend between the two
     *
     * @return       a blend of the given colors
     *
     * @exception   IllegalArgumentException
     *          if factor is not betwee 0 and 1 inclusive
     */
    public static Color blend(Color cFrom, Color cTo, float factor) {
        if (factor < 0f || factor > 1f) {
            throw new IllegalArgumentException("factor not between 0 and 1: " + factor);
        }

        float[] rgbaFrom = cFrom.getRGBComponents(null);
        float[] rgbaTo = cTo.getRGBComponents(null);

        rgbaFrom[0] += (rgbaTo[0] - rgbaFrom[0]) * factor;
        rgbaFrom[1] += (rgbaTo[1] - rgbaFrom[1]) * factor;
        rgbaFrom[2] += (rgbaTo[2] - rgbaFrom[2]) * factor;

        return new Color(rgbaFrom[0], rgbaFrom[1], rgbaFrom[2], rgbaFrom[3]);
    }
}

Related

  1. darker(Color c)
  2. darker(Color c)
  3. darker(Color c, double factor)
  4. darker(Color c, double p)
  5. darker(Color c, double p)
  6. darker(Color col, double FACTOR)
  7. darker(Color color)
  8. darker(Color color)
  9. darker(Color color, double factor)