【问题标题】:How to do that Gradient Color Generator?渐变色生成器怎么做?
【发布时间】:2009-06-12 12:21:00
【问题描述】:

如何生成 16 色。我的起始颜色是“红色”,我的终端颜色是“卡其色”。我必须插入 14 种颜色。但它看起来像梯度流。例如 color.Black 不是来自红色。暴力应该由红变红。

【问题讨论】:

    标签: c# .net asp.net algorithm


    【解决方案1】:

    你应该能够插值?这个例子是winforms,但数学是相同的——只是使用ASP.NET,你必须以十六进制形式编写颜色。您可能还(使用 ASP.NET)需要单独查找 RGB 值 - 但对于信息,卡其色(在 winforms 中)是 {240,230,140}(红色显然是 {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;
        }    
    }
    

    【讨论】:

    • +1 是明智的答案,但我觉得这家伙正在使用枚举中的预设颜色,因此 color.Violet 东西。
    • 一切正常。但是颜色太接近了。如何扩展它。我在图表中添加了这种颜色,它们彼此并不靠近...
    • 该示例确实显示了 16 种颜色;您说的是“渐变”-这意味着连续的颜色相当接近(除非您只有几条带,并且颜色差异很大)-所以....您的意思是什么?
    猜你喜欢
    • 2010-11-19
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多