C# getting all colors from Color
I want to make a ComboBox
filled with all the colors from System.Drawing.Color
But I can't seem to collect all the colors from that collection
I've already tried using a foreach
to do the job like this:
fo开发者_如何学JAVAreach (Color clr in Color)
{
}
But all I get is an error.
So how can I loop trough all the colors?
Any help will be appreciated.
You could take color from KnownColor
KnownColor[] colors = Enum.GetValues(typeof(KnownColor));
foreach(KnownColor knowColor in colors)
{
Color color = Color.FromKnownColor(knowColor);
}
or use reflection to avoid color like Menu, Desktop... contain in KnowColor
Type colorType = typeof(System.Drawing.Color);
// We take only static property to avoid properties like Name, IsSystemColor ...
PropertyInfo[] propInfos = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo propInfo in propInfos)
{
Console.WriteLine(propInfo.Name);
}
Similar to @madgnome’s code, but I prefer the following since it doesn’t require parsing the string names (a redundant indirection, in my opinion):
foreach (var colorValue in Enum.GetValues(typeof(KnownColor)))
Color color = Color.FromKnownColor((KnownColor)colorValue);
My way to get colors. I think it is the best way via Reflection library.
private List<Color> GetAllColors()
{
List<Color> allColors = new List<Color>();
foreach (PropertyInfo property in typeof(Color).GetProperties())
{
if (property.PropertyType == typeof(Color))
{
allColors.Add((Color)property.GetValue(null));
}
}
return allColors;
}
This is what I think you want:
foreach (Color color in new ColorConverter().GetStandardValues())
{
MessageBox.Show(color.ToString());
}
it will loop through all the standard values for color, and should work for what you need
Based on @Julien Hoarau answer what i use to lookup a color is dump them in LinqPad like this:
void Main()
{
((KnownColor[])Enum.GetValues(typeof(KnownColor)))
.Select(knowColor => System.Drawing.Color.FromKnownColor(knowColor))
.Where(color => color.R == color.R)
.ToList()
.ForEach(color =>
{
var _label = new Label();
_label.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
_label.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, (byte)~color.R, (byte)~color.G, (byte)~color.B));
_label.Content = $"{color.Name} R:{color.R} G:{color.G} B:{color.B}";
_label.Dump();
});
}
Which produces nice list like this:
The requirement was to have a list of the system colors to chose from, a list of the "web" colors, AKA the professional colors, and then RGB via R,G,B syntax, and finally the use of the color picker control for completeness.
I save the list of colors and system color properties for use later. The ReduceName(color) removes the "Color [Name]" components from the string. If you don't maintain a running list of the colors, you will have them show up twice in the second list. There is probably a more elegant approach to handling that, but time was more important than perfect, as is often the case.
_ListAllColors = new List<Color>();
_SystemColorProperties = typeof(SystemColors).GetProperties();
foreach (PropertyInfo propertyInfo in _SystemColorProperties)
{
object colorObject = propertyInfo.GetValue(null, null);
Color color = (Color)colorObject;
if (!_ListAllColors.Contains(color))
{
systemColorsComboBox.Items.Add(ReduceName(color));
_ListAllColors.Add(color);
}
}
foreach (KnownColor colorValue in Enum.GetValues(typeof(KnownColor)))
{
Color color = Color.FromKnownColor(colorValue);
if (!_ListAllColors.Contains(color))
{
professionalColorsComboBox.Items.Add(ReduceName(color));
_ListAllColors.Add(color);
}
}
var knownColors = Enum.GetValues(typeof(KnownColor))
.Cast<KnownColor>()
.Where(color => color != KnownColor.ActiveBorder
&& color != KnownColor.ActiveCaptionText
&& color != KnownColor.ActiveCaption
&& color != KnownColor.AppWorkspace
&& color != KnownColor.ButtonFace
&& color != KnownColor.ButtonHighlight
&& color != KnownColor.ButtonShadow
&& color != KnownColor.Control
&& color != KnownColor.ControlDark
&& color != KnownColor.ControlDarkDark
&& color != KnownColor.ControlLight
&& color != KnownColor.ControlLightLight
&& color != KnownColor.ControlText
&& color != KnownColor.Desktop
&& color != KnownColor.GradientActiveCaption
&& color != KnownColor.GradientInactiveCaption
&& color != KnownColor.GrayText
&& color != KnownColor.Highlight
&& color != KnownColor.HighlightText
&& color != KnownColor.HotTrack
&& color != KnownColor.InactiveBorder
&& color != KnownColor.InactiveCaption
&& color != KnownColor.InactiveCaptionText
&& color != KnownColor.Info
&& color != KnownColor.InfoText
&& color != KnownColor.Menu
&& color != KnownColor.MenuBar
&& color != KnownColor.MenuHighlight
&& color != KnownColor.MenuText
&& color != KnownColor.ScrollBar
&& color != KnownColor.Window
&& color != KnownColor.WindowFrame
&& color != KnownColor.WindowText)
.ToArray();
精彩评论