Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get colors between two color HEX refs or RGB [duplicate]

Tags:

c#

.net

colors

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.

like image 231
user1238321 Avatar asked Nov 06 '12 14:11

user1238321


People also ask

Is hex more accurate than RGB?

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.

How do you find the difference between two colors?

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*₂)².

How do you average 2 RGB colors?

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.


2 Answers

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.

like image 66
Tengiz Avatar answered Nov 15 '22 02:11

Tengiz


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.

like image 32
Steven Jeuris Avatar answered Nov 15 '22 02:11

Steven Jeuris