Possible Duplicate:
Color Interpolation Between 3 Colors in .NET
I have been trying to get a categories list of colors using C#:
Red:
255, 69, 0
255, 99, 71
etc..
Green:
0, 250, 154
143, 188, 139
etc...
So far I have been pretty unsuccessful. Ideally, what I'd like is a way to supply two HEX refs or RGB refs and get a list back of say 10 colors between those two references. Is this possible in C#?
EDIT
Found this... http://meyerweb.com/eric/tools/color-blend/ just converting the js to c# now. Will post when its done.
There is no informational difference between RGB and HEX colors; they are simply different ways of communicating the same thing – a red, green, and blue color value. HEX, along with its sister RGB, is one of the color languages used in coding, such as CSS.
In order to measure the difference between two colors, the difference is assigned to a distance within the color space. In an equidistant-method color space, the color difference ∆E can be determined from the distance between the color places: ΔE = √ (L*₁-L*₂)² + (a*₁-a*₂)² + (b*₁-b*₂)².
The typical approach to averaging RGB colors is to add up all the red, green, and blue values, and divide each by the number of pixels to get the components of the final color.
I am not aware of built-in function which would help you, but you can do it yourself.
As long as color can be defined using 3 numbers (R,G,B), you can take two colors:
(R1,G1,B1)
(R2,G2,B2)
Then divide diff between pairs and produce numbers by intervals.
int numberOfIntervals = 10; //or change to whatever you want.
var interval_R = (R2 - R1) / numberOfIntervals;
var interval_G = (G2 - G1) / numberOfIntervals;
var interval_B = (B2 - B1) / numberOfIntervals;
var current_R = R1;
var current_G = G1;
var current_B = B1;
for (var i = 0; i <= numberOfIntervals; i++)
{
var color = Color.FromRGB(current_R, current_G, current_B);
//do something with color.
//increment.
current_R += interval_R;
current_G += interval_G;
current_B += interval_B;
}
I haven't compiled the code, but you get the idea.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With