Browse Source

Revert "Use RgbaVector for color backing"

This reverts commit 257ff1929e.
pull/1801/head
James Jackson-South 5 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> /// </summary>
/// <param name="pixel">The <see cref="Rgba64"/> containing the color information.</param> /// <param name="pixel">The <see cref="Rgba64"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgba64 pixel) public Color(Rgba64 pixel) => this.data = pixel;
{
RgbaVector vector = default;
vector.FromRgba64(pixel);
this.data = vector;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct. /// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary> /// </summary>
/// <param name="pixel">The <see cref="Rgba32"/> containing the color information.</param> /// <param name="pixel">The <see cref="Rgba32"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgba32 pixel) public Color(Rgba32 pixel) => this.data = new Rgba64(pixel);
{
RgbaVector vector = default;
vector.FromRgba32(pixel);
this.data = vector;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct. /// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary> /// </summary>
/// <param name="pixel">The <see cref="Argb32"/> containing the color information.</param> /// <param name="pixel">The <see cref="Argb32"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Color(Argb32 pixel) public Color(Argb32 pixel) => this.data = new Rgba64(pixel);
{
RgbaVector vector = default;
vector.FromArgb32(pixel);
this.data = vector;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct. /// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary> /// </summary>
/// <param name="pixel">The <see cref="Bgra32"/> containing the color information.</param> /// <param name="pixel">The <see cref="Bgra32"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Color(Bgra32 pixel) public Color(Bgra32 pixel) => this.data = new Rgba64(pixel);
{
RgbaVector vector = default;
vector.FromBgra32(pixel);
this.data = vector;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct. /// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary> /// </summary>
/// <param name="pixel">The <see cref="Rgb24"/> containing the color information.</param> /// <param name="pixel">The <see cref="Rgb24"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Color(Rgb24 pixel) public Color(Rgb24 pixel) => this.data = new Rgba64(pixel);
{
RgbaVector vector = default;
vector.FromRgb24(pixel);
this.data = vector;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct. /// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary> /// </summary>
/// <param name="pixel">The <see cref="Bgr24"/> containing the color information.</param> /// <param name="pixel">The <see cref="Bgr24"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Color(Bgr24 pixel) public Color(Bgr24 pixel) => this.data = new Rgba64(pixel);
{
RgbaVector vector = default;
vector.FromBgr24(pixel);
this.data = vector;
}
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Color"/> struct. /// Initializes a new instance of the <see cref="Color"/> struct.
/// </summary> /// </summary>
/// <param name="vector">The <see cref="Vector4"/> containing the color information.</param> /// <param name="vector">The <see cref="Vector4"/> containing the color information.</param>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public Color(Vector4 vector) public Color(Vector4 vector) => this.data = new Rgba64(vector);
{
vector = Numerics.Clamp(vector, Vector4.Zero, Vector4.One);
this.data = new RgbaVector(vector.X, vector.Y, vector.Z, vector.W);
}
/// <summary> /// <summary>
/// Converts a <see cref="Color"/> to <see cref="Vector4"/>. /// Converts a <see cref="Color"/> to <see cref="Vector4"/>.
/// </summary> /// </summary>
/// <param name="color">The <see cref="Color"/>.</param> /// <param name="color">The <see cref="Color"/>.</param>
/// <returns>The <see cref="Vector4"/>.</returns> /// <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> /// <summary>
/// Converts an <see cref="Vector4"/> to <see cref="Color"/>. /// Converts an <see cref="Vector4"/> to <see cref="Color"/>.
@ -108,47 +74,22 @@ namespace SixLabors.ImageSharp
/// <param name="source">The <see cref="Vector4"/>.</param> /// <param name="source">The <see cref="Vector4"/>.</param>
/// <returns>The <see cref="Color"/>.</returns> /// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)] [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)] [MethodImpl(InliningOptions.ShortMethod)]
internal Rgba32 ToRgba32() internal Rgba32 ToRgba32() => this.data.ToRgba32();
{
Rgba32 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal Bgra32 ToBgra32() internal Bgra32 ToBgra32() => this.data.ToBgra32();
{
Bgra32 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal Argb32 ToArgb32() internal Argb32 ToArgb32() => this.data.ToArgb32();
{
Argb32 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal Rgb24 ToRgb24() internal Rgb24 ToRgb24() => this.data.ToRgb24();
{
Rgb24 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal Bgr24 ToBgr24() internal Bgr24 ToBgr24() => this.data.ToBgr24();
{
Bgr24 result = default;
result.FromScaledVector4(this.data.ToScaledVector4());
return result;
}
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
internal Vector4 ToVector4() => this.data.ToVector4(); internal Vector4 ToVector4() => this.data.ToVector4();

74
src/ImageSharp/Color/Color.cs

@ -20,22 +20,26 @@ namespace SixLabors.ImageSharp
/// </remarks> /// </remarks>
public readonly partial struct Color : IEquatable<Color> public readonly partial struct Color : IEquatable<Color>
{ {
private readonly RgbaVector data; private readonly Rgba64 data;
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private Color(byte r, byte g, byte b, byte a) private Color(byte r, byte g, byte b, byte a)
{ {
RgbaVector vector = default; this.data = new Rgba64(
vector.FromRgba32(new(r, g, b, a)); ColorNumerics.UpscaleFrom8BitTo16Bit(r),
this.data = vector; ColorNumerics.UpscaleFrom8BitTo16Bit(g),
ColorNumerics.UpscaleFrom8BitTo16Bit(b),
ColorNumerics.UpscaleFrom8BitTo16Bit(a));
} }
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private Color(byte r, byte g, byte b) private Color(byte r, byte g, byte b)
{ {
RgbaVector vector = default; this.data = new Rgba64(
vector.FromRgba32(new(r, g, b)); ColorNumerics.UpscaleFrom8BitTo16Bit(r),
this.data = vector; ColorNumerics.UpscaleFrom8BitTo16Bit(g),
ColorNumerics.UpscaleFrom8BitTo16Bit(b),
ushort.MaxValue);
} }
/// <summary> /// <summary>
@ -48,7 +52,10 @@ namespace SixLabors.ImageSharp
/// otherwise, false. /// otherwise, false.
/// </returns> /// </returns>
[MethodImpl(InliningOptions.ShortMethod)] [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> /// <summary>
/// Checks whether two <see cref="Color"/> structures are equal. /// Checks whether two <see cref="Color"/> structures are equal.
@ -60,7 +67,10 @@ namespace SixLabors.ImageSharp
/// otherwise, false. /// otherwise, false.
/// </returns> /// </returns>
[MethodImpl(InliningOptions.ShortMethod)] [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> /// <summary>
/// Creates a <see cref="Color"/> from RGBA bytes. /// Creates a <see cref="Color"/> from RGBA bytes.
@ -71,7 +81,7 @@ namespace SixLabors.ImageSharp
/// <param name="a">The alpha component (0-255).</param> /// <param name="a">The alpha component (0-255).</param>
/// <returns>The <see cref="Color"/>.</returns> /// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)] [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> /// <summary>
/// Creates a <see cref="Color"/> from RGB bytes. /// Creates a <see cref="Color"/> from RGB bytes.
@ -81,17 +91,7 @@ namespace SixLabors.ImageSharp
/// <param name="b">The blue component (0-255).</param> /// <param name="b">The blue component (0-255).</param>
/// <returns>The <see cref="Color"/>.</returns> /// <returns>The <see cref="Color"/>.</returns>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public static Color FromRgb(byte r, byte g, byte b) => new(r, g, b); public static Color FromRgb(byte r, byte g, byte b) => new Color(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());
/// <summary> /// <summary>
/// Creates a new instance of the <see cref="Color"/> struct /// Creates a new instance of the <see cref="Color"/> struct
@ -207,18 +207,13 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <returns>A hexadecimal string representation of the value.</returns> /// <returns>A hexadecimal string representation of the value.</returns>
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
public string ToHex() public string ToHex() => this.data.ToRgba32().ToHex();
{
Rgba32 rgba = default;
this.data.ToRgba32(ref rgba);
return rgba.ToHex();
}
/// <inheritdoc /> /// <inheritdoc />
public override string ToString() => this.ToHex(); public override string ToString() => this.ToHex();
/// <summary> /// <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> /// </summary>
/// <typeparam name="TPixel">The pixel type to convert to.</typeparam> /// <typeparam name="TPixel">The pixel type to convert to.</typeparam>
/// <returns>The pixel value.</returns> /// <returns>The pixel value.</returns>
@ -227,12 +222,12 @@ namespace SixLabors.ImageSharp
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
TPixel pixel = default; TPixel pixel = default;
pixel.FromScaledVector4(this.data.ToScaledVector4()); pixel.FromRgba64(this.data);
return pixel; return pixel;
} }
/// <summary> /// <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> /// </summary>
/// <typeparam name="TPixel">The pixel type to convert to.</typeparam> /// <typeparam name="TPixel">The pixel type to convert to.</typeparam>
/// <param name="configuration">The configuration.</param> /// <param name="configuration">The configuration.</param>
@ -245,19 +240,28 @@ namespace SixLabors.ImageSharp
Span<TPixel> destination) Span<TPixel> destination)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
ReadOnlySpan<RgbaVector> rgbaSpan = MemoryMarshal.Cast<Color, RgbaVector>(source); ReadOnlySpan<Rgba64> rgba64Span = MemoryMarshal.Cast<Color, Rgba64>(source);
PixelOperations<TPixel>.Instance.From(configuration, rgbaSpan, destination); PixelOperations<TPixel>.Instance.FromRgba64(configuration, rgba64Span, destination);
} }
/// <inheritdoc /> /// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)] [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 /> /// <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 /> /// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)] [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] [Fact]
public void Rgb24() public void Rgb24()
{ {
var source = new Rgb24(1, 22, 231); var source = new Rgb24(1, 22, 231);
// Act: // Act:
Color color = source; Color color = source;
@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact] [Fact]
public void Bgr24() public void Bgr24()
{ {
var source = new Bgr24(1, 22, 231); var source = new Bgr24(1, 22, 231);
// Act: // Act:
Color color = source; Color color = source;
@ -88,19 +88,6 @@ namespace SixLabors.ImageSharp.Tests
Bgr24 data = color.ToPixel<Bgr24>(); Bgr24 data = color.ToPixel<Bgr24>();
Assert.Equal(source, data); 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] [Fact]
public void Rgb24() public void Rgb24()
{ {
var source = new Rgb24(1, 22, 231); var source = new Rgb24(1, 22, 231);
// Act: // Act:
var color = new Color(source); var color = new Color(source);
@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Tests
[Fact] [Fact]
public void Bgr24() public void Bgr24()
{ {
var source = new Bgr24(1, 22, 231); var source = new Bgr24(1, 22, 231);
// Act: // Act:
var color = new Color(source); var color = new Color(source);

Loading…
Cancel
Save