Java Color Lighten lighter(Color c, float factor)

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

Description

Returns a lighter 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 white) inclusive

Return

a lighter (closer to white) version of the given color

Declaration

public static Color lighter(Color c, float factor) 

Method Source Code


//package com.java2s;
/*//from  www  . ja  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 lighter version of the given color by the given
     * factor.
     * <p>
     * Compared with {@code Color.brighter()}, this method provides finer
     * control over how much to lighten the given color.
     * <p>
     * Note: When {@code factor &gt; 0}, this method differs functionally from
     * {@code Color.brighter()} - repeated invocations of the former for any
     * color will eventually yield white, whereas the latter will just create a
     * brighter version of the same color.  For "pure" colors (zero r, g, or b),
     * repeated invocations of {@code Color.brighter()} will never yield white.
     *
     * @param       c
     *          the subject color
     *
     * @param       factor
     *          a number between 0 (no change) and 1 (resulting color will
     *          be white) inclusive
     *
     * @return       a lighter (closer to white) version of the given color
     *
     * @exception   IllegalArgumentException
     *          if factor is not betwee 0 and 1 inclusive
     */
    public static Color lighter(Color c, float factor) {
        return blend(c, Color.WHITE, 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. lightenColor(Color col, float factor)
  2. lightenColor(Color color)
  3. lighter(Color c)
  4. lighter(Color c)
  5. lighter(Color c, boolean transparant)
  6. lighter(Color clr)
  7. lighter(Color clr, double saturationFraction)
  8. lighter(Color color, double fraction)
  9. lighter(Color color, float ratio)