Browse Source

Revert "Use RgbaVector for color backing"

This reverts commit 257ff1929e.
pull/1801/head
James Jackson-South 4 years ago
parent
commit
ef90575a11
  1. 87
      src/ImageSharp/Color/Color.Conversions.cs
  2. 74
      src/ImageSharp/Color/Color.cs
  3. 17
      tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs
  4. 4
      tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs

87
src/ImageSharp/Color/Color.Conversions.cs

@ -17,90 +17,56 @@ namespace SixLabors.ImageSharp
/// </summary>
/// <param name="pixel">The <see cref="Rgba64"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgba64 pixel)
{
RgbaVector vector = default;
vector.FromRgba64(pixel);
this.data = vector;
}
public Color(Rgba64 pixel) => this.data = pixel;
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Rgba32"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgba32 pixel)
{
RgbaVector vector = default;
vector.FromRgba32(pixel);
this.data = vector;
}
public Color(Rgba32 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Argb32"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Argb32 pixel)
{
RgbaVector vector = default;
vector.FromArgb32(pixel);
this.data = vector;
}
public Color(Argb32 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Bgra32"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Bgra32 pixel)
{
RgbaVector vector = default;
vector.FromBgra32(pixel);
this.data = vector;
}
public Color(Bgra32 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Rgb24"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgb24 pixel)
{
RgbaVector vector = default;
vector.FromRgb24(pixel);
this.data = vector;
}
public Color(Rgb24 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="pixel">The <see cref="Bgr24"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Bgr24 pixel)
{
RgbaVector vector = default;
vector.FromBgr24(pixel);
this.data = vector;
}
public Color(Bgr24 pixel) => this.data = new Rgba64(pixel);
/// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary>
/// <param name="vector">The <see cref="Vector4"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public Color(Vector4 vector)
{
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One);
this.data = new RgbaVector(vector.X, vector.Y, vector.Z, vector.W);
}
public Color(Vector4 vector) => this.data = new Rgba64(vector);
/// <summary>
/// Converts a <see cref="Color"/> to <see cref="Vector4"/>.
/// </summary>
/// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Vector4"/>.</returns>
public static explicit operator Vector4(Color color) => color.data.ToScaledVector4();
public static explicit operator Vector4(Color color) => color.data.ToVector4();
/// <summary>
/// Converts an <see cref="Vector4"/> to <see cref="Color"/>.
@ -108,47 +74,22 @@ namespace SixLabors.ImageSharp
/// <param name="source">The <see cref="Vector4"/>.</param>
/// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
public static explicit operator Color(Vector4 source) => new(source);
public static explicit operator Color(Vector4 source) => new Color(source);
[MethodImpl(InliningOptions.ShortMethod)]
internal Rgba32 ToRgba32()
{
Rgba32 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
internal Rgba32 ToRgba32() => this.data.ToRgba32();
[MethodImpl(InliningOptions.ShortMethod)]
internal Bgra32 ToBgra32()
{
Bgra32 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
internal Bgra32 ToBgra32() => this.data.ToBgra32();
[MethodImpl(InliningOptions.ShortMethod)]
internal Argb32 ToArgb32()
{
Argb32 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
internal Argb32 ToArgb32() => this.data.ToArgb32();
[MethodImpl(InliningOptions.ShortMethod)]
internal Rgb24 ToRgb24()
{
Rgb24 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
internal Rgb24 ToRgb24() => this.data.ToRgb24();
[MethodImpl(InliningOptions.ShortMethod)]
internal Bgr24 ToBgr24()
{
Bgr24 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
internal Bgr24 ToBgr24() => this.data.ToBgr24();
[MethodImpl(InliningOptions.ShortMethod)]
internal Vector4 ToVector4() => this.data.ToVector4();

74
src/ImageSharp/Color/Color.cs

@ -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();
}
}
}

17
tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs

@ -66,7 +66,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void Rgb24()
{
var source = new Rgb24(1, 22, 231);
var source = new Rgb24(1, 22, 231);
// Act:
Color color = source;
@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void Bgr24()
{
var source = new Bgr24(1, 22, 231);
var source = new Bgr24(1, 22, 231);
// Act:
Color color = source;
@ -88,19 +88,6 @@ namespace SixLabors.ImageSharp.Tests
Bgr24 data = color.ToPixel<Bgr24>();
Assert.Equal(source, data);
}
[Fact]
public void TPixel()
{
var source = new RgbaVector(1, .1F, .133F, .864F);
// Act:
var color = Color.FromPixel(source);
// Assert:
RgbaVector data = color.ToPixel<RgbaVector>();
Assert.Equal(source, data);
}
}
}
}

4
tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs

@ -66,7 +66,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void Rgb24()
{
var source = new Rgb24(1, 22, 231);
var source = new Rgb24(1, 22, 231);
// Act:
var color = new Color(source);
@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void Bgr24()
{
var source = new Bgr24(1, 22, 231);
var source = new Bgr24(1, 22, 231);
// Act:
var color = new Color(source);

Loading…
Cancel
Save