Here you can find the source of interpolate(Color low, Color high, double min, double max, double v)
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 |
public static Color interpolate(Color low, Color high, double min, double max, double v)
//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; } }