Java Color Blend blend(Color cFrom, Color cTo, float factor)

Here you can find the source of blend(Color cFrom, Color cTo, float factor)

Description

Blends the given colors by the given factor.

License

Open Source License

Parameter

Parameter Description
cFrom the color to blend with cTo
cTo the color to blend with cFrom
factor a number between 0 (resulting color will be cFrom ) and 1 (resulting color will be cTo ) inclusive, with .5 indicating an even blend between the two

Return

a blend of the given colors

Declaration

public static Color blend(Color cFrom, Color cTo, float factor) 

Method Source Code


//package com.java2s;
/*/*w ww . j av  a  2 s  .c om*/
 * 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 {
    /**
     * 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. blend(Color a, Color b, double val)
  2. blend(Color c0, Color c1)
  3. blend(Color c1, Color c2)
  4. blend(Color c1, Color c2)
  5. blend(Color c1, Color c2, double v)
  6. blend(Color col1, Color col2, float weight1)
  7. blend(Color color1, Color color2, float ratio)
  8. blend(Color color1, Color color2, int factor)
  9. blend(Color origin, Color over)