ColorStorage.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Reflection;
  4. #if MONOGAME || FNA
  5. using Microsoft.Xna.Framework;
  6. #elif STRIDE
  7. using Stride.Core.Mathematics;
  8. #else
  9. using Color = Veldrid.RgbaFloat;
  10. #endif
  11. namespace FontStashSharp.RichText
  12. {
  13. public static class ColorStorage
  14. {
  15. public class ColorInfo
  16. {
  17. public Color Color { get; set; }
  18. public string Name { get; set; }
  19. }
  20. public static readonly Dictionary<string, ColorInfo> Colors = new Dictionary<string, ColorInfo>();
  21. static ColorStorage()
  22. {
  23. var type = typeof(Color);
  24. #if !STRIDE
  25. var colors = type.GetRuntimeProperties();
  26. foreach (var c in colors)
  27. {
  28. if (c.PropertyType != typeof(Color))
  29. {
  30. continue;
  31. }
  32. var value = (Color)c.GetValue(null, null);
  33. Colors[c.Name.ToLower()] = new ColorInfo
  34. {
  35. Color = value,
  36. Name = c.Name
  37. };
  38. }
  39. #else
  40. var colors = type.GetRuntimeFields();
  41. foreach (var c in colors)
  42. {
  43. if (c.FieldType != typeof(Color))
  44. {
  45. continue;
  46. }
  47. var value = (Color)c.GetValue(null);
  48. Colors[c.Name.ToLower()] = new ColorInfo
  49. {
  50. Color = value,
  51. Name = c.Name
  52. };
  53. }
  54. #endif
  55. }
  56. public static string ToHexString(this Color c)
  57. {
  58. return string.Format("#{0}{1}{2}{3}",
  59. c.R.ToString("X2"),
  60. c.G.ToString("X2"),
  61. c.B.ToString("X2"),
  62. c.A.ToString("X2"));
  63. }
  64. public static string GetColorName(this Color color)
  65. {
  66. foreach (var c in Colors)
  67. {
  68. if (c.Value.Color == color)
  69. {
  70. return c.Value.Name;
  71. }
  72. }
  73. return null;
  74. }
  75. public static Color? FromName(string name)
  76. {
  77. if (name.StartsWith("#"))
  78. {
  79. name = name.Substring(1);
  80. uint u;
  81. if (uint.TryParse(name, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out u))
  82. {
  83. // Parsed value contains color in RGBA form
  84. // Extract color components
  85. byte r = 0, g = 0, b = 0, a = 0;
  86. unchecked
  87. {
  88. if (name.Length == 6)
  89. {
  90. r = (byte)(u >> 16);
  91. g = (byte)(u >> 8);
  92. b = (byte)u;
  93. a = 255;
  94. }
  95. else if (name.Length == 8)
  96. {
  97. r = (byte)(u >> 24);
  98. g = (byte)(u >> 16);
  99. b = (byte)(u >> 8);
  100. a = (byte)u;
  101. }
  102. }
  103. return new Color(r, g, b, a);
  104. }
  105. }
  106. else
  107. {
  108. ColorInfo result;
  109. if (Colors.TryGetValue(name.ToLower(), out result))
  110. {
  111. return result.Color;
  112. }
  113. }
  114. return null;
  115. }
  116. public static Color CreateColor(int r, int g, int b, int a = 255)
  117. {
  118. return new Color((byte)r, (byte)g, (byte)b, (byte)a);
  119. }
  120. }
  121. }