Java Color Mix mixOver(Color backColor, Color frontColor)

Here you can find the source of mixOver(Color backColor, Color frontColor)

Description

Mix 2 colors with priority color

License

Open Source License

Declaration

public static Color mixOver(Color backColor, Color frontColor) 

Method Source Code


//package com.java2s;
/*//from   w w w. j  ava 2  s .  c o m
 * Copyright 2010, 2011 Institut Pasteur.
 * 
 * This file is part of ICY.
 * 
 * ICY is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ICY 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

public class Main {
    /**
     * Mix 2 colors with priority color
     */
    public static Color mixOver(Color backColor, Color frontColor) {
        final int r, g, b, a;

        final float frontAlpha = frontColor.getAlpha() / 255f;
        final float invAlpha = 1f - frontAlpha;

        r = (int) ((backColor.getRed() * invAlpha) + (frontColor.getRed() * frontAlpha));
        g = (int) ((backColor.getGreen() * invAlpha) + (frontColor.getGreen() * frontAlpha));
        b = (int) ((backColor.getBlue() * invAlpha) + (frontColor.getBlue() * frontAlpha));
        a = Math.max(backColor.getAlpha(), frontColor.getAlpha());

        return new Color(r, g, b, a);
    }
}

Related

  1. mixColors(Color a, Color b, double r)
  2. mixColors(Color c)
  3. mixColors(List colors)
  4. mixColorWithAlpha(Color base, Color mix)
  5. mixedColor(Color originalColor, Color overlayColor)
  6. mixWith(Paint paint, Color mix)