123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- namespace SmartCoalApplication.Core
- {
- [Serializable]
- [StructLayout(LayoutKind.Explicit)]
- public struct ColorBgra
- {
- [FieldOffset(0)]
- public byte B;
- [FieldOffset(1)]
- public byte G;
- [FieldOffset(2)]
- public byte R;
- [FieldOffset(3)]
- public byte A;
- [NonSerialized]
- [FieldOffset(0)]
- public uint Bgra;
- public const int SizeOf = 4;
- /// <summary>
- /// Compares two ColorBgra instance to determine if they are equal.
- /// </summary>
- public override bool Equals(object obj)
- {
- if (obj != null && obj is ColorBgra && ((ColorBgra)obj).Bgra == this.Bgra)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- /// <summary>
- /// Returns a hash code for this color value.
- /// </summary>
- /// <returns></returns>
- public override int GetHashCode()
- {
- unchecked
- {
- return (int)Bgra;
- }
- }
- /// <summary>
- /// Creates a new ColorBgra instance with the given color and alpha values.
- /// </summary>
- public static ColorBgra FromBgra(byte b, byte g, byte r, byte a)
- {
- ColorBgra color = new ColorBgra();
- color.Bgra = BgraToUInt32(b, g, r, a);
- return color;
- }
- /// <summary>
- /// Packs color and alpha values into a 32-bit integer.
- /// </summary>
- public static UInt32 BgraToUInt32(byte b, byte g, byte r, byte a)
- {
- return (uint)b + ((uint)g << 8) + ((uint)r << 16) + ((uint)a << 24);
- }
- /// <summary>
- /// Constructs a new ColorBgra instance with the given 32-bit value.
- /// </summary>
- public static ColorBgra FromUInt32(UInt32 bgra)
- {
- ColorBgra color = new ColorBgra();
- color.Bgra = bgra;
- return color;
- }
- public override string ToString()
- {
- return "B: " + B + ", G: " + G + ", R: " + R + ", A: " + A;
- }
- /// <summary>
- /// Casts a ColorBgra to a UInt32.
- /// </summary>
- public static explicit operator UInt32(ColorBgra color)
- {
- return color.Bgra;
- }
- /// <summary>
- /// Compares two ColorBgra instance to determine if they are equal.
- /// </summary>
- public static bool operator ==(ColorBgra lhs, ColorBgra rhs)
- {
- return lhs.Bgra == rhs.Bgra;
- }
- /// <summary>
- /// Compares two ColorBgra instance to determine if they are not equal.
- /// </summary>
- public static bool operator !=(ColorBgra lhs, ColorBgra rhs)
- {
- return lhs.Bgra != rhs.Bgra;
- }
- /// <summary>
- /// Casts a UInt32 to a ColorBgra.
- /// </summary>
- public static explicit operator ColorBgra(UInt32 uint32)
- {
- return ColorBgra.FromUInt32(uint32);
- }
- /// <summary>
- /// Converts this ColorBgra instance to a Color instance.
- /// </summary>
- public Color ToColor()
- {
- return Color.FromArgb(A, R, G, B);
- }
- public static ColorBgra White
- {
- get
- {
- return ColorBgra.FromBgra(255, 255, 255, 255);
- }
- }
- public static ColorBgra Black
- {
- get
- {
- return ColorBgra.FromBgra(0, 0, 0, 255);
- }
- }
- /// <summary>
- /// Constructs a new ColorBgra instance from the values in the given Color instance.
- /// </summary>
- public static ColorBgra FromColor(Color c)
- {
- return FromBgra(c.B, c.G, c.R, c.A);
- }
- public static ColorBgra ParseHexString(string hexString)
- {
- uint value = Convert.ToUInt32(hexString, 16);
- return ColorBgra.FromUInt32(value);
- }
- public string ToHexString()
- {
- int rgbNumber = (this.R << 16) | (this.G << 8) | this.B;
- string colorString = Convert.ToString(rgbNumber, 16);
- while (colorString.Length < 6)
- {
- colorString = "0" + colorString;
- }
- string alphaString = System.Convert.ToString(this.A, 16);
- while (alphaString.Length < 2)
- {
- alphaString = "0" + alphaString;
- }
- colorString = alphaString + colorString;
- return colorString.ToUpper();
- }
- }
- }
|