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;
///
/// Compares two ColorBgra instance to determine if they are equal.
///
public override bool Equals(object obj)
{
if (obj != null && obj is ColorBgra && ((ColorBgra)obj).Bgra == this.Bgra)
{
return true;
}
else
{
return false;
}
}
///
/// Returns a hash code for this color value.
///
///
public override int GetHashCode()
{
unchecked
{
return (int)Bgra;
}
}
///
/// Creates a new ColorBgra instance with the given color and alpha values.
///
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;
}
///
/// Packs color and alpha values into a 32-bit integer.
///
public static UInt32 BgraToUInt32(byte b, byte g, byte r, byte a)
{
return (uint)b + ((uint)g << 8) + ((uint)r << 16) + ((uint)a << 24);
}
///
/// Constructs a new ColorBgra instance with the given 32-bit value.
///
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;
}
///
/// Casts a ColorBgra to a UInt32.
///
public static explicit operator UInt32(ColorBgra color)
{
return color.Bgra;
}
///
/// Compares two ColorBgra instance to determine if they are equal.
///
public static bool operator ==(ColorBgra lhs, ColorBgra rhs)
{
return lhs.Bgra == rhs.Bgra;
}
///
/// Compares two ColorBgra instance to determine if they are not equal.
///
public static bool operator !=(ColorBgra lhs, ColorBgra rhs)
{
return lhs.Bgra != rhs.Bgra;
}
///
/// Casts a UInt32 to a ColorBgra.
///
public static explicit operator ColorBgra(UInt32 uint32)
{
return ColorBgra.FromUInt32(uint32);
}
///
/// Converts this ColorBgra instance to a Color instance.
///
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);
}
}
///
/// Constructs a new ColorBgra instance from the values in the given Color instance.
///
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();
}
}
}