Color
estructura contiene todos los colores web como constantes (los colores del sistema se definen como constantes en SystemColors
clase)
Para obtener una lista de estos colores acaba de hacer:
var webColors = GetConstants(typeof(Color));
var sysColors = GetConstants(typeof(SystemColors));
teniendo GetConstants
definido de la siguiente manera:
static List<Color> GetConstants(Type enumType)
{
MethodAttributes attributes = MethodAttributes.Static | MethodAttributes.Public;
PropertyInfo[] properties = enumType.GetProperties();
List<Color> list = new List<Color>();
for (int i = 0; i < properties.Length; i++)
{
PropertyInfo info = properties[i];
if (info.PropertyType == typeof(Color))
{
MethodInfo getMethod = info.GetGetMethod();
if ((getMethod != null) && ((getMethod.Attributes & attributes) == attributes))
{
object[] index = null;
list.Add((Color)info.GetValue(null, index));
}
}
}
return list;
}
EDIT:
Para obtener colores ordenados exactamente como en VS hacer:
var webColors = GetConstants(typeof(Color));
var sysColors = GetConstants(typeof(SystemColors));
webColors.Sort(new StandardColorComparer());
sysColors.Sort(new SystemColorComparer());
con StandardColorComparer
y SystemColorComparer
definen como sigue:
class StandardColorComparer : IComparer<Color>
{
// Methods
public int Compare(Color color, Color color2)
{
if (color.A < color2.A)
{
return -1;
}
if (color.A > color2.A)
{
return 1;
}
if (color.GetHue() < color2.GetHue())
{
return -1;
}
if (color.GetHue() > color2.GetHue())
{
return 1;
}
if (color.GetSaturation() < color2.GetSaturation())
{
return -1;
}
if (color.GetSaturation() > color2.GetSaturation())
{
return 1;
}
if (color.GetBrightness() < color2.GetBrightness())
{
return -1;
}
if (color.GetBrightness() > color2.GetBrightness())
{
return 1;
}
return 0;
}
}
class SystemColorComparer : IComparer<Color>
{
// Methods
public int Compare(Color color, Color color2)
{
return string.Compare(color.Name, color2.Name, false, CultureInfo.InvariantCulture);
}
}
N. B. :
Este código ha sido tomado de System.Drawing.Design.ColorEditor
a través del reflector.
¿Desea una lista de KnownColor enum? –
Magnus
Básicamente sí, pero me gustaría que los colores se clasifiquen como en la pestaña Web en Visual Studio. He intentado ordenar los colores con algunos algoritmos pero el resultado no es tan bueno. – Klaus78