Java Color Mix mix(Color c1, Color c2, double f)

Here you can find the source of mix(Color c1, Color c2, double f)

Description

Mix two colors by a given amount

License

GNU General Public License

Declaration

public static Color mix(Color c1, Color c2, double f) 

Method Source Code

//package com.java2s;
/*/*www .  j a  va2 s.  c  om*/
 * Copyright (C) 2015  University of Oregon
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Apache License, as specified in the LICENSE file.
 *
 * For more information, see the LICENSE file.
 */

import java.awt.*;

public class Main {
    /**
     * Mix two colors by a given amount  
     */
    public static Color mix(Color c1, Color c2, double f) {
        if (f >= 1.0)
            return c2;

        if (f <= 0)
            return c1;

        double r1 = c1.getRed();
        double g1 = c1.getGreen();
        double b1 = c1.getBlue();
        double r2 = c2.getRed();
        double g2 = c2.getGreen();
        double b2 = c2.getBlue();
        double r = f * r2 + (1 - f) * r1;
        double g = f * g2 + (1 - f) * g1;
        double b = f * b2 + (1 - f) * b1;

        Color c3 = new Color(Math.min((int) (r), 255), Math.min((int) (g),
                255), Math.min((int) (b), 255));

        return c3;
    }
}

Related

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