Java Color Interpolate interpolate(Color low, Color high, double min, double max, double v)

Here you can find the source of interpolate(Color low, Color high, double min, double max, double v)

Description

Linear interpolation between two colors.

License

Open Source License

Parameter

Parameter Description
low Color to associate with the min value
high Color to associate with the max value
min Smallest value that will be passed
max Largest value that will be passed
v Current value

Declaration

public static Color interpolate(Color low, Color high, double min, double max, double v) 

Method Source Code


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

import java.awt.Color;

public class Main {
    /**Linear interpolation between two colors.
     * /*  w  ww  . j a v a 2 s  . c  o m*/
     * @param low Color to associate with the min value
     * @param high Color to associate with the max value
     * @param min Smallest value that will be passed
     * @param max Largest value that will be passed
     * @param v Current value 
     * **/
    public static Color interpolate(Color low, Color high, double min, double max, double v) {
        if (v > max) {
            v = max;
        }
        if (v < min) {
            v = min;
        }
        double distance = 1 - ((max - v) / (max - min));
        if (Double.isNaN(distance) || Double.isInfinite(distance)) {
            return high;
        }
        int r = (int) weightedAverage(high.getRed(), low.getRed(), distance);
        int g = (int) weightedAverage(high.getGreen(), low.getGreen(), distance);
        int b = (int) weightedAverage(high.getBlue(), low.getBlue(), distance);
        int a = (int) weightedAverage(high.getAlpha(), low.getAlpha(), distance);
        return new java.awt.Color(r, g, b, a);
    }

    /**Weighted average between two values
     * 
     * @param min The lowest value to expect
     * @param max The highest value to expect
     * @param p The desired percentage offset between max and min 
     * @return The resulting value
     */
    public static double weightedAverage(double min, double max, double p) {
        return (min - max) * p + max;
    }
}

Related

  1. interpolate(Color a, Color b, float f)
  2. interpolate(Color b, Color a, float t)
  3. interpolate(Color c1, Color c2, float fracFromC1)
  4. interpolate(Color colour1, Color colour2, float colour2Fraction)
  5. interpolate(Color start, Color end, float p)
  6. interpolate(double factor, Color bottomCol, Color topCol)
  7. interpolate(double x, double y, float[] colorLL, float[] colorLR, float[] colorUL, float[] colorUR)
  8. interpolate(final Color aBaseColor, final Color aSecondaryColor, final float aDelta)