2

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.

Penguen
  • 16,836
  • 42
  • 130
  • 205

1 Answers1

11

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    +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 James Jun 12 '09 at 12:34
  • 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... – Penguen Jun 12 '09 at 12:49
  • 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 Jun 12 '09 at 13:30