Java Color Blend blend(Color a, Color b)

Here you can find the source of blend(Color a, Color b)

Description

blend

License

Open Source License

Declaration

public static Color blend(Color a, Color b) 

Method Source Code


//package com.java2s;
/*/*  ww w .j a va2s.  com*/
 * Copyright (C) 2012 CyborgDev <cyborg@alta189.com>
 *
 * This file is part of Cyborg
 *
 * Cyborg 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.
 *
 * Cyborg 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.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.*;

public class Main {
    public static Color blend(Color a, Color b) {
        int red = lerp(a.getRed(), b.getRed(), (a.getAlpha() / 255.0));
        int blue = lerp(a.getBlue(), b.getBlue(), (a.getAlpha() / 255.0));
        int green = lerp(a.getGreen(), b.getGreen(), (a.getAlpha() / 255.0));
        int alpha = lerp(a.getAlpha(), b.getAlpha(), (a.getAlpha() / 255.0));
        return new Color(red, green, blue, alpha);
    }

    /**
     * Calculates the linear interpolation between a and b with the given
     * percent
     * @param a
     * @param b
     * @param percent
     * @return
     */
    public static double lerp(double a, double b, double percent) {
        return (1 - percent) * a + percent * b;
    }

    /**
     * Calculates the linear interpolation between a and b with the given
     * percent
     * @param a
     * @param b
     * @param percent
     * @return
     */
    public static float lerp(float a, float b, float percent) {
        return (1 - percent) * a + percent * b;
    }

    /**
     * Calculates the linear interpolation between a and b with the given
     * percent
     * @param a
     * @param b
     * @param percent
     * @return
     */
    public static int lerp(int a, int b, double percent) {
        return (int) ((1 - percent) * a + percent * b);
    }

    /**
     * Calculates the linear interpolation between a and b with the given
     * percent
     * @param a
     * @param b
     * @param percent
     * @return
     */
    public static Color lerp(Color a, Color b, double percent) {
        int red = lerp(a.getRed(), b.getRed(), percent);
        int blue = lerp(a.getBlue(), b.getBlue(), percent);
        int green = lerp(a.getGreen(), b.getGreen(), percent);
        int alpha = lerp(a.getAlpha(), b.getAlpha(), percent);
        return new Color(red, green, blue, alpha);
    }
}

Related

  1. alphaBlend(Color o, int a)
  2. alphaBlend(final Color src, final Color dst)
  3. blend(Color a, Color b, double val)
  4. blend(Color c0, Color c1)
  5. blend(Color c1, Color c2)
  6. blend(Color c1, Color c2)