diff --git a/ImageSharp.sln.DotSettings b/ImageSharp.sln.DotSettings index 526817242..ece3dddb3 100644 --- a/ImageSharp.sln.DotSettings +++ b/ImageSharp.sln.DotSettings @@ -388,5 +388,6 @@ True True True + True True \ No newline at end of file diff --git a/src/ImageSharp/Color/Color.Conversions.cs b/src/ImageSharp/Color/Color.Conversions.cs index 001aee5a4..a524b9db1 100644 --- a/src/ImageSharp/Color/Color.Conversions.cs +++ b/src/ImageSharp/Color/Color.Conversions.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System.Numerics; +using System.Runtime.CompilerServices; using SixLabors.ImageSharp.PixelFormats; @@ -16,147 +17,67 @@ namespace SixLabors.ImageSharp /// Initializes a new instance of the struct. /// /// The containing the color information. - public Color(Rgba64 pixel) - { - this.data = pixel; - } + [MethodImpl(InliningOptions.ShortMethod)] + public Color(Rgba64 pixel) => this.data = pixel; /// /// Initializes a new instance of the struct. /// /// The containing the color information. - public Color(Rgba32 pixel) - { - this.data = new Rgba64(pixel); - } + [MethodImpl(InliningOptions.ShortMethod)] + public Color(Rgba32 pixel) => this.data = new Rgba64(pixel); /// /// Initializes a new instance of the struct. /// /// The containing the color information. - public Color(Argb32 pixel) - { - this.data = new Rgba64(pixel); - } + [MethodImpl(InliningOptions.ShortMethod)] + public Color(Argb32 pixel) => this.data = new Rgba64(pixel); /// /// Initializes a new instance of the struct. /// /// The containing the color information. - public Color(Bgra32 pixel) - { - this.data = new Rgba64(pixel); - } + [MethodImpl(InliningOptions.ShortMethod)] + public Color(Bgra32 pixel) => this.data = new Rgba64(pixel); /// /// Initializes a new instance of the struct. /// /// The containing the color information. - public Color(Rgb24 pixel) - { - this.data = new Rgba64(pixel); - } + [MethodImpl(InliningOptions.ShortMethod)] + public Color(Rgb24 pixel) => this.data = new Rgba64(pixel); /// /// Initializes a new instance of the struct. /// /// The containing the color information. - public Color(Bgr24 pixel) - { - this.data = new Rgba64(pixel); - } + [MethodImpl(InliningOptions.ShortMethod)] + public Color(Bgr24 pixel) => this.data = new Rgba64(pixel); /// /// Initializes a new instance of the struct. /// /// The containing the color information. - public Color(Vector4 vector) - { - this.data = new Rgba64(vector); - } + [MethodImpl(InliningOptions.ShortMethod)] + public Color(Vector4 vector) => this.data = new Rgba64(vector); - /// - /// Converts an to . - /// - /// The . - /// The . - public static implicit operator Color(Rgba64 source) => new Color(source); - - /// - /// Converts an to . - /// - /// The . - /// The . - public static implicit operator Color(Rgba32 source) => new Color(source); - - /// - /// Converts an to . - /// - /// The . - /// The . - public static implicit operator Color(Bgra32 source) => new Color(source); - - /// - /// Converts an to . - /// - /// The . - /// The . - public static implicit operator Color(Argb32 source) => new Color(source); - - /// - /// Converts an to . - /// - /// The . - /// The . - public static implicit operator Color(Rgb24 source) => new Color(source); - - /// - /// Converts an to . - /// - /// The . - /// The . - public static implicit operator Color(Bgr24 source) => new Color(source); + [MethodImpl(InliningOptions.ShortMethod)] + internal Rgba64 ToRgba64() => this.data; - /// - /// Converts a to . - /// - /// The . - /// The . - public static implicit operator Rgba64(Color color) => color.data; + [MethodImpl(InliningOptions.ShortMethod)] + internal Rgba32 ToRgba32() => this.data.ToRgba32(); - /// - /// Converts a to . - /// - /// The . - /// The . - public static implicit operator Rgba32(Color color) => color.data.ToRgba32(); + [MethodImpl(InliningOptions.ShortMethod)] + internal Bgra32 ToBgra32() => this.data.ToBgra32(); - /// - /// Converts a to . - /// - /// The . - /// The . - public static implicit operator Bgra32(Color color) => color.data.ToBgra32(); + [MethodImpl(InliningOptions.ShortMethod)] + internal Argb32 ToArgb32() => this.data.ToArgb32(); - /// - /// Converts a to . - /// - /// The . - /// The . - public static implicit operator Argb32(Color color) => color.data.ToArgb32(); + [MethodImpl(InliningOptions.ShortMethod)] + internal Rgb24 ToRgb24() => this.data.ToRgb24(); - /// - /// Converts a to . - /// - /// The . - /// The . - public static implicit operator Rgb24(Color color) => color.data.ToRgb24(); - - /// - /// Converts a to . - /// - /// The . - /// The . - public static implicit operator Bgr24(Color color) => color.data.ToBgr24(); + [MethodImpl(InliningOptions.ShortMethod)] + internal Bgr24 ToBgr24() => this.data.ToBgr24(); } } \ No newline at end of file diff --git a/src/ImageSharp/Color/Color.cs b/src/ImageSharp/Color/Color.cs index f2f8578d8..e4640ff63 100644 --- a/src/ImageSharp/Color/Color.cs +++ b/src/ImageSharp/Color/Color.cs @@ -5,6 +5,7 @@ using System; using System.Buffers.Binary; using System.Globalization; using System.Numerics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.PixelFormats; @@ -24,6 +25,26 @@ namespace SixLabors.ImageSharp { private readonly Rgba64 data; + [MethodImpl(InliningOptions.ShortMethod)] + private Color(byte r, byte g, byte b, byte a) + { + this.data = new Rgba64( + ImageMaths.UpscaleFrom8BitTo16Bit(r), + ImageMaths.UpscaleFrom8BitTo16Bit(g), + ImageMaths.UpscaleFrom8BitTo16Bit(b), + ImageMaths.UpscaleFrom8BitTo16Bit(a)); + } + + [MethodImpl(InliningOptions.ShortMethod)] + private Color(byte r, byte g, byte b) + { + this.data = new Rgba64( + ImageMaths.UpscaleFrom8BitTo16Bit(r), + ImageMaths.UpscaleFrom8BitTo16Bit(g), + ImageMaths.UpscaleFrom8BitTo16Bit(b), + ushort.MaxValue); + } + /// /// Checks whether two structures are equal. /// @@ -33,6 +54,7 @@ namespace SixLabors.ImageSharp /// True if the parameter is equal to the parameter; /// otherwise, false. /// + [MethodImpl(InliningOptions.ShortMethod)] public static bool operator ==(Color left, Color right) { return left.Equals(right); @@ -47,6 +69,7 @@ namespace SixLabors.ImageSharp /// True if the parameter is not equal to the parameter; /// otherwise, false. /// + [MethodImpl(InliningOptions.ShortMethod)] public static bool operator !=(Color left, Color right) { return !left.Equals(right); @@ -60,7 +83,8 @@ namespace SixLabors.ImageSharp /// The blue component (0-255). /// The alpha component (0-255). /// The . - public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(new Rgba32(r, g, b, a)); + [MethodImpl(InliningOptions.ShortMethod)] + public static Color FromRgba(byte r, byte g, byte b, byte a) => new Color(r, g, b, a); /// /// Creates a from RGB bytes. @@ -69,7 +93,8 @@ namespace SixLabors.ImageSharp /// The green component (0-255). /// The blue component (0-255). /// The . - public static Color FromRgb(byte r, byte g, byte b) => FromRgba(r, g, b, 255); + [MethodImpl(InliningOptions.ShortMethod)] + public static Color FromRgb(byte r, byte g, byte b) => new Color(r, g, b); /// /// Creates a new instance from the string representing a color in hexadecimal form. @@ -98,6 +123,7 @@ namespace SixLabors.ImageSharp /// Gets the hexadecimal representation of the color instance in rrggbbaa form. /// /// A hexadecimal string representation of the value. + [MethodImpl(InliningOptions.ShortMethod)] public string ToHex() => this.data.ToRgba32().ToHex(); /// @@ -109,6 +135,7 @@ namespace SixLabors.ImageSharp /// /// The pixel type to convert to. /// The pixel value. + [MethodImpl(InliningOptions.ShortMethod)] public TPixel ToPixel() where TPixel : struct, IPixel { @@ -118,6 +145,7 @@ namespace SixLabors.ImageSharp } /// + [MethodImpl(InliningOptions.ShortMethod)] public bool Equals(Color other) { return this.data.PackedValue == other.data.PackedValue; @@ -126,15 +154,11 @@ namespace SixLabors.ImageSharp /// public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) - { - return false; - } - return obj is Color other && this.Equals(other); } /// + [MethodImpl(InliningOptions.ShortMethod)] public override int GetHashCode() { return this.data.PackedValue.GetHashCode(); @@ -143,6 +167,7 @@ namespace SixLabors.ImageSharp /// /// Bulk convert a span of to a span of a specified pixel type. /// + [MethodImpl(InliningOptions.ShortMethod)] internal static void ToPixel( Configuration configuration, ReadOnlySpan source, diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs index 075df01cd..8981c8745 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Argb32.cs @@ -142,6 +142,22 @@ namespace SixLabors.ImageSharp.PixelFormats set => this.Argb = value; } + /// + /// Converts an to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Color(Argb32 source) => new Color(source); + + /// + /// Converts a to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Argb32(Color color) => color.ToArgb32(); + /// /// Compares two objects for equality. /// diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs index 3ba6436a0..a0b059dfc 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Bgr24.cs @@ -50,6 +50,22 @@ namespace SixLabors.ImageSharp.PixelFormats this.B = b; } + /// + /// Converts an to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Color(Bgr24 source) => new Color(source); + + /// + /// Converts a to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Bgr24(Color color) => color.ToBgr24(); + /// /// Compares two objects for equality. /// diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs index 758be8043..ea7a96188 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Bgra32.cs @@ -98,6 +98,22 @@ namespace SixLabors.ImageSharp.PixelFormats set => this.Bgra = value; } + /// + /// Converts an to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Color(Bgra32 source) => new Color(source); + + /// + /// Converts a to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Bgra32(Color color) => color.ToBgra32(); + /// /// Compares two objects for equality. /// diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs index 1255f66d1..469dbbad4 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs @@ -53,6 +53,22 @@ namespace SixLabors.ImageSharp.PixelFormats this.B = b; } + /// + /// Converts an to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Color(Rgb24 source) => new Color(source); + + /// + /// Converts a to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Rgb24(Color color) => color.ToRgb24(); + /// /// Allows the implicit conversion of an instance of to a /// . diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs index 7367c4463..c58d17ef4 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs @@ -169,6 +169,22 @@ namespace SixLabors.ImageSharp.PixelFormats set => this.Rgba = value; } + /// + /// Converts an to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Color(Rgba32 source) => new Color(source); + + /// + /// Converts a to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Rgba32(Color color) => color.ToRgba32(); + /// /// Allows the implicit conversion of an instance of to a /// . diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs index cf9d4a552..978d9b015 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba64.cs @@ -46,6 +46,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// The green component. /// The blue component. /// The alpha component. + [MethodImpl(InliningOptions.ShortMethod)] public Rgba64(ushort r, ushort g, ushort b, ushort a) { this.R = r; @@ -58,6 +59,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// Initializes a new instance of the struct. /// /// A structure of 4 bytes in RGBA byte order. + [MethodImpl(InliningOptions.ShortMethod)] public Rgba64(Rgba32 source) { this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R); @@ -70,6 +72,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// Initializes a new instance of the struct. /// /// A structure of 4 bytes in BGRA byte order. + [MethodImpl(InliningOptions.ShortMethod)] public Rgba64(Bgra32 source) { this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R); @@ -82,6 +85,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// Initializes a new instance of the struct. /// /// A structure of 4 bytes in ARGB byte order. + [MethodImpl(InliningOptions.ShortMethod)] public Rgba64(Argb32 source) { this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R); @@ -94,6 +98,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// Initializes a new instance of the struct. /// /// A structure of 3 bytes in RGB byte order. + [MethodImpl(InliningOptions.ShortMethod)] public Rgba64(Rgb24 source) { this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R); @@ -106,6 +111,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// Initializes a new instance of the struct. /// /// A structure of 3 bytes in BGR byte order. + [MethodImpl(InliningOptions.ShortMethod)] public Rgba64(Bgr24 source) { this.R = ImageMaths.UpscaleFrom8BitTo16Bit(source.R); @@ -118,6 +124,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// Initializes a new instance of the struct. /// /// The . + [MethodImpl(InliningOptions.ShortMethod)] public Rgba64(Vector4 vector) { vector = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * Max; @@ -149,6 +156,22 @@ namespace SixLabors.ImageSharp.PixelFormats set => Unsafe.As(ref this) = value; } + /// + /// Converts an to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Color(Rgba64 source) => new Color(source); + + /// + /// Converts a to . + /// + /// The . + /// The . + [MethodImpl(InliningOptions.ShortMethod)] + public static implicit operator Rgba64(Color color) => color.ToPixel(); + /// /// Compares two objects for equality. /// diff --git a/tests/ImageSharp.Tests/Color/ColorTests.cs b/tests/ImageSharp.Tests/Color/ColorTests.cs index 729ef94e3..b2559d1d1 100644 --- a/tests/ImageSharp.Tests/Color/ColorTests.cs +++ b/tests/ImageSharp.Tests/Color/ColorTests.cs @@ -38,6 +38,8 @@ namespace SixLabors.ImageSharp.Tests Assert.False(c1 == c2); Assert.True(c1 != c2); + + Assert.False(c1.Equals(null)); } [Fact]