【问题标题】:Create a nice color palette创建一个漂亮的调色板
【发布时间】:2013-11-20 14:38:22
【问题描述】:

我正在尝试创建一个 ComboBox,我可以在其中选择一种颜色(之后用于为图表绘制线条)

我玩过一些类似这样的事情:

        int interval = 120;

        for (int red = 0; red < 255; red += interval)
        {
            for (int green = 0; green < 255; green += interval)
            {
                for (int blue = 0; blue < 255; blue += interval)
                {
                    if (red > 150 | blue > 150 | green > 150 ) //to make sure color is not too dark
                    {
                        ComboBoxItem item = new ComboBoxItem();
                        item.Background = new SolidColorBrush(Color.FromArgb(255, (byte)(red), (byte)(green), (byte)(blue)));
                        item.Content = "#FF" + red.ToString("X2") + green.ToString("X2") + blue.ToString("X2");
                        cmbColors.Items.Add(item);
                    }
                }
            }
        }

这里是这样的:

如您所见,我有颜色对,看起来有点奇怪,有人对此有更好的主意吗? (我使用 wpf)

【问题讨论】:

  • 考虑按色调而不是按其 RGB 值对颜色进行排序。这样颜色之间的过渡就不会那么刺耳了。
  • 这有什么奇怪的地方?
  • @SriramSakthivel,据我所知,询问某人是否是色盲并不粗鲁,但如果您认为是的话,我们深表歉意。你能看到绿色/蓝色中间的紫色吗?或者黄色“跳”到粉红色,而不是逐渐消失的方式?这就是我们所说的。
  • @Sheridan 我的第一语言不是英语,这可能是我认为它令人反感的原因。非易失性存储器。那么 op 的意思是他们只是混淆了对吗?

标签: c# wpf colors


【解决方案1】:

您的要求是主观的(“有点奇怪”并不是一个准确措辞的问题!)但按色调对它们进行排序看起来像这样:

    int interval = 120;

    List<Color> colors = new List<Color>();
    for (int red = 0; red < 255; red += interval)
    {
        for (int green = 0; green < 255; green += interval)
        {
            for (int blue = 0; blue < 255; blue += interval)
            {
                if (red > 150 | blue > 150 | green > 150 ) //to make sure color is not too dark
                {
                    colors.Add(Color.FromARGB(Color.FromArgb(255, (byte)(red), (byte)(green), (byte)(blue));
                }
            }
         }
     }
     var sortedColors = colors.OrderBy(c => c.GetHue())
                              .ThenBy(c => c.GetSaturation())
                              .ThenBy(c => c.GetBrightness());
     foreach (Color c in sortedColors)
     {                        
         ComboBoxItem item = new ComboBoxItem {
             Background = new SolidColorBrush(c),
             Content = string.Format("#{0:X8}", c.ToArgb())
         };
         cmbColors.Items.Add(item);
     }

如果这看起来不够美观,请尝试排列 GetHueGetSaturationGetBrightness 调用,直到您满意为止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2011-01-09
    • 2013-05-25
    • 2018-03-15
    • 1970-01-01
    • 2013-02-09
    • 2012-12-20
    相关资源
    最近更新 更多