using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Uwaa.PNG;
///
/// A 32-bit RGBA pixel in a image.
///
[StructLayout(LayoutKind.Sequential)]
public readonly struct Pixel : IEquatable
{
///
/// The red value for the pixel.
///
public byte R { get; }
///
/// The green value for the pixel.
///
public byte G { get; }
///
/// The blue value for the pixel.
///
public byte B { get; }
///
/// The alpha transparency value for the pixel.
///
public byte A { get; }
///
/// Create a new .
///
/// The red value for the pixel.
/// The green value for the pixel.
/// The blue value for the pixel.
/// The alpha transparency value for the pixel.
public Pixel(byte r, byte g, byte b, byte a)
{
R = r;
G = g;
B = b;
A = a;
}
///
/// Create a new which is fully opaque.
///
/// The red value for the pixel.
/// The green value for the pixel.
/// The blue value for the pixel.
public Pixel(byte r, byte g, byte b)
{
R = r;
G = g;
B = b;
A = 255;
}
///
/// Create a new grayscale .
///
/// The grayscale value.
public Pixel(byte grayscale)
{
R = grayscale;
G = grayscale;
B = grayscale;
A = 255;
}
public override string ToString() => $"({R}, {G}, {B}, {A})";
public override bool Equals(object? obj) => obj is Pixel pixel && Equals(pixel);
///
/// Whether the pixel values are equal.
///
/// The other pixel.
/// if all pixel values are equal otherwise .
public bool Equals(Pixel other)
{
Pixel this_ = this;
return Unsafe.As(ref this_) == Unsafe.As(ref other);
}
public override int GetHashCode() => HashCode.Combine(R, G, B, A);
public static bool operator ==(Pixel left, Pixel right) => left.Equals(right);
public static bool operator !=(Pixel left, Pixel right) => !(left == right);
}