Java Color Interpolate interpolate(final Color from, final Color to, final double t)

Here you can find the source of interpolate(final Color from, final Color to, final double t)

Description

Interpolates between two colors.

License

Open Source License

Parameter

Parameter Description
from The color to interpolate from.
to The color to interpolate to.
t The interpolation value from <code>0</code> to <code>1</code>.

Return

The interpolated color.

Declaration

public static Color interpolate(final Color from, final Color to, final double t) 

Method Source Code

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

import java.awt.Color;

public class Main {
    /**// w w w . j ava  2s. c om
     * Interpolates between two colors.
     * 
     * @param from The color to interpolate from.
     * @param to The color to interpolate to.
     * @param t The interpolation value from <code>0</code> to <code>1</code>.
     * @return The interpolated color.
     */
    public static Color interpolate(final Color from, final Color to, final double t) {
        if (Double.isNaN(t))
            throw new IllegalArgumentException("NaN");
        if (t > 1 || t < 0)
            throw new IllegalArgumentException("" + t);
        final float[] fromRGBA = new float[4];
        final float[] toRGBA = new float[4];
        from.getRGBComponents(fromRGBA);
        to.getRGBComponents(toRGBA);
        final double r = fromRGBA[0] * (1 - t) + toRGBA[0] * t;
        final double g = fromRGBA[1] * (1 - t) + toRGBA[1] * t;
        final double b = fromRGBA[2] * (1 - t) + toRGBA[2] * t;
        final double a = fromRGBA[3] * (1 - t) + toRGBA[3] * t;
        return new Color((float) r, (float) g, (float) b, (float) a);
    }
}

Related

  1. interpolate(Color low, Color high, double min, double max, double v)
  2. interpolate(Color start, Color end, float p)
  3. interpolate(double factor, Color bottomCol, Color topCol)
  4. interpolate(double x, double y, float[] colorLL, float[] colorLR, float[] colorUL, float[] colorUR)
  5. interpolate(final Color aBaseColor, final Color aSecondaryColor, final float aDelta)
  6. interpolate(java.awt.Color a, java.awt.Color b, float portion)
  7. interpolate2Color(final Color first, final Color second, float fraction)
  8. interpolateColor(Color color0, Color color1, double mixer, boolean useHue)
  9. interpolateColor(final Color minColor, final Color maxColor, final int currentClass, final int numOfClasses)