Java examples for 2D Graphics:Color Blend
Blends the given colors by the given factor.
/*/* w ww. j ava 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 */ //package com.java2s; 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]); } }