Color
struct (सिस्टम रंग SystemColors
कक्षा में स्थिरांक के रूप में परिभाषित कर रहे हैं)
इन रंगों की एक सूची प्राप्त करने के लिए बस करो स्थिरांक के रूप में सभी वेब रंग शामिल हैं:
var webColors = GetConstants(typeof(Color));
var sysColors = GetConstants(typeof(SystemColors));
GetConstants
के रूप में परिभाषित किया गया है:
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;
}
संपादित करें:
रंग हल कर बिल्कुल तरह वी.एस. में क्या प्राप्त करने के लिए:
var webColors = GetConstants(typeof(Color));
var sysColors = GetConstants(typeof(SystemColors));
webColors.Sort(new StandardColorComparer());
sysColors.Sort(new SystemColorComparer());
StandardColorComparer
और SystemColorComparer
साथ इस प्रकार के रूप में परिभाषित:
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. :
यह कोड System.Drawing.Design.ColorEditor
से परावर्तक के माध्यम से लिया गया है।
आप KnownColor enum की सूची सूची चाहते हैं? –
Magnus
असल में हाँ, लेकिन मैं दृश्य स्टूडियो में वेब टैब में रंगों को सॉर्ट करना चाहता हूं। मैंने कुछ एल्गोरिदम के साथ रंगों को सॉर्ट करने का प्रयास किया लेकिन परिणाम उतना अच्छा नहीं है। – Klaus78