You could use linear interpolation to mix the R, G and B values (and A if you want). Here's some example code for a Windows Form project. Just add a trackbar with range 0 to 400 and wire up the trackbar's scroll event to the handler below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
byte interpolate(byte a, byte b, double p)
{
return (byte)(a * (1 - p) + b * p);
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
int v = trackBar1.Value;
BackColor = getColor(v);
}
private Color getColor(int v)
{
SortedDictionary<int, Color> d = new SortedDictionary<int, Color>();
d.Add(0, Color.Blue);
d.Add(100, Color.Green);
d.Add(200, Color.Yellow);
d.Add(300, Color.Orange);
d.Add(400, Color.Red);
KeyValuePair<int, Color> kvp_previous = new KeyValuePair<int,Color>(-1, Color.Black);
foreach (KeyValuePair<int, Color> kvp in d)
{
if (kvp.Key > v)
{
double p = (v - kvp_previous.Key) / (double)(kvp.Key - kvp_previous.Key);
Color a = kvp_previous.Value;
Color b = kvp.Value;
Color c = Color.FromArgb(
interpolate(a.R, b.R, p),
interpolate(a.G, b.G, p),
interpolate(a.B, b.B, p));
return c;
}
kvp_previous = kvp;
}
return Color.Black;
}
}
You could also use this idea with HSL colors as suggested by nobugz.
Note: This code is just proof-of-concept. In a real application you would want to create a class to encapsulate the color choosing logic, make it more customizable, and error handling, etc. It's also not optimized for speed. If speed is an important consideration then you should probably use a look-up table instead.