ColorExtensions.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. namespace MonoGame.Extended
  4. {
  5. /// <summary>
  6. /// Provides additional methods for working with color
  7. /// </summary>
  8. public static class ColorExtensions
  9. {
  10. /// <summary>
  11. /// Converts a <see cref="Color"/> to its hexadecimal string representation in RGBA format.
  12. /// </summary>
  13. /// <param name="color">The <see cref="Color"/> to convert.</param>
  14. /// <returns>A hexadecimal string representation of the color in the format #RRGGBBAA.</returns>
  15. public static string ToHex(this Color color)
  16. {
  17. string rx = $"{color.R:x2}";
  18. string gx = $"{color.G:x2}";
  19. string bx = $"{color.B:x2}";
  20. string ax = $"{color.A:x2}";
  21. return $"#{rx}{gx}{bx}{ax}";
  22. }
  23. [Obsolete("Use ColorHelper.FromHex instead. This will be removed in the next major SemVer release.")]
  24. public static Color FromHex(string value)
  25. {
  26. return ColorHelper.FromHex(value);
  27. }
  28. [Obsolete("Use HslColor.ToRgb instead. This will be removed in the next major SemVer update")]
  29. public static Color ToRgb(this HslColor c)
  30. {
  31. var h = c.H;
  32. var s = c.S;
  33. var l = c.L;
  34. if (s == 0f)
  35. return new Color(l, l, l);
  36. h = h/360f;
  37. var max = l < 0.5f ? l*(1 + s) : l + s - l*s;
  38. var min = 2f*l - max;
  39. return new Color(
  40. ComponentFromHue(min, max, h + 1f/3f),
  41. ComponentFromHue(min, max, h),
  42. ComponentFromHue(min, max, h - 1f/3f));
  43. }
  44. [Obsolete("This will be removed in the next major SemVer release.")]
  45. private static float ComponentFromHue(float m1, float m2, float h)
  46. {
  47. h = (h + 1f)%1f;
  48. if (h*6f < 1)
  49. return m1 + (m2 - m1)*6f*h;
  50. if (h*2 < 1)
  51. return m2;
  52. if (h*3 < 2)
  53. return m1 + (m2 - m1)*(2f/3f - h)*6f;
  54. return m1;
  55. }
  56. [Obsolete("Use HslColor.FromRgb instead. This method will be removed in the next SemVer release")]
  57. public static HslColor ToHsl(this Color c)
  58. {
  59. var r = c.R/255f;
  60. var b = c.B/255f;
  61. var g = c.G/255f;
  62. var max = Math.Max(Math.Max(r, g), b);
  63. var min = Math.Min(Math.Min(r, g), b);
  64. var chroma = max - min;
  65. var sum = max + min;
  66. var l = sum*0.5f;
  67. if (chroma == 0)
  68. return new HslColor(0f, 0f, l);
  69. float h;
  70. if (r == max)
  71. h = (60*(g - b)/chroma + 360)%360;
  72. else
  73. {
  74. if (g == max)
  75. h = 60*(b - r)/chroma + 120f;
  76. else
  77. h = 60*(r - g)/chroma + 240f;
  78. }
  79. var s = l <= 0.5f ? chroma/sum : chroma/(2f - sum);
  80. return new HslColor(h, s, l);
  81. }
  82. /// <summary>
  83. /// Returns a new <see cref="Color"/> value based on a packed value in the ABGR format.
  84. /// </summary>
  85. /// <remarks>
  86. /// This is useful for when you have HTML hex style values such as #123456 and want to use it in hex format for
  87. /// the parameter. Since Color's standard format is RGBA, you would have to do new Color(0xFF563212) since R
  88. /// is the LSB. With this method, you can write it the same way it is written in HTML hex by doing
  89. /// <c>>ColorExtensions.FromAbgr(0x123456FF);</c>
  90. /// </remarks>
  91. /// <param name="abgr">The packed color value in ABGR format</param>
  92. /// <returns>The <see cref="Color"/> value created</returns>
  93. [Obsolete("Use ColorHelper.FromAbgr instead. This will be removed in the next major SemVer release.")]
  94. public static Color FromAbgr(uint abgr)
  95. {
  96. uint rgba = (abgr & 0x000000FF) << 24 | // Alpha
  97. (abgr & 0x0000FF00) << 8 | // Blue
  98. (abgr & 0x00FF0000) >> 8 | // Green
  99. (abgr & 0xFF000000) >> 24; // Red
  100. Color result;
  101. #if FNA
  102. result = default;
  103. result.PackedValue = rgba;
  104. #else
  105. result = new Color(rgba);
  106. #endif
  107. return result;
  108. }
  109. }
  110. }