2013-11-20 83 views
0

我试图创建一个组合框在那里我可以选择一种颜色(这是后来用在画线的坐标图)创建一个漂亮的调色板

我打aroung一点来是这样的:

 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); 
        } 
       } 
      } 
     } 

,这使得这样的事情在这里:

enter image description here

正如你可以看到我有颜色对,它看起来有点不可思议,不有人对此有更好的想法吗? (我用WPF)

+3

考虑按色调而不是RGB值对颜色进行排序。这样,颜色之间的转换就不那么震撼了。 –

+0

这看起来很奇怪吗? –

+1

使用[Extended WPF Toolkit ColorPicker](http://wpftoolkit.codeplex.com/wikipage?title=ColorPicker)怎么样? – DamenEU

回答

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); 
    } 

如果不看足够美观,尝试排列​​,GetSaturationGetBrightness调用,直到你很高兴。