Java Color Blend blend(Color color1, Color color2, float ratio)

Here you can find the source of blend(Color color1, Color color2, float ratio)

Description

Blend two colors.

License

Open Source License

Parameter

Parameter Description
color1 First color to blend.
color2 Second color to blend.
ratio Blend ratio. 0.5 will give even blend, 1.0 will return color1, 0.0 will return color2 and so on.

Return

Blended color.

Declaration

public static Color blend(Color color1, Color color2, float ratio) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

public class Main {
    /**//from   w  w  w . j  a v  a2  s .  com
     * Blend two colors.
     *
     * @param color1 First color to blend.
     * @param color2 Second color to blend.
     * @param ratio  Blend ratio. 0.5 will give even blend, 1.0 will return
     *               color1, 0.0 will return color2 and so on.
     * @return Blended color.
     */
    public static Color blend(Color color1, Color color2, float ratio) {
        float ir = (float) 1.0 - ratio;

        float rgb1[] = new float[3];
        float rgb2[] = new float[3];

        color1.getColorComponents(rgb1);
        color2.getColorComponents(rgb2);

        return new Color(rgb1[0] * ratio + rgb2[0] * ir, rgb1[1] * ratio + rgb2[1] * ir,
                rgb1[2] * ratio + rgb2[2] * ir);
    }

    /**
     * Make an even blend between two colors.
     *
     * @param color1 First color to blend.
     * @param color2 Second color to blend.
     * @return Blended color.
     */
    public static Color blend(Color color1, Color color2) {
        return blend(color1, color2, 0.5f);
    }
}

Related

  1. blend(Color c1, Color c2)
  2. blend(Color c1, Color c2)
  3. blend(Color c1, Color c2, double v)
  4. blend(Color cFrom, Color cTo, float factor)
  5. blend(Color col1, Color col2, float weight1)
  6. blend(Color color1, Color color2, int factor)
  7. blend(Color origin, Color over)
  8. blend(Color pColor, Color pOther)
  9. blend(double factor, Color color1, Color color2)