How can i generate 16 color. my starter color is "Red" and my terminal color "khaki". i have to insert 14 color. But it looks like gradient flow. Forexample color.Black does not come from red. Violent should come red from red.
+7
A:
You should be able to interpolate? This example is winforms, but the maths is identical - simply that with ASP.NET you'd have to write the color in the hex form. You may also (with ASP.NET) need to find the RGB values separately - but for info, Khaki (in winforms) is {240,230,140} (red is obviously {255,0,0}).
using System.Drawing;
using System.Windows.Forms;
static class Program {
static void Main()
{
Form form = new Form();
Color start = Color.Red, end = Color.Khaki;
for (int i = 0; i < 16; i++)
{
int r = Interpolate(start.R, end.R, 15, i),
g = Interpolate(start.G, end.G, 15, i),
b = Interpolate(start.B, end.B, 15, i);
Button button = new Button();
button.Dock = DockStyle.Top;
button.BackColor = Color.FromArgb(r, g, b);
form.Controls.Add(button);
button.BringToFront();
}
Application.Run(form);
}
static int Interpolate(int start, int end, int steps, int count)
{
float s = start, e = end, final = s + (((e - s) / steps) * count);
return (int)final;
}
}
Marc Gravell
2009-06-12 12:33:27
+1 for the sensible answer but I get the feeling the guy is working with preset colours from an enum, hence the colour.Violet thingy.
Ed Woodcock
2009-06-12 12:34:38
Every thing is ok. But Color too near each other. How to expand it. i add this color in a chart they does not near each other...
Phsika
2009-06-12 12:49:00
But i need 16 color!!! thanks again....
Phsika
2009-06-12 13:27:01
The example does show 16 colours; you said "gradient" - which implies that successive colors *will* be fairly close (unless you have only a few bands, and a big difference in colors) - so.... what did you mean?
Marc Gravell
2009-06-12 13:30:16