diff --git a/src/ImageSharp/Color/Color.Conversions.cs b/src/ImageSharp/Color/Color.Conversions.cs
index abcb54b80..0455fd26a 100644
--- a/src/ImageSharp/Color/Color.Conversions.cs
+++ b/src/ImageSharp/Color/Color.Conversions.cs
@@ -17,90 +17,56 @@ namespace SixLabors.ImageSharp
///
/// The containing the color information.
[MethodImpl(InliningOptions.ShortMethod)]
- public Color(Rgba64 pixel)
- {
- RgbaVector vector = default;
- vector.FromRgba64(pixel);
- this.data = vector;
- }
+ public Color(Rgba64 pixel) => this.data = pixel;
///
/// Initializes a new instance of the struct.
///
/// The containing the color information.
[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);
///
/// Initializes a new instance of the struct.
///
/// The containing the color information.
[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);
///
/// Initializes a new instance of the struct.
///
/// The containing the color information.
[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);
///
/// Initializes a new instance of the struct.
///
/// The containing the color information.
[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);
///
/// Initializes a new instance of the struct.
///
/// The containing the color information.
[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);
///
/// Initializes a new instance of the struct.
///
/// The containing the color information.
[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);
///
/// Converts a to .
///
/// The .
/// The .
- public static explicit operator Vector4(Color color) => color.data.ToScaledVector4();
+ public static explicit operator Vector4(Color color) => color.data.ToVector4();
///
/// Converts an to .
@@ -108,47 +74,22 @@ namespace SixLabors.ImageSharp
/// The .
/// The .
[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();
diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs
index 9a4df4e62..d5eedc160 100644
--- a/src/ImageSharp/Color/Color.cs
+++ b/src/ImageSharp/Color/Color.cs
@@ -20,22 +20,26 @@ namespace SixLabors.ImageSharp
///
public readonly partial struct Color : IEquatable
{
- 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);
}
///
@@ -48,7 +52,10 @@ namespace SixLabors.ImageSharp
/// otherwise, false.
///
[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);
+ }
///
/// Checks whether two structures are equal.
@@ -60,7 +67,10 @@ namespace SixLabors.ImageSharp
/// otherwise, false.
///
[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);
+ }
///
/// Creates a from RGBA bytes.
@@ -71,7 +81,7 @@ namespace SixLabors.ImageSharp
/// The alpha component (0-255).
/// The .
[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);
///
/// Creates a from RGB bytes.
@@ -81,17 +91,7 @@ namespace SixLabors.ImageSharp
/// The blue component (0-255).
/// The .
[MethodImpl(InliningOptions.ShortMethod)]
- public static Color FromRgb(byte r, byte g, byte b) => new(r, g, b);
-
- ///
- /// Creates a from the given .
- ///
- /// The pixel to convert from.
- /// The pixel format.
- /// The .
- [MethodImpl(InliningOptions.ShortMethod)]
- public static Color FromPixel(TPixel pixel)
- where TPixel : unmanaged, IPixel => new(pixel.ToScaledVector4());
+ public static Color FromRgb(byte r, byte g, byte b) => new Color(r, g, b);
///
/// Creates a new instance of the struct
@@ -207,18 +207,13 @@ namespace SixLabors.ImageSharp
///
/// A hexadecimal string representation of the value.
[MethodImpl(InliningOptions.ShortMethod)]
- public string ToHex()
- {
- Rgba32 rgba = default;
- this.data.ToRgba32(ref rgba);
- return rgba.ToHex();
- }
+ public string ToHex() => this.data.ToRgba32().ToHex();
///
public override string ToString() => this.ToHex();
///
- /// Converts the color instance to a specified type.
+ /// Converts the color instance to a specified type.
///
/// The pixel type to convert to.
/// The pixel value.
@@ -227,12 +222,12 @@ namespace SixLabors.ImageSharp
where TPixel : unmanaged, IPixel
{
TPixel pixel = default;
- pixel.FromScaledVector4(this.data.ToScaledVector4());
+ pixel.FromRgba64(this.data);
return pixel;
}
///
- /// Bulk converts a span of to a span of a specified type.
+ /// Bulk converts a span of to a span of a specified type.
///
/// The pixel type to convert to.
/// The configuration.
@@ -245,19 +240,28 @@ namespace SixLabors.ImageSharp
Span destination)
where TPixel : unmanaged, IPixel
{
- ReadOnlySpan rgbaSpan = MemoryMarshal.Cast(source);
- PixelOperations.Instance.From(configuration, rgbaSpan, destination);
+ ReadOnlySpan rgba64Span = MemoryMarshal.Cast(source);
+ PixelOperations.Instance.FromRgba64(configuration, rgba64Span, destination);
}
///
[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;
+ }
///
- 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);
+ }
///
[MethodImpl(InliningOptions.ShortMethod)]
- public override int GetHashCode() => this.data.GetHashCode();
+ public override int GetHashCode()
+ {
+ return this.data.PackedValue.GetHashCode();
+ }
}
}
diff --git a/tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs b/tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs
index 356ef7351..38b94f486 100644
--- a/tests/ImageSharp.Tests/Color/ColorTests.CastFrom.cs
+++ b/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();
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();
- Assert.Equal(source, data);
- }
}
}
}
diff --git a/tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs b/tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs
index dd51f3a6c..89276014b 100644
--- a/tests/ImageSharp.Tests/Color/ColorTests.ConstructFrom.cs
+++ b/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);