Java Color Mix mix(Color c1, Color c2, float bias)

Here you can find the source of mix(Color c1, Color c2, float bias)

Description

Mixes two given colors.

License

Open Source License

Parameter

Parameter Description
c1 First color to mix.
c2 Second color to mix.
bias If this is near 0, c1 is used more, and if this is near 1, c2 is used more.

Return

Mix of two given colors.

Declaration

public static Color mix(Color c1, Color c2, float bias) 

Method Source Code


//package com.java2s;
/*// w  ww .  j  a va 2 s .c  o m
 * Copyright 2013  Thom Castermans  thom.castermans@gmail.com
 * Copyright 2013  Willem Sonke     willemsonke@planet.nl
 * 
 * This program 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 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy 
 * defined in Section 14 of version 3 of the license.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;

public class Main {
    /**
     * Mixes two given colors.
     * 
     * @param c1  First color to mix.
     * @param c2  Second color to mix.
     * @param bias If this is near 0, c1 is used more, and if this is near 1, c2 is used more.
     * @return Mix of two given colors.
     */
    public static Color mix(Color c1, Color c2, float bias) {
        if (bias <= 0.0)
            return c1;
        if (bias >= 1.0)
            return c2;
        if (Double.isNaN(bias))
            return c1; // special case copied from original code

        // [ws] bug fixed: /256f was missing
        float r = mixNumberFloat(c1.getRed() / 256f, c2.getRed() / 256f, bias);
        float g = mixNumberFloat(c1.getGreen() / 256f, c2.getGreen() / 256f, bias);
        float b = mixNumberFloat(c1.getBlue() / 256f, c2.getBlue() / 256f, bias);
        float a = mixNumberFloat(c1.getAlpha() / 256f, c2.getAlpha() / 256f, bias);

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

    private static float mixNumberFloat(float a, float b, float bias) {
        return a + (b - a) * bias;
    }
}

Related

  1. mix(Color c0, Color c1, float amount)
  2. mix(Color c1, Color c2)
  3. mix(Color c1, Color c2)
  4. mix(Color c1, Color c2, boolean useAlpha)
  5. mix(Color c1, Color c2, double f)
  6. mix(Color src, Color dst, double factor)
  7. mix(double x, double y, double a)
  8. mix(final Color c1, final Color c2, final double factor)
  9. mix(int a, int b, int amt)