Mixes two colour by the provided percentages - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Mixes two colour by the provided percentages

Demo Code

/*//  www  .ja v a2 s. c  o  m
 * This file is part of Creative Application Framework (CAF).
 *
 * The Core Application Framework 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 3 of the
 * License, or (at your option) any later version.
 *
 * The Core Application Framework 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 Core Application Framework.  If not, 
 * see <http://www.gnu.org/licenses/>.
 * 
 */
import java.awt.Color;
import org.apache.log4j.Logger;

public class Main{
    /**
     * Mixes two colour by the provided percentages
     * @param color1
     * @param percentageColor1
     * @param color2
     * @param percentageColor2
     * @return
     */
    public static Color getMixedColor(Color color1, float percentageColor1,
            Color color2, float percentageColor2) {
        float[] clr1 = color1.getComponents(null);
        float[] clr2 = color2.getComponents(null);
        for (int i = 0; i < clr1.length; i++) {
            clr1[i] = (clr1[i] * percentageColor1)
                    + (clr2[i] * percentageColor2);
        }
        return new Color(clr1[0], clr1[1], clr1[2], clr1[3]);
    }
}

Related Tutorials