93 lines
No EOL
2.6 KiB
C#
93 lines
No EOL
2.6 KiB
C#
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace Uwaa.PNG;
|
|
|
|
/// <summary>
|
|
/// A 32-bit RGBA pixel in a <see cref="Png"/> image.
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public readonly struct Pixel : IEquatable<Pixel>
|
|
{
|
|
/// <summary>
|
|
/// The red value for the pixel.
|
|
/// </summary>
|
|
public byte R { get; }
|
|
|
|
/// <summary>
|
|
/// The green value for the pixel.
|
|
/// </summary>
|
|
public byte G { get; }
|
|
|
|
/// <summary>
|
|
/// The blue value for the pixel.
|
|
/// </summary>
|
|
public byte B { get; }
|
|
|
|
/// <summary>
|
|
/// The alpha transparency value for the pixel.
|
|
/// </summary>
|
|
public byte A { get; }
|
|
|
|
/// <summary>
|
|
/// Create a new <see cref="Pixel"/>.
|
|
/// </summary>
|
|
/// <param name="r">The red value for the pixel.</param>
|
|
/// <param name="g">The green value for the pixel.</param>
|
|
/// <param name="b">The blue value for the pixel.</param>
|
|
/// <param name="a">The alpha transparency value for the pixel.</param>
|
|
public Pixel(byte r, byte g, byte b, byte a)
|
|
{
|
|
R = r;
|
|
G = g;
|
|
B = b;
|
|
A = a;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new <see cref="Pixel"/> which is fully opaque.
|
|
/// </summary>
|
|
/// <param name="r">The red value for the pixel.</param>
|
|
/// <param name="g">The green value for the pixel.</param>
|
|
/// <param name="b">The blue value for the pixel.</param>
|
|
public Pixel(byte r, byte g, byte b)
|
|
{
|
|
R = r;
|
|
G = g;
|
|
B = b;
|
|
A = 255;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new grayscale <see cref="Pixel"/>.
|
|
/// </summary>
|
|
/// <param name="grayscale">The grayscale value.</param>
|
|
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);
|
|
|
|
/// <summary>
|
|
/// Whether the pixel values are equal.
|
|
/// </summary>
|
|
/// <param name="other">The other pixel.</param>
|
|
/// <returns><see langword="true"/> if all pixel values are equal otherwise <see langword="false"/>.</returns>
|
|
public bool Equals(Pixel other)
|
|
{
|
|
Pixel this_ = this;
|
|
return Unsafe.As<Pixel, uint>(ref this_) == Unsafe.As<Pixel, uint>(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);
|
|
} |