mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
466 lines
17 KiB
466 lines
17 KiB
// Copyright (c) Six Labors and contributors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace SixLabors.ImageSharp.PixelFormats
|
|
{
|
|
/// <summary>
|
|
/// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255.
|
|
/// The color components are stored in red, green, blue, and alpha order (least significant to most significant byte).
|
|
/// <para>
|
|
/// Ranges from [0, 0, 0, 0] to [1, 1, 1, 1] in vector form.
|
|
/// </para>
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This struct is fully mutable. This is done (against the guidelines) for the sake of performance,
|
|
/// as it avoids the need to create new values for modification operations.
|
|
/// </remarks>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public partial struct Rgba32 : IPixel<Rgba32>, IPackedVector<uint>
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the red component.
|
|
/// </summary>
|
|
public byte R;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the green component.
|
|
/// </summary>
|
|
public byte G;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the blue component.
|
|
/// </summary>
|
|
public byte B;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the alpha component.
|
|
/// </summary>
|
|
public byte A;
|
|
|
|
/// <summary>
|
|
/// The shift count for the red component
|
|
/// </summary>
|
|
private const int RedShift = 0;
|
|
|
|
/// <summary>
|
|
/// The shift count for the green component
|
|
/// </summary>
|
|
private const int GreenShift = 8;
|
|
|
|
/// <summary>
|
|
/// The shift count for the blue component
|
|
/// </summary>
|
|
private const int BlueShift = 16;
|
|
|
|
/// <summary>
|
|
/// The shift count for the alpha component
|
|
/// </summary>
|
|
private const int AlphaShift = 24;
|
|
|
|
/// <summary>
|
|
/// The maximum byte value.
|
|
/// </summary>
|
|
private static readonly Vector4 MaxBytes = new Vector4(255);
|
|
|
|
/// <summary>
|
|
/// The half vector value.
|
|
/// </summary>
|
|
private static readonly Vector4 Half = new Vector4(0.5F);
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Rgba32"/> struct.
|
|
/// </summary>
|
|
/// <param name="r">The red component.</param>
|
|
/// <param name="g">The green component.</param>
|
|
/// <param name="b">The blue component.</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Rgba32(byte r, byte g, byte b)
|
|
{
|
|
this.R = r;
|
|
this.G = g;
|
|
this.B = b;
|
|
this.A = byte.MaxValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Rgba32"/> struct.
|
|
/// </summary>
|
|
/// <param name="r">The red component.</param>
|
|
/// <param name="g">The green component.</param>
|
|
/// <param name="b">The blue component.</param>
|
|
/// <param name="a">The alpha component.</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Rgba32(byte r, byte g, byte b, byte a)
|
|
{
|
|
this.R = r;
|
|
this.G = g;
|
|
this.B = b;
|
|
this.A = a;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Rgba32"/> struct.
|
|
/// </summary>
|
|
/// <param name="r">The red component.</param>
|
|
/// <param name="g">The green component.</param>
|
|
/// <param name="b">The blue component.</param>
|
|
/// <param name="a">The alpha component.</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Rgba32(float r, float g, float b, float a = 1)
|
|
: this() => this.Pack(r, g, b, a);
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Rgba32"/> struct.
|
|
/// </summary>
|
|
/// <param name="vector">
|
|
/// The vector containing the components for the packed vector.
|
|
/// </param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Rgba32(Vector3 vector)
|
|
: this() => this.Pack(ref vector);
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Rgba32"/> struct.
|
|
/// </summary>
|
|
/// <param name="vector">
|
|
/// The vector containing the components for the packed vector.
|
|
/// </param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Rgba32(Vector4 vector)
|
|
: this() => this = PackNew(ref vector);
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Rgba32"/> struct.
|
|
/// </summary>
|
|
/// <param name="packed">
|
|
/// The packed value.
|
|
/// </param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Rgba32(uint packed)
|
|
: this() => this.Rgba = packed;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the packed representation of the Rgba32 struct.
|
|
/// </summary>
|
|
public uint Rgba
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => Unsafe.As<Rgba32, uint>(ref this);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
set => Unsafe.As<Rgba32, uint>(ref this) = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the RGB components of this struct as <see cref="Rgb24"/>
|
|
/// </summary>
|
|
public Rgb24 Rgb
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => Unsafe.As<Rgba32, Rgb24>(ref this);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
set => Unsafe.As<Rgba32, Rgb24>(ref this) = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the RGB components of this struct as <see cref="Bgr24"/> reverting the component order.
|
|
/// </summary>
|
|
public Bgr24 Bgr
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => new Bgr24(this.R, this.G, this.B);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
set
|
|
{
|
|
this.R = value.R;
|
|
this.G = value.G;
|
|
this.B = value.B;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public uint PackedValue
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get => this.Rgba;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
set => this.Rgba = value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Allows the implicit conversion of an instance of <see cref="ColorSpaces.Rgb"/> to a
|
|
/// <see cref="Rgba32"/>.
|
|
/// </summary>
|
|
/// <param name="color">The instance of <see cref="ColorSpaces.Rgb"/> to convert.</param>
|
|
/// <returns>An instance of <see cref="Rgba32"/>.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static implicit operator Rgba32(ColorSpaces.Rgb color)
|
|
{
|
|
var vector = new Vector4(color.ToVector3(), 1);
|
|
Rgba32 rgba = default;
|
|
rgba.PackFromScaledVector4(vector);
|
|
return rgba;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Rgba32"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">The <see cref="Rgba32"/> on the left side of the operand.</param>
|
|
/// <param name="right">The <see cref="Rgba32"/> on the right side of the operand.</param>
|
|
/// <returns>
|
|
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
|
|
/// </returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator ==(Rgba32 left, Rgba32 right) => left.Rgba == right.Rgba;
|
|
|
|
/// <summary>
|
|
/// Compares two <see cref="Rgba32"/> objects for equality.
|
|
/// </summary>
|
|
/// <param name="left">The <see cref="Rgba32"/> on the left side of the operand.</param>
|
|
/// <param name="right">The <see cref="Rgba32"/> on the right side of the operand.</param>
|
|
/// <returns>
|
|
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
|
|
/// </returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static bool operator !=(Rgba32 left, Rgba32 right) => left.Rgba != right.Rgba;
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="Rgba32"/> struct.
|
|
/// </summary>
|
|
/// <param name="hex">
|
|
/// The hexadecimal representation of the combined color components arranged
|
|
/// in rgb, rgba, rrggbb, or rrggbbaa format to match web syntax.
|
|
/// </param>
|
|
/// <returns>
|
|
/// The <see cref="Rgba32"/>.
|
|
/// </returns>
|
|
public static Rgba32 FromHex(string hex) => ColorBuilder<Rgba32>.FromHex(hex);
|
|
|
|
/// <inheritdoc />
|
|
public PixelOperations<Rgba32> CreatePixelOperations() => new PixelOperations();
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgba32(Rgba32 source) => this = source;
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromArgb32(Argb32 source)
|
|
{
|
|
this.R = source.R;
|
|
this.G = source.G;
|
|
this.B = source.B;
|
|
this.A = source.A;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromBgra32(Bgra32 source)
|
|
{
|
|
this.R = source.R;
|
|
this.G = source.G;
|
|
this.B = source.B;
|
|
this.A = source.A;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts the value of this instance to a hexadecimal string.
|
|
/// </summary>
|
|
/// <returns>A hexadecimal string representation of the value.</returns>
|
|
public string ToHex()
|
|
{
|
|
uint hexOrder = (uint)(this.A << 0 | this.B << 8 | this.G << 16 | this.R << 24);
|
|
return hexOrder.ToString("X8");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgb24(ref Rgb24 dest) => dest = Unsafe.As<Rgba32, Rgb24>(ref this);
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgba32(ref Rgba32 dest) => dest = this;
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToArgb32(ref Argb32 dest)
|
|
{
|
|
dest.R = this.R;
|
|
dest.G = this.G;
|
|
dest.B = this.B;
|
|
dest.A = this.A;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToBgr24(ref Bgr24 dest)
|
|
{
|
|
dest.R = this.R;
|
|
dest.G = this.G;
|
|
dest.B = this.B;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToBgra32(ref Bgra32 dest)
|
|
{
|
|
dest.R = this.R;
|
|
dest.G = this.G;
|
|
dest.B = this.B;
|
|
dest.A = this.A;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromScaledVector4(Vector4 vector) => this.PackFromVector4(vector);
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Vector4 ToScaledVector4() => this.ToVector4();
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromVector4(Vector4 vector) => this.Pack(ref vector);
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Vector4 ToVector4() => new Vector4(this.R, this.G, this.B, this.A) / MaxBytes;
|
|
|
|
/// <summary>
|
|
/// Gets the value of this struct as <see cref="Bgra32"/>.
|
|
/// Useful for changing the component order.
|
|
/// </summary>
|
|
/// <returns>A <see cref="Bgra32"/> value.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Bgra32 ToBgra32() => new Bgra32(this.R, this.G, this.B, this.A);
|
|
|
|
/// <summary>
|
|
/// Gets the value of this struct as <see cref="Argb32"/>.
|
|
/// Useful for changing the component order.
|
|
/// </summary>
|
|
/// <returns>A <see cref="Argb32"/> value.</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Argb32 ToArgb32() => new Argb32(this.R, this.G, this.B, this.A);
|
|
|
|
/// <summary>
|
|
/// Converts the pixel to <see cref="Rgba32"/> format.
|
|
/// </summary>
|
|
/// <returns>The RGBA value</returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public Rgba32 ToRgba32() => this;
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgb48(Rgb48 source)
|
|
{
|
|
this.R = (byte)(((source.R * 255) + 32895) >> 16);
|
|
this.G = (byte)(((source.G * 255) + 32895) >> 16);
|
|
this.B = (byte)(((source.B * 255) + 32895) >> 16);
|
|
this.A = byte.MaxValue;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgb48(ref Rgb48 dest) => dest.PackFromScaledVector4(this.ToScaledVector4());
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void PackFromRgba64(Rgba64 source)
|
|
{
|
|
// Taken from libpng pngtran.c line: 2419
|
|
this.R = (byte)(((source.R * 255) + 32895) >> 16);
|
|
this.G = (byte)(((source.G * 255) + 32895) >> 16);
|
|
this.B = (byte)(((source.B * 255) + 32895) >> 16);
|
|
this.A = (byte)(((source.A * 255) + 32895) >> 16);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void ToRgba64(ref Rgba64 dest) => dest.PackFromScaledVector4(this.ToScaledVector4());
|
|
|
|
/// <inheritdoc/>
|
|
public override bool Equals(object obj) => obj is Rgba32 rgba32 && this.Equals(rgba32);
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public bool Equals(Rgba32 other) => this.Rgba == other.Rgba;
|
|
|
|
/// <inheritdoc/>
|
|
public override string ToString() => $"({this.R},{this.G},{this.B},{this.A})";
|
|
|
|
/// <inheritdoc/>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public override int GetHashCode() => this.Rgba.GetHashCode();
|
|
|
|
/// <summary>
|
|
/// Gets the <see cref="Vector4"/> representation without normalizing to [0, 1]
|
|
/// </summary>
|
|
/// <returns>A <see cref="Vector4"/> of values in [0, 255] </returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
internal Vector4 ToByteScaledVector4() => new Vector4(this.R, this.G, this.B, this.A);
|
|
|
|
/// <summary>
|
|
/// Packs a <see cref="Vector4"/> into a color returning a new instance as a result.
|
|
/// </summary>
|
|
/// <param name="vector">The vector containing the values to pack.</param>
|
|
/// <returns>The <see cref="Rgba32"/></returns>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static Rgba32 PackNew(ref Vector4 vector)
|
|
{
|
|
vector *= MaxBytes;
|
|
vector += Half;
|
|
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
|
|
|
|
return new Rgba32((byte)vector.X, (byte)vector.Y, (byte)vector.Z, (byte)vector.W);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Packs the four floats into a color.
|
|
/// </summary>
|
|
/// <param name="x">The x-component</param>
|
|
/// <param name="y">The y-component</param>
|
|
/// <param name="z">The z-component</param>
|
|
/// <param name="w">The w-component</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private void Pack(float x, float y, float z, float w)
|
|
{
|
|
var value = new Vector4(x, y, z, w);
|
|
this.Pack(ref value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Packs a <see cref="Vector3"/> into a uint.
|
|
/// </summary>
|
|
/// <param name="vector">The vector containing the values to pack.</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private void Pack(ref Vector3 vector)
|
|
{
|
|
var value = new Vector4(vector, 1);
|
|
this.Pack(ref value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Packs a <see cref="Vector4"/> into a color.
|
|
/// </summary>
|
|
/// <param name="vector">The vector containing the values to pack.</param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private void Pack(ref Vector4 vector)
|
|
{
|
|
vector *= MaxBytes;
|
|
vector += Half;
|
|
vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes);
|
|
|
|
this.R = (byte)vector.X;
|
|
this.G = (byte)vector.Y;
|
|
this.B = (byte)vector.Z;
|
|
this.A = (byte)vector.W;
|
|
}
|
|
}
|
|
}
|