Advertisement
Guest User

Color struct

a guest
Jan 2nd, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. public struct Color
  2. {
  3.     public byte A, R, G, B;
  4.     public const uint White = 0x000000FF;
  5.     public const uint Green = 0x00FF00FF;
  6.     public const uint Red = 0xFF0000FF;
  7.     public const uint Blue = 0x0000FFFF;
  8.  
  9.     /// <summary>
  10.     /// Creates a Color structure from the four ARGB component (alpha, red, green, and blue) values.
  11.     /// </summary>
  12.     /// <param name="a">The Alpha (opacity) value of the color.</param>
  13.     /// <param name="r">The Red value of the color.</param>
  14.     /// <param name="g">The Green value of the color.</param>
  15.     /// <param name="b">The Blue value of the color.</param>
  16.     public Color(float a, float r, float g, float b)
  17.     {
  18.         A = (byte)(a * 255);
  19.         R = (byte)(r * 255);
  20.         G = (byte)(g * 255);
  21.         B = (byte)(b * 255);
  22.     }
  23.  
  24.     /// <summary>
  25.     /// Creates a Color structure from the four ARGB component (alpha, red, green, and blue) values.
  26.     /// </summary>
  27.     /// <param name="a">The Alpha (opacity) value of the color.</param>
  28.     /// <param name="r">The Red value of the color.</param>
  29.     /// <param name="g">The Green value of the color.</param>
  30.     /// <param name="b">The Blue value of the color.</param>
  31.     public Color(byte a, byte r, byte g, byte b)
  32.     {
  33.         A = a;
  34.         R = r;
  35.         G = g;
  36.         B = b;
  37.     }
  38.  
  39.     public override string ToString()
  40.     {
  41.         return "0x" + string.Format("{0:x}", (uint)this).ToUpper();
  42.     }
  43.  
  44.     public static implicit operator uint(Color color)
  45.     {
  46.         return System.BitConverter.ToUInt32(new byte[] { color.A, color.B, color.G, color.R }, 0);
  47.     }
  48.  
  49.     public static implicit operator Color(uint uColor)
  50.     {
  51.         Color color = new Color();
  52.         byte[] bytes = System.BitConverter.GetBytes(uColor);
  53.         color.A = bytes[0];
  54.         color.B = bytes[1];
  55.         color.G = bytes[2];
  56.         color.R = bytes[3];
  57.         return color;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement