ColorBgra.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SmartCoalApplication.Core
  9. {
  10. [Serializable]
  11. [StructLayout(LayoutKind.Explicit)]
  12. public struct ColorBgra
  13. {
  14. [FieldOffset(0)]
  15. public byte B;
  16. [FieldOffset(1)]
  17. public byte G;
  18. [FieldOffset(2)]
  19. public byte R;
  20. [FieldOffset(3)]
  21. public byte A;
  22. [NonSerialized]
  23. [FieldOffset(0)]
  24. public uint Bgra;
  25. public const int SizeOf = 4;
  26. /// <summary>
  27. /// Compares two ColorBgra instance to determine if they are equal.
  28. /// </summary>
  29. public override bool Equals(object obj)
  30. {
  31. if (obj != null && obj is ColorBgra && ((ColorBgra)obj).Bgra == this.Bgra)
  32. {
  33. return true;
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }
  40. /// <summary>
  41. /// Returns a hash code for this color value.
  42. /// </summary>
  43. /// <returns></returns>
  44. public override int GetHashCode()
  45. {
  46. unchecked
  47. {
  48. return (int)Bgra;
  49. }
  50. }
  51. /// <summary>
  52. /// Creates a new ColorBgra instance with the given color and alpha values.
  53. /// </summary>
  54. public static ColorBgra FromBgra(byte b, byte g, byte r, byte a)
  55. {
  56. ColorBgra color = new ColorBgra();
  57. color.Bgra = BgraToUInt32(b, g, r, a);
  58. return color;
  59. }
  60. /// <summary>
  61. /// Packs color and alpha values into a 32-bit integer.
  62. /// </summary>
  63. public static UInt32 BgraToUInt32(byte b, byte g, byte r, byte a)
  64. {
  65. return (uint)b + ((uint)g << 8) + ((uint)r << 16) + ((uint)a << 24);
  66. }
  67. /// <summary>
  68. /// Constructs a new ColorBgra instance with the given 32-bit value.
  69. /// </summary>
  70. public static ColorBgra FromUInt32(UInt32 bgra)
  71. {
  72. ColorBgra color = new ColorBgra();
  73. color.Bgra = bgra;
  74. return color;
  75. }
  76. public override string ToString()
  77. {
  78. return "B: " + B + ", G: " + G + ", R: " + R + ", A: " + A;
  79. }
  80. /// <summary>
  81. /// Casts a ColorBgra to a UInt32.
  82. /// </summary>
  83. public static explicit operator UInt32(ColorBgra color)
  84. {
  85. return color.Bgra;
  86. }
  87. /// <summary>
  88. /// Compares two ColorBgra instance to determine if they are equal.
  89. /// </summary>
  90. public static bool operator ==(ColorBgra lhs, ColorBgra rhs)
  91. {
  92. return lhs.Bgra == rhs.Bgra;
  93. }
  94. /// <summary>
  95. /// Compares two ColorBgra instance to determine if they are not equal.
  96. /// </summary>
  97. public static bool operator !=(ColorBgra lhs, ColorBgra rhs)
  98. {
  99. return lhs.Bgra != rhs.Bgra;
  100. }
  101. /// <summary>
  102. /// Casts a UInt32 to a ColorBgra.
  103. /// </summary>
  104. public static explicit operator ColorBgra(UInt32 uint32)
  105. {
  106. return ColorBgra.FromUInt32(uint32);
  107. }
  108. /// <summary>
  109. /// Converts this ColorBgra instance to a Color instance.
  110. /// </summary>
  111. public Color ToColor()
  112. {
  113. return Color.FromArgb(A, R, G, B);
  114. }
  115. public static ColorBgra White
  116. {
  117. get
  118. {
  119. return ColorBgra.FromBgra(255, 255, 255, 255);
  120. }
  121. }
  122. public static ColorBgra Black
  123. {
  124. get
  125. {
  126. return ColorBgra.FromBgra(0, 0, 0, 255);
  127. }
  128. }
  129. /// <summary>
  130. /// Constructs a new ColorBgra instance from the values in the given Color instance.
  131. /// </summary>
  132. public static ColorBgra FromColor(Color c)
  133. {
  134. return FromBgra(c.B, c.G, c.R, c.A);
  135. }
  136. public static ColorBgra ParseHexString(string hexString)
  137. {
  138. uint value = Convert.ToUInt32(hexString, 16);
  139. return ColorBgra.FromUInt32(value);
  140. }
  141. public string ToHexString()
  142. {
  143. int rgbNumber = (this.R << 16) | (this.G << 8) | this.B;
  144. string colorString = Convert.ToString(rgbNumber, 16);
  145. while (colorString.Length < 6)
  146. {
  147. colorString = "0" + colorString;
  148. }
  149. string alphaString = System.Convert.ToString(this.A, 16);
  150. while (alphaString.Length < 2)
  151. {
  152. alphaString = "0" + alphaString;
  153. }
  154. colorString = alphaString + colorString;
  155. return colorString.ToUpper();
  156. }
  157. }
  158. }