Java Color Blend blend(final Color color1, final Color color2, final double ratio)

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

Description

blend

License

Open Source License

Declaration

public static Color blend(final Color color1, final Color color2, final double ratio) 

Method Source Code


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

import java.awt.Color;
import java.text.NumberFormat;

public class Main {
    public static Color blend(final Color color1, final Color color2, final double ratio) {
        final float r = (float) ratio;
        final float ir = (float) 1.0 - r;

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

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

        float red = rgb1[0] * r + rgb2[0] * ir;
        float green = rgb1[1] * r + rgb2[1] * ir;
        float blue = rgb1[2] * r + rgb2[2] * ir;

        if (red < 0) {
            red = 0;/*from w w w  . ja v a 2  s.  co  m*/
        } else if (red > 255) {
            red = 255;
        }
        if (green < 0) {
            green = 0;
        } else if (green > 255) {
            green = 255;
        }
        if (blue < 0) {
            blue = 0;
        } else if (blue > 255) {
            blue = 255;
        }

        Color color = null;
        try {
            color = new Color(red, green, blue);
        } catch (final IllegalArgumentException exp) {
            final NumberFormat nf = NumberFormat.getNumberInstance();
            System.out.println(nf.format(red) + "; " + nf.format(green) + "; " + nf.format(blue));
            exp.printStackTrace();
        }
        return color;
    }
}

Related

  1. blend(Color color1, Color color2, float ratio)
  2. blend(Color color1, Color color2, int factor)
  3. blend(Color origin, Color over)
  4. blend(Color pColor, Color pOther)
  5. blend(double factor, Color color1, Color color2)
  6. blend(final Color first, final Color second, final float alpha)
  7. blendAlpha(Color under, Color over)
  8. blendcol(Color in, Color bl)
  9. blendColor(Color clOne, Color clTwo, double amount)