Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. var webColors = GetConstants(typeof(Color));
  2. var sysColors = GetConstants(typeof(SystemColors));
  3.  
  4. static List<Color> GetConstants(Type enumType)
  5. {
  6. MethodAttributes attributes = MethodAttributes.Static | MethodAttributes.Public;
  7. PropertyInfo[] properties = enumType.GetProperties();
  8. List<Color> list = new List<Color>();
  9. for (int i = 0; i < properties.Length; i++)
  10. {
  11. PropertyInfo info = properties[i];
  12. if (info.PropertyType == typeof(Color))
  13. {
  14. MethodInfo getMethod = info.GetGetMethod();
  15. if ((getMethod != null) && ((getMethod.Attributes & attributes) == attributes))
  16. {
  17. object[] index = null;
  18. list.Add((Color)info.GetValue(null, index));
  19. }
  20. }
  21. }
  22. return list;
  23. }
  24.  
  25. var webColors = GetConstants(typeof(Color));
  26. var sysColors = GetConstants(typeof(SystemColors));
  27.  
  28. webColors.Sort(new StandardColorComparer());
  29. sysColors.Sort(new SystemColorComparer());
  30.  
  31. class StandardColorComparer : IComparer<Color>
  32. {
  33. // Methods
  34. public int Compare(Color color, Color color2)
  35. {
  36. if (color.A < color2.A)
  37. {
  38. return -1;
  39. }
  40. if (color.A > color2.A)
  41. {
  42. return 1;
  43. }
  44. if (color.GetHue() < color2.GetHue())
  45. {
  46. return -1;
  47. }
  48. if (color.GetHue() > color2.GetHue())
  49. {
  50. return 1;
  51. }
  52. if (color.GetSaturation() < color2.GetSaturation())
  53. {
  54. return -1;
  55. }
  56. if (color.GetSaturation() > color2.GetSaturation())
  57. {
  58. return 1;
  59. }
  60. if (color.GetBrightness() < color2.GetBrightness())
  61. {
  62. return -1;
  63. }
  64. if (color.GetBrightness() > color2.GetBrightness())
  65. {
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. }
  71.  
  72. class SystemColorComparer : IComparer<Color>
  73. {
  74. // Methods
  75. public int Compare(Color color, Color color2)
  76. {
  77. return string.Compare(color.Name, color2.Name, false, CultureInfo.InvariantCulture);
  78. }
  79. }
  80.  
  81. var webColors =
  82. Enum.GetValues(typeof(KnownColor))
  83. .Cast<KnownColor>()
  84. .Where (k => k >= KnownColor.Transparent && k < KnownColor.ButtonFace) //Exclude system colors
  85. .Select(k => Color.FromKnownColor(k));
  86.  
  87. .OrderBy(c => c.GetHue())
  88. .ThenBy(c => c.GetSaturation())
  89. .ThenBy(c => c.GetBrightness());
  90.  
  91. var colors = Enum.GetValues(typeof(KnownColor))
  92. .Cast<KnownColor>()
  93. .Select(kc => Color.FromKnownColor(kc))
  94. .OrderBy(c => c.GetHue())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement