我正在尝试创建一个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)
答案 0 :(得分: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);
}
如果这看起来不够美观,请尝试置换GetHue
,GetSaturation
和GetBrightness
来电,直到您满意为止。