Java Color Merge mergeColors(Color a, float fa, Color b, float fb)

Here you can find the source of mergeColors(Color a, float fa, Color b, float fb)

Description

Merges two colors.

License

Open Source License

Declaration

public static Color mergeColors(Color a, float fa, Color b, float fb) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   www  .  ja v  a 2 s  . c o  m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.awt.Color;

public class Main {
    /**
     * Merges two colors. The two floating point arguments specify "how much" of the corresponding color is added to the
     * resulting color. Both arguments should (but don't have to) add to <code>1.0</code>.
     * <p>
     * This method is null-safe. If one of the given colors is <code>null</code>, the other color is returned (unchanged).
     */
    public static Color mergeColors(Color a, float fa, Color b, float fb) {
        if (a == null) {
            return b;
        }
        if (b == null) {
            return a;
        }
        return new Color((fa * a.getRed() + fb * b.getRed()) / (fa + fb) / 255f,
                (fa * a.getGreen() + fb * b.getGreen()) / (fa + fb) / 255f,
                (fa * a.getBlue() + fb * b.getBlue()) / (fa + fb) / 255f);
    }
}

Related

  1. mergeColor(Color a, Color b)
  2. mergeColor(Color c1, Color c2)
  3. mergeColors(Color color1, Color color2)
  4. mergeCols(Color col1, Color col2)