Java Color Mix mixColorWithAlpha(Color base, Color mix)

Here you can find the source of mixColorWithAlpha(Color base, Color mix)

Description

mix Color With Alpha

License

Open Source License

Declaration

public static Color mixColorWithAlpha(Color base, Color mix) 

Method Source Code

//package com.java2s;
/*/*from  ww w. j  a v  a2s.  com*/
 *  NimbusHelper.java
 *  (SwingOSC)
 *
 *  Copyright (c) 2005-2012 Hanns Holger Rutz. All rights reserved.
 *
 *   This software 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 2, june 1991 of the License, or (at your option) any later version.
 *
 *   This software 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 (gpl.txt) along with this software; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *   For further information, please contact Hanns Holger Rutz at
 *   contact@sciss.de
 */

import java.awt.Color;

public class Main {
    public static Color mixColorWithAlpha(Color base, Color mix) {
        if (mix == null)
            return base;
        final int a0 = mix.getAlpha();
        if (a0 == 0) {
            return base;
        } else if (a0 == 0xFF)
            return mix;

        final float wm = (float) a0 / 0xFF;
        final float wb = 1f - wm;
        final int r = (int) (base.getRed() * wb + mix.getRed() * wm + 0.5f);
        final int g = (int) (base.getGreen() * wb + mix.getGreen() * wm + 0.5f);
        final int b = (int) (base.getBlue() * wb + mix.getBlue() * wm + 0.5f);
        return new Color(r, g, b);
    }
}

Related

  1. mix(int a, int b, int amt)
  2. mixColor(Paint p, double value)
  3. mixColors(Color a, Color b, double r)
  4. mixColors(Color c)
  5. mixColors(List colors)
  6. mixedColor(Color originalColor, Color overlayColor)
  7. mixOver(Color backColor, Color frontColor)
  8. mixWith(Paint paint, Color mix)