|
|
|
@ -20,22 +20,26 @@ namespace SixLabors.ImageSharp |
|
|
|
/// </remarks>
|
|
|
|
public readonly partial struct Color : IEquatable<Color> |
|
|
|
{ |
|
|
|
private readonly RgbaVector data; |
|
|
|
private readonly Rgba64 data; |
|
|
|
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
private Color(byte r, byte g, byte b, byte a) |
|
|
|
{ |
|
|
|
RgbaVector vector = default; |
|
|
|
vector.FromRgba32(new(r, g, b, a)); |
|
|
|
this.data = vector; |
|
|
|
this.data = new Rgba64( |
|
|
|
ColorNumerics.UpscaleFrom8BitTo16Bit(r), |
|
|
|
ColorNumerics.UpscaleFrom8BitTo16Bit(g), |
|
|
|
ColorNumerics.UpscaleFrom8BitTo16Bit(b), |
|
|
|
ColorNumerics.UpscaleFrom8BitTo16Bit(a)); |
|
|
|
} |
|
|
|
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
private Color(byte r, byte g, byte b) |
|
|
|
{ |
|
|
|
RgbaVector vector = default; |
|
|
|
vector.FromRgba32(new(r, g, b)); |
|
|
|
this.data = vector; |
|
|
|
this.data = new Rgba64( |
|
|
|
ColorNumerics.UpscaleFrom8BitTo16Bit(r), |
|
|
|
ColorNumerics.UpscaleFrom8BitTo16Bit(g), |
|
|
|
ColorNumerics.UpscaleFrom8BitTo16Bit(b), |
|
|
|
ushort.MaxValue); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -48,7 +52,10 @@ namespace SixLabors.ImageSharp |
|
|
|
/// otherwise, false.
|
|
|
|
/// </returns>
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public static bool operator ==(Color left, Color right) => left.Equals(right); |
|
|
|
public static bool operator ==(Color left, Color right) |
|
|
|
{ |
|
|
|
return left.Equals(right); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks whether two <see cref="Color"/> structures are equal.
|
|
|
|
@ -60,7 +67,10 @@ namespace SixLabors.ImageSharp |
|
|
|
/// otherwise, false.
|
|
|
|
/// </returns>
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public static bool operator !=(Color left, Color right) => !left.Equals(right); |
|
|
|
public static bool operator !=(Color left, Color right) |
|
|
|
{ |
|
|
|
return !left.Equals(right); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a <see cref="Color"/> from RGBA bytes.
|
|
|
|
@ -71,7 +81,7 @@ namespace SixLabors.ImageSharp |
|
|
|
/// <param name="a">The alpha component (0-255).</param>
|
|
|
|
/// <returns>The <see cref="Color"/>.</returns>
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public static Color FromRgba(byte r, byte g, byte b, byte a) => new(r, g, b, a); |
|
|
|
public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(r, g, b, a); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a <see cref="Color"/> from RGB bytes.
|
|
|
|
@ -81,17 +91,7 @@ namespace SixLabors.ImageSharp |
|
|
|
/// <param name="b">The blue component (0-255).</param>
|
|
|
|
/// <returns>The <see cref="Color"/>.</returns>
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public static Color FromRgb(byte r, byte g, byte b) => new(r, g, b); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a <see cref="Color"/> from the given <typeparamref name="TPixel"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pixel">The pixel to convert from.</param>
|
|
|
|
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|
|
|
/// <returns>The <see cref="Color"/>.</returns>
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public static Color FromPixel<TPixel>(TPixel pixel) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> => new(pixel.ToScaledVector4()); |
|
|
|
public static Color FromRgb(byte r, byte g, byte b) => new Color(r, g, b); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a new instance of the <see cref="Color"/> struct
|
|
|
|
@ -207,18 +207,13 @@ namespace SixLabors.ImageSharp |
|
|
|
/// </summary>
|
|
|
|
/// <returns>A hexadecimal string representation of the value.</returns>
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public string ToHex() |
|
|
|
{ |
|
|
|
Rgba32 rgba = default; |
|
|
|
this.data.ToRgba32(ref rgba); |
|
|
|
return rgba.ToHex(); |
|
|
|
} |
|
|
|
public string ToHex() => this.data.ToRgba32().ToHex(); |
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override string ToString() => this.ToHex(); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Converts the color instance to a specified <typeparamref name="TPixel"/> type.
|
|
|
|
/// Converts the color instance to a specified <see cref="IPixel{TSelf}"/> type.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TPixel">The pixel type to convert to.</typeparam>
|
|
|
|
/// <returns>The pixel value.</returns>
|
|
|
|
@ -227,12 +222,12 @@ namespace SixLabors.ImageSharp |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
TPixel pixel = default; |
|
|
|
pixel.FromScaledVector4(this.data.ToScaledVector4()); |
|
|
|
pixel.FromRgba64(this.data); |
|
|
|
return pixel; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Bulk converts a span of <see cref="Color"/> to a span of a specified <typeparamref name="TPixel"/> type.
|
|
|
|
/// Bulk converts a span of <see cref="Color"/> to a span of a specified <see cref="IPixel{TSelf}"/> type.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TPixel">The pixel type to convert to.</typeparam>
|
|
|
|
/// <param name="configuration">The configuration.</param>
|
|
|
|
@ -245,19 +240,28 @@ namespace SixLabors.ImageSharp |
|
|
|
Span<TPixel> destination) |
|
|
|
where TPixel : unmanaged, IPixel<TPixel> |
|
|
|
{ |
|
|
|
ReadOnlySpan<RgbaVector> rgbaSpan = MemoryMarshal.Cast<Color, RgbaVector>(source); |
|
|
|
PixelOperations<TPixel>.Instance.From(configuration, rgbaSpan, destination); |
|
|
|
ReadOnlySpan<Rgba64> rgba64Span = MemoryMarshal.Cast<Color, Rgba64>(source); |
|
|
|
PixelOperations<TPixel>.Instance.FromRgba64(configuration, rgba64Span, destination); |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public bool Equals(Color other) => this.data.Equals(other.data); |
|
|
|
public bool Equals(Color other) |
|
|
|
{ |
|
|
|
return this.data.PackedValue == other.data.PackedValue; |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override bool Equals(object obj) => obj is Color other && this.Equals(other); |
|
|
|
public override bool Equals(object obj) |
|
|
|
{ |
|
|
|
return obj is Color other && this.Equals(other); |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
[MethodImpl(InliningOptions.ShortMethod)] |
|
|
|
public override int GetHashCode() => this.data.GetHashCode(); |
|
|
|
public override int GetHashCode() |
|
|
|
{ |
|
|
|
return this.data.PackedValue.GetHashCode(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|