blend Alpha - Java 2D Graphics

Java examples for 2D Graphics:Color Blend

Description

blend Alpha

Demo Code

/*//ww w  .  j  a  va  2 s .com
 * This code 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 2.1 of the License, or (at your option) any later version.
 *
 * This code 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 this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 * MA  02111-1307, USA.
 */
//package com.java2s;
import java.awt.Color;

public class Main {
    public static Color blendAlpha(Color under, Color over) {
        float rgb1[] = new float[3];
        float rgb2[] = new float[3];

        under.getColorComponents(rgb1);
        over.getColorComponents(rgb2);
        int ualpha = under.getAlpha();
        int oalpha = over.getAlpha();
        float u = ualpha / 255f;
        float o = oalpha / 255f;
        u *= u * o;
        Color color = new Color(rgb1[0] * u + rgb2[0] * o, rgb1[1] * u
                + rgb2[1] * o, rgb1[2] * u + rgb2[2] * o);
        return color;
    }
}

Related Tutorials