using System;
namespace PdfOxide.Geometry
{
public struct Color : IEquatable<Color>
{
public byte Red { get; }
public byte Green { get; }
public byte Blue { get; }
public byte Alpha { get; }
public Color(byte red, byte green, byte blue)
: this(red, green, blue, 255)
{
}
public Color(byte red, byte green, byte blue, byte alpha)
{
Red = red;
Green = green;
Blue = blue;
Alpha = alpha;
}
public static Color FromArgb(uint argb)
{
var a = (byte)((argb >> 24) & 0xFF);
var r = (byte)((argb >> 16) & 0xFF);
var g = (byte)((argb >> 8) & 0xFF);
var b = (byte)(argb & 0xFF);
return new Color(r, g, b, a);
}
public uint ToArgb() =>
((uint)Alpha << 24) | ((uint)Red << 16) | ((uint)Green << 8) | Blue;
public string ToHex() =>
$"#{Red:X2}{Green:X2}{Blue:X2}";
public float Opacity => Alpha / 255f;
public static Color Black => new Color(0, 0, 0);
public static Color White => new Color(255, 255, 255);
public static Color Yellow => new Color(255, 255, 0);
public static Color Cyan => new Color(0, 255, 255);
public static Color Magenta => new Color(255, 0, 255);
public override bool Equals(object? obj) => obj is Color color && Equals(color);
public bool Equals(Color other) =>
Red == other.Red && Green == other.Green && Blue == other.Blue && Alpha == other.Alpha;
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 31 + Red.GetHashCode();
hash = hash * 31 + Green.GetHashCode();
hash = hash * 31 + Blue.GetHashCode();
hash = hash * 31 + Alpha.GetHashCode();
return hash;
}
}
public override string ToString() =>
$"Color(R={Red}, G={Green}, B={Blue}, A={Alpha})";
public static bool operator ==(Color left, Color right) => left.Equals(right);
public static bool operator !=(Color left, Color right) => !left.Equals(right);
}
}