Skip to content

Instantly share code, notes, and snippets.

@sukesh-ak
Forked from peterk87/interpolate_colors.cs
Created August 11, 2020 15:39
Show Gist options
  • Save sukesh-ak/cb5c08d299efb1f2f850cd06b8c45051 to your computer and use it in GitHub Desktop.
Save sukesh-ak/cb5c08d299efb1f2f850cd06b8c45051 to your computer and use it in GitHub Desktop.
C#: Interpolate between 2 colors - 2 different approaches taken from StackOverflow.com
// Interpolate between 2 colors in C#
// Taken from answer by user Jason
// http://stackoverflow.com/questions/1236683/color-interpolation-between-3-colors-in-net
class ColorInterpolator {
delegate byte ComponentSelector(Color color);
static ComponentSelector _redSelector = color => color.R;
static ComponentSelector _greenSelector = color => color.G;
static ComponentSelector _blueSelector = color => color.B;
public static Color InterpolateBetween(
Color endPoint1,
Color endPoint2,
double lambda) {
if (lambda < 0 || lambda > 1) {
throw new ArgumentOutOfRangeException("lambda");
}
Color color = Color.FromArgb(
InterpolateComponent(endPoint1, endPoint2, lambda, _redSelector),
InterpolateComponent(endPoint1, endPoint2, lambda, _greenSelector),
InterpolateComponent(endPoint1, endPoint2, lambda, _blueSelector)
);
return color;
}
static byte InterpolateComponent(
Color endPoint1,
Color endPoint2,
double lambda,
ComponentSelector selector) {
return (byte)(selector(endPoint1)
+ (selector(endPoint2) - selector(endPoint1)) * lambda);
}
}
// -----------------------------------------------------------------------------
// More generic function:
// Taken from Answer by Steven Jeuris
// http://stackoverflow.com/questions/13252838/get-colors-between-two-color-hex-refs-or-rgb/13253304#13253304
// What you are looking for is called interpolation. In this particular scenario you need to interpolate data between two key points.
// Since interpolation is a really common scenario when programming, I wrote a generic solution for it, easily allowing you to interpolate between two or more key points, using either linear or even cardinal spline interpolation.
// Using my library you could calculate intermediate colors as follows:
var keyPoints = new CumulativeKeyPointCollection<Color, double>(
new ColorInterpolationProvider() );
keyPoints.Add( Color.FromArgb(0, 250, 154) );
keyPoints.Add( Color.FromArgb(143, 188, 139) );
var linear = new LinearInterpolation<Color, double>( keyPoints );
// // E.g. to get a color halfway the two other colors.
// Color colorHalfway = linear.Interpolate( 0.5 );
// You would have to implement ColorInterpolationProvider by extending from AbstractInterpolationProvider<Color, double>, but this is quite straightforward, and more information can be found in my blog post.
// This example uses the Media.Color class, but you could just as well support any other Color class by passing along a different interpolation provider.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment