任何人都可以向我指出一个参考图表,其中包含 System.Drawing.Color 中表示的所有颜色的样本吗?
来自维基百科的“Web Colors - X11 颜色名称”:
http://en.wikipedia.org/wiki/Web_colors
http://en.wikipedia.org/wiki/X11_color_names
WPF 颜色
Windows Presentation Foundation (WPF) 中的颜色名称与 Microsoft .NET Framework、Windows Forms 和 Microsoft Internet Explorer 中的颜色名称相匹配。这些颜色及其名称基于 UNIX X11 颜色值。
http://msdn.microsoft.com/en-us/library/system.windows.media.brushes.aspx
这是一个 WPF 示例,它生成 @StayOnTarget 在其答案中使用的列表:
MainWindow.xaml
<Window
x:Class="Colors.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Colors"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
>
<Grid>
<ListBox ItemsSource="{Binding Colors}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Rectangle Width="23" Height="23">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Path=Value.Color}" />
</Rectangle.Fill>
</Rectangle>
<TextBlock
Text="{Binding Path=Value}"
FontFamily="Consolas"
FontSize="16"
VerticalAlignment="Center"
Margin="5 0 0 0" />
<TextBlock
Text="{Binding Path=Name}"
FontFamily="Consolas"
FontSize="16"
FontWeight="Bold"
VerticalAlignment="Center"
Margin="5 0 0 0" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Media;
namespace Colors
{
public partial class MainWindow : Window
{
public class ColorInfo
{
public string Name { get; set; } = string.Empty;
public SolidColorBrush Value { get; set; } = default!;
}
public IEnumerable<ColorInfo> Colors => typeof(Brushes)
.GetProperties()
.Where(info => info.PropertyType == typeof(SolidColorBrush))
.Select(info => new ColorInfo() {
Name = info.Name,
Value = (SolidColorBrush)info.GetValue(info, null)!
})
.ToList()! ?? Enumerable.Empty<ColorInfo>();
public MainWindow()
{
InitializeComponent();
DataContext = this; // Needed for binding the ListBox to Colors
}
}
}