From 34a2acc6c4b65e606281547a1332cb4c556060db Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 22 Feb 2021 21:53:48 +0100 Subject: [PATCH] Added missing in-parameters --- .../Color/Behaviors/DefaultColorBehavior.cs | 12 ++--- .../Color/Behaviors/IColorBehavior.cs | 8 +-- RGB.NET.Core/Color/Color.cs | 13 ++--- RGB.NET.Core/Color/HSVColor.cs | 18 +++---- RGB.NET.Core/Color/RGBColor.cs | 50 +++++++++---------- RGB.NET.Core/Devices/AbstractRGBDevice.cs | 2 +- RGB.NET.Core/Devices/IRGBDevice.cs | 2 +- RGB.NET.Core/Extensions/ColorExtensions.cs | 2 +- RGB.NET.Core/Extensions/MathExtensions.cs | 2 +- RGB.NET.Core/Extensions/PointExtensions.cs | 14 ++++-- .../Extensions/RectangleExtensions.cs | 28 +++++------ RGB.NET.Core/Positioning/Point.cs | 19 +++---- RGB.NET.Core/Positioning/Rectangle.cs | 8 +-- RGB.NET.Core/Positioning/Rotation.cs | 14 +++--- RGB.NET.Core/Positioning/Size.cs | 23 +++++---- Tests/RGB.NET.Core.Tests/Color/ColorTest.cs | 24 ++++----- 16 files changed, 123 insertions(+), 116 deletions(-) diff --git a/RGB.NET.Core/Color/Behaviors/DefaultColorBehavior.cs b/RGB.NET.Core/Color/Behaviors/DefaultColorBehavior.cs index 53289c6..48ef24f 100644 --- a/RGB.NET.Core/Color/Behaviors/DefaultColorBehavior.cs +++ b/RGB.NET.Core/Color/Behaviors/DefaultColorBehavior.cs @@ -8,18 +8,18 @@ /// Converts the individual byte values of this to a human-readable string. /// /// A string that contains the individual byte values of this . For example "[A: 255, R: 255, G: 0, B: 0]". - public virtual string ToString(Color color) => $"[A: {color.GetA()}, R: {color.GetR()}, G: {color.GetG()}, B: {color.GetB()}]"; + public virtual string ToString(in Color color) => $"[A: {color.GetA()}, R: {color.GetR()}, G: {color.GetG()}, B: {color.GetB()}]"; /// /// Tests whether the specified object is a and is equivalent to this . /// /// The object to test. /// true if is a equivalent to this ; otherwise, false. - public virtual bool Equals(Color color, object? obj) + public virtual bool Equals(in Color color, object? obj) { - if (!(obj is Color)) return false; + if (!(obj is Color color2)) return false; - (float a, float r, float g, float b) = ((Color)obj).GetRGB(); + (float a, float r, float g, float b) = color2.GetRGB(); return color.A.EqualsInTolerance(a) && color.R.EqualsInTolerance(r) && color.G.EqualsInTolerance(g) && color.B.EqualsInTolerance(b); } @@ -27,7 +27,7 @@ /// Returns a hash code for this . /// /// An integer value that specifies the hash code for this . - public virtual int GetHashCode(Color color) + public virtual int GetHashCode(in Color color) { unchecked { @@ -43,7 +43,7 @@ /// Blends a over this color. /// /// The to blend. - public virtual Color Blend(Color baseColor, Color blendColor) + public virtual Color Blend(in Color baseColor, in Color blendColor) { if (blendColor.A.EqualsInTolerance(0)) return baseColor; diff --git a/RGB.NET.Core/Color/Behaviors/IColorBehavior.cs b/RGB.NET.Core/Color/Behaviors/IColorBehavior.cs index 3ee7b78..0e4a23f 100644 --- a/RGB.NET.Core/Color/Behaviors/IColorBehavior.cs +++ b/RGB.NET.Core/Color/Behaviors/IColorBehavior.cs @@ -2,12 +2,12 @@ { public interface IColorBehavior { - string ToString(Color color); + string ToString(in Color color); - bool Equals(Color color, object? obj); + bool Equals(in Color color, object? obj); - int GetHashCode(Color color); + int GetHashCode(in Color color); - Color Blend(Color baseColor, Color blendColor); + Color Blend(in Color baseColor, in Color blendColor); } } diff --git a/RGB.NET.Core/Color/Color.cs b/RGB.NET.Core/Color/Color.cs index 0948c47..9e8f83e 100644 --- a/RGB.NET.Core/Color/Color.cs +++ b/RGB.NET.Core/Color/Color.cs @@ -15,10 +15,11 @@ namespace RGB.NET.Core { #region Constants + private static readonly Color TRANSPARENT = new(0, 0, 0, 0); /// /// Gets an transparent color [A: 0, R: 0, G: 0, B: 0] /// - public static Color Transparent => new(0, 0, 0, 0); + public static ref readonly Color Transparent => ref TRANSPARENT; #endregion @@ -174,7 +175,7 @@ namespace RGB.NET.Core /// Initializes a new instance of the struct by cloning a existing . /// /// The the values are copied from. - public Color(Color color) + public Color(in Color color) : this(color.A, color.R, color.G, color.B) { } @@ -206,7 +207,7 @@ namespace RGB.NET.Core /// Blends a over this color, as defined by the current . /// /// The to blend. - public Color Blend(Color color) => Behavior.Blend(this, color); + public Color Blend(in Color color) => Behavior.Blend(this, color); #endregion @@ -218,7 +219,7 @@ namespace RGB.NET.Core /// The base color. /// The color to blend. /// The blended color. - public static Color operator +(Color color1, Color color2) => color1.Blend(color2); + public static Color operator +(in Color color1, in Color color2) => color1.Blend(color2); /// /// Returns a value that indicates whether two specified are equal. @@ -226,7 +227,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are equal; otherwise, false. - public static bool operator ==(Color color1, Color color2) => color1.Equals(color2); + public static bool operator ==(in Color color1, in Color color2) => color1.Equals(color2); /// /// Returns a value that indicates whether two specified are equal. @@ -234,7 +235,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are not equal; otherwise, false. - public static bool operator !=(Color color1, Color color2) => !(color1 == color2); + public static bool operator !=(in Color color1, in Color color2) => !(color1 == color2); /// /// Converts a of ARGB-components to a . diff --git a/RGB.NET.Core/Color/HSVColor.cs b/RGB.NET.Core/Color/HSVColor.cs index 40c89c8..492742f 100644 --- a/RGB.NET.Core/Color/HSVColor.cs +++ b/RGB.NET.Core/Color/HSVColor.cs @@ -13,21 +13,21 @@ namespace RGB.NET.Core /// /// /// - public static float GetHue(this Color color) => color.GetHSV().hue; + public static float GetHue(this in Color color) => color.GetHSV().hue; /// /// Gets the saturation component value (HSV-color space) of this in the range [0..1]. /// /// /// - public static float GetSaturation(this Color color) => color.GetHSV().saturation; + public static float GetSaturation(this in Color color) => color.GetHSV().saturation; /// /// Gets the value component value (HSV-color space) of this in the range [0..1]. /// /// /// - public static float GetValue(this Color color) => color.GetHSV().value; + public static float GetValue(this in Color color) => color.GetHSV().value; /// /// Gets the hue, saturation and value component values (HSV-color space) of this . @@ -37,7 +37,7 @@ namespace RGB.NET.Core /// /// /// - public static (float hue, float saturation, float value) GetHSV(this Color color) + public static (float hue, float saturation, float value) GetHSV(this in Color color) => CaclulateHSVFromRGB(color.R, color.G, color.B); #endregion @@ -51,7 +51,7 @@ namespace RGB.NET.Core /// The saturation value to add. /// The value value to add. /// The new color after the modification. - public static Color AddHSV(this Color color, float hue = 0, float saturation = 0, float value = 0) + public static Color AddHSV(this in Color color, float hue = 0, float saturation = 0, float value = 0) { (float cHue, float cSaturation, float cValue) = color.GetHSV(); return Create(color.A, cHue + hue, cSaturation + saturation, cValue + value); @@ -64,7 +64,7 @@ namespace RGB.NET.Core /// The saturation value to subtract. /// The value value to subtract. /// The new color after the modification. - public static Color SubtractHSV(this Color color, float hue = 0, float saturation = 0, float value = 0) + public static Color SubtractHSV(this in Color color, float hue = 0, float saturation = 0, float value = 0) { (float cHue, float cSaturation, float cValue) = color.GetHSV(); return Create(color.A, cHue - hue, cSaturation - saturation, cValue - value); @@ -77,7 +77,7 @@ namespace RGB.NET.Core /// The saturation value to multiply. /// The value value to multiply. /// The new color after the modification. - public static Color MultiplyHSV(this Color color, float hue = 1, float saturation = 1, float value = 1) + public static Color MultiplyHSV(this in Color color, float hue = 1, float saturation = 1, float value = 1) { (float cHue, float cSaturation, float cValue) = color.GetHSV(); return Create(color.A, cHue * hue, cSaturation * saturation, cValue * value); @@ -90,7 +90,7 @@ namespace RGB.NET.Core /// The saturation value to divide. /// The value value to divide. /// The new color after the modification. - public static Color DivideHSV(this Color color, float hue = 1, float saturation = 1, float value = 1) + public static Color DivideHSV(this in Color color, float hue = 1, float saturation = 1, float value = 1) { (float cHue, float cSaturation, float cValue) = color.GetHSV(); return Create(color.A, cHue / hue, cSaturation / saturation, cValue / value); @@ -103,7 +103,7 @@ namespace RGB.NET.Core /// The saturation value to set. /// The value value to set. /// The new color after the modification. - public static Color SetHSV(this Color color, float? hue = null, float? saturation = null, float? value = null) + public static Color SetHSV(this in Color color, float? hue = null, float? saturation = null, float? value = null) { (float cHue, float cSaturation, float cValue) = color.GetHSV(); return Create(color.A, hue ?? cHue, saturation ?? cSaturation, value ?? cValue); diff --git a/RGB.NET.Core/Color/RGBColor.cs b/RGB.NET.Core/Color/RGBColor.cs index 17919ec..9f4c2cb 100644 --- a/RGB.NET.Core/Color/RGBColor.cs +++ b/RGB.NET.Core/Color/RGBColor.cs @@ -13,35 +13,35 @@ namespace RGB.NET.Core /// /// /// - public static byte GetA(this Color color) => color.A.GetByteValueFromPercentage(); + public static byte GetA(this in Color color) => color.A.GetByteValueFromPercentage(); /// /// Gets the R component value of this as byte in the range [0..255]. /// /// /// - public static byte GetR(this Color color) => color.R.GetByteValueFromPercentage(); + public static byte GetR(this in Color color) => color.R.GetByteValueFromPercentage(); /// /// Gets the G component value of this as byte in the range [0..255]. /// /// /// - public static byte GetG(this Color color) => color.G.GetByteValueFromPercentage(); + public static byte GetG(this in Color color) => color.G.GetByteValueFromPercentage(); /// /// Gets the B component value of this as byte in the range [0..255]. /// /// /// - public static byte GetB(this Color color) => color.B.GetByteValueFromPercentage(); + public static byte GetB(this in Color color) => color.B.GetByteValueFromPercentage(); /// /// Gets the A, R, G and B component value of this as byte in the range [0..255]. /// /// /// - public static (byte a, byte r, byte g, byte b) GetRGBBytes(this Color color) + public static (byte a, byte r, byte g, byte b) GetRGBBytes(this in Color color) => (color.GetA(), color.GetR(), color.GetG(), color.GetB()); /// @@ -49,7 +49,7 @@ namespace RGB.NET.Core /// /// /// - public static (float a, float r, float g, float b) GetRGB(this Color color) + public static (float a, float r, float g, float b) GetRGB(this in Color color) => (color.A, color.R, color.G, color.B); #endregion @@ -65,7 +65,7 @@ namespace RGB.NET.Core /// The green value to add. /// The blue value to add. /// The new color after the modification. - public static Color AddRGB(this Color color, int r = 0, int g = 0, int b = 0) + public static Color AddRGB(this in Color color, int r = 0, int g = 0, int b = 0) => new(color.A, color.GetR() + r, color.GetG() + g, color.GetB() + b); /// @@ -75,7 +75,7 @@ namespace RGB.NET.Core /// The green value to add. /// The blue value to add. /// The new color after the modification. - public static Color AddRGB(this Color color, float r = 0, float g = 0, float b = 0) + public static Color AddRGB(this in Color color, float r = 0, float g = 0, float b = 0) => new(color.A, color.R + r, color.G + g, color.B + b); /// @@ -83,7 +83,7 @@ namespace RGB.NET.Core /// /// The alpha value to add. /// The new color after the modification. - public static Color AddA(this Color color, int a) + public static Color AddA(this in Color color, int a) => new(color.GetA() + a, color.R, color.G, color.B); /// @@ -91,7 +91,7 @@ namespace RGB.NET.Core /// /// The alpha value to add. /// The new color after the modification. - public static Color AddA(this Color color, float a) + public static Color AddA(this in Color color, float a) => new(color.A + a, color.R, color.G, color.B); #endregion @@ -105,7 +105,7 @@ namespace RGB.NET.Core /// The green value to subtract. /// The blue value to subtract. /// The new color after the modification. - public static Color SubtractRGB(this Color color, int r = 0, int g = 0, int b = 0) + public static Color SubtractRGB(this in Color color, int r = 0, int g = 0, int b = 0) => new(color.A, color.GetR() - r, color.GetG() - g, color.GetB() - b); /// @@ -115,7 +115,7 @@ namespace RGB.NET.Core /// The green value to subtract. /// The blue value to subtract. /// The new color after the modification. - public static Color SubtractRGB(this Color color, float r = 0, float g = 0, float b = 0) + public static Color SubtractRGB(this in Color color, float r = 0, float g = 0, float b = 0) => new(color.A, color.R - r, color.G - g, color.B - b); /// @@ -123,7 +123,7 @@ namespace RGB.NET.Core /// /// The alpha value to subtract. /// The new color after the modification. - public static Color SubtractA(this Color color, int a) + public static Color SubtractA(this in Color color, int a) => new(color.GetA() - a, color.R, color.G, color.B); /// @@ -131,7 +131,7 @@ namespace RGB.NET.Core /// /// The alpha value to subtract. /// The new color after the modification. - public static Color SubtractA(this Color color, float aPercent) + public static Color SubtractA(this in Color color, float aPercent) => new(color.A - aPercent, color.R, color.G, color.B); #endregion @@ -145,7 +145,7 @@ namespace RGB.NET.Core /// The green value to multiply. /// The blue value to multiply. /// The new color after the modification. - public static Color MultiplyRGB(this Color color, float r = 1, float g = 1, float b = 1) + public static Color MultiplyRGB(this in Color color, float r = 1, float g = 1, float b = 1) => new(color.A, color.R * r, color.G * g, color.B * b); /// @@ -153,7 +153,7 @@ namespace RGB.NET.Core /// /// The alpha value to multiply. /// The new color after the modification. - public static Color MultiplyA(this Color color, float a) + public static Color MultiplyA(this in Color color, float a) => new(color.A * a, color.R, color.G, color.B); #endregion @@ -167,7 +167,7 @@ namespace RGB.NET.Core /// The green value to divide. /// The blue value to divide. /// The new color after the modification. - public static Color DivideRGB(this Color color, float r = 1, float g = 1, float b = 1) + public static Color DivideRGB(this in Color color, float r = 1, float g = 1, float b = 1) => new(color.A, color.R / r, color.G / g, color.B / b); /// @@ -175,7 +175,7 @@ namespace RGB.NET.Core /// /// The alpha value to divide. /// The new color after the modification. - public static Color DivideA(this Color color, float a) + public static Color DivideA(this in Color color, float a) => new(color.A / a, color.R, color.G, color.B); #endregion @@ -189,7 +189,7 @@ namespace RGB.NET.Core /// The green value to set. /// The blue value to set. /// The new color after the modification. - public static Color SetRGB(this Color color, byte? r = null, byte? g = null, byte? b = null) + public static Color SetRGB(this in Color color, byte? r = null, byte? g = null, byte? b = null) => new(color.A, r ?? color.GetR(), g ?? color.GetG(), b ?? color.GetB()); /// @@ -199,7 +199,7 @@ namespace RGB.NET.Core /// The green value to set. /// The blue value to set. /// The new color after the modification. - public static Color SetRGB(this Color color, int? r = null, int? g = null, int? b = null) + public static Color SetRGB(this in Color color, int? r = null, int? g = null, int? b = null) => new(color.A, r ?? color.GetR(), g ?? color.GetG(), b ?? color.GetB()); /// @@ -209,7 +209,7 @@ namespace RGB.NET.Core /// The green value to set. /// The blue value to set. /// The new color after the modification. - public static Color SetRGB(this Color color, float? r = null, float? g = null, float? b = null) + public static Color SetRGB(this in Color color, float? r = null, float? g = null, float? b = null) => new(color.A, r ?? color.R, g ?? color.G, b ?? color.B); /// @@ -217,14 +217,14 @@ namespace RGB.NET.Core /// /// The alpha value to set. /// The new color after the modification. - public static Color SetA(this Color color, int a) => new(a, color.R, color.G, color.B); + public static Color SetA(this in Color color, int a) => new(a, color.R, color.G, color.B); /// /// Sets the given alpha value of this color. /// /// The alpha value to set. /// The new color after the modification. - public static Color SetA(this Color color, float a) => new(a, color.R, color.G, color.B); + public static Color SetA(this in Color color, float a) => new(a, color.R, color.G, color.B); #endregion @@ -236,13 +236,13 @@ namespace RGB.NET.Core /// Gets the current color as a RGB-HEX-string. /// /// The RGB-HEX-string. - public static string AsRGBHexString(this Color color, bool leadingHash = true) => (leadingHash ? "#" : "") + ConversionHelper.ToHex(color.GetR(), color.GetG(), color.GetB()); + public static string AsRGBHexString(this in Color color, bool leadingHash = true) => (leadingHash ? "#" : "") + ConversionHelper.ToHex(color.GetR(), color.GetG(), color.GetB()); /// /// Gets the current color as a ARGB-HEX-string. /// /// The ARGB-HEX-string. - public static string AsARGBHexString(this Color color, bool leadingHash = true) => (leadingHash ? "#" : "") + ConversionHelper.ToHex(color.GetA(), color.GetR(), color.GetG(), color.GetB()); + public static string AsARGBHexString(this in Color color, bool leadingHash = true) => (leadingHash ? "#" : "") + ConversionHelper.ToHex(color.GetA(), color.GetR(), color.GetG(), color.GetB()); #endregion diff --git a/RGB.NET.Core/Devices/AbstractRGBDevice.cs b/RGB.NET.Core/Devices/AbstractRGBDevice.cs index f0083c8..0b6fe09 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDevice.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDevice.cs @@ -114,7 +114,7 @@ namespace RGB.NET.Core /// The location of the to initialize. /// The size of the to initialize. /// The initialized led. - public virtual Led? AddLed(LedId ledId, Point location, Size size, object? customData = null) + public virtual Led? AddLed(LedId ledId, in Point location, in Size size, object? customData = null) { if ((ledId == LedId.Invalid) || LedMapping.ContainsKey(ledId)) return null; diff --git a/RGB.NET.Core/Devices/IRGBDevice.cs b/RGB.NET.Core/Devices/IRGBDevice.cs index 6a83e50..0ad1dc7 100644 --- a/RGB.NET.Core/Devices/IRGBDevice.cs +++ b/RGB.NET.Core/Devices/IRGBDevice.cs @@ -58,7 +58,7 @@ namespace RGB.NET.Core /// Specifies whether all (including clean ones) should be updated. void Update(bool flushLeds = false); - Led? AddLed(LedId ledId, Point location, Size size, object? customData = null); + Led? AddLed(LedId ledId, in Point location, in Size size, object? customData = null); Led? RemoveLed(LedId ledId); diff --git a/RGB.NET.Core/Extensions/ColorExtensions.cs b/RGB.NET.Core/Extensions/ColorExtensions.cs index 15d0c80..b905baf 100644 --- a/RGB.NET.Core/Extensions/ColorExtensions.cs +++ b/RGB.NET.Core/Extensions/ColorExtensions.cs @@ -13,7 +13,7 @@ namespace RGB.NET.Core /// The start color of the distance calculation. /// The end color fot the distance calculation. /// - public static double DistanceTo(this Color color1, Color color2) + public static double DistanceTo(this in Color color1, in Color color2) { (_, byte r1, byte g1, byte b1) = color1.GetRGBBytes(); (_, byte r2, byte g2, byte b2) = color2.GetRGBBytes(); diff --git a/RGB.NET.Core/Extensions/MathExtensions.cs b/RGB.NET.Core/Extensions/MathExtensions.cs index d47db3f..ad098bf 100644 --- a/RGB.NET.Core/Extensions/MathExtensions.cs +++ b/RGB.NET.Core/Extensions/MathExtensions.cs @@ -13,7 +13,7 @@ namespace RGB.NET.Core /// /// Defines the precision RGB.NET processes floating point comparisons in. /// - public const float TOLERANCE = 1E-10f; + public const float TOLERANCE = 1E-7f; #endregion diff --git a/RGB.NET.Core/Extensions/PointExtensions.cs b/RGB.NET.Core/Extensions/PointExtensions.cs index 00436c1..cdc04f7 100644 --- a/RGB.NET.Core/Extensions/PointExtensions.cs +++ b/RGB.NET.Core/Extensions/PointExtensions.cs @@ -13,7 +13,7 @@ namespace RGB.NET.Core /// The x-ammount to move. /// The y-ammount to move. /// The new location of the point. - public static Point Translate(this Point point, float x = 0, float y = 0) => new(point.X + x, point.Y + y); + public static Point Translate(this in Point point, float x = 0, float y = 0) => new(point.X + x, point.Y + y); /// /// Rotates the specified by the given amuont around the given origin. @@ -22,14 +22,18 @@ namespace RGB.NET.Core /// The rotation. /// The origin to rotate around. [0,0] if not set. /// The new location of the point. - public static Point Rotate(this Point point, Rotation rotation, Point origin = new()) + public static Point Rotate(this in Point point, in Rotation rotation, in Point origin = new()) { float sin = MathF.Sin(rotation.Radians); float cos = MathF.Cos(rotation.Radians); - point = new Point(point.X - origin.X, point.Y - origin.Y); - point = new Point((point.X * cos) - (point.Y * sin), (point.X * sin) + (point.Y * cos)); - return new Point(point.X + origin.X, point.Y + origin.Y); ; + float x = point.X - origin.X; + float y = point.Y - origin.Y; + + x = (x * cos) - (y * sin); + y = (x * sin) + (y * cos); + + return new Point(x + origin.X, y + origin.Y); ; } #endregion diff --git a/RGB.NET.Core/Extensions/RectangleExtensions.cs b/RGB.NET.Core/Extensions/RectangleExtensions.cs index 634c3f6..ce5745e 100644 --- a/RGB.NET.Core/Extensions/RectangleExtensions.cs +++ b/RGB.NET.Core/Extensions/RectangleExtensions.cs @@ -12,7 +12,7 @@ namespace RGB.NET.Core /// The rectangle to modify. /// The new location of the rectangle. /// The modified . - public static Rectangle SetLocation(this Rectangle rect, Point location) => new(location, rect.Size); + public static Rectangle SetLocation(this in Rectangle rect, in Point location) => new(location, rect.Size); /// /// Sets the of the of the given rectangle. @@ -20,7 +20,7 @@ namespace RGB.NET.Core /// The rectangle to modify. /// The new x-location of the rectangle. /// The modified . - public static Rectangle SetX(this Rectangle rect, float x) => new(new Point(x, rect.Location.Y), rect.Size); + public static Rectangle SetX(this in Rectangle rect, float x) => new(new Point(x, rect.Location.Y), rect.Size); /// /// Sets the of the of the given rectangle. @@ -28,7 +28,7 @@ namespace RGB.NET.Core /// The rectangle to modify. /// The new y-location of the rectangle. /// The modified . - public static Rectangle SetY(this Rectangle rect, float y) => new(new Point(rect.Location.X, y), rect.Size); + public static Rectangle SetY(this in Rectangle rect, float y) => new(new Point(rect.Location.X, y), rect.Size); /// /// Sets the of the given rectangle. @@ -36,7 +36,7 @@ namespace RGB.NET.Core /// The rectangle to modify. /// The new size of the rectangle. /// The modified . - public static Rectangle SetSize(this Rectangle rect, Size size) => new(rect.Location, size); + public static Rectangle SetSize(this in Rectangle rect, in Size size) => new(rect.Location, size); /// /// Sets the of the of the given rectangle. @@ -44,7 +44,7 @@ namespace RGB.NET.Core /// The rectangle to modify. /// The new width of the rectangle. /// The modified . - public static Rectangle SetWidth(this Rectangle rect, float width) => new(rect.Location, new Size(width, rect.Size.Height)); + public static Rectangle SetWidth(this in Rectangle rect, float width) => new(rect.Location, new Size(width, rect.Size.Height)); /// /// Sets the of the of the given rectangle. @@ -52,14 +52,14 @@ namespace RGB.NET.Core /// The rectangle to modify. /// The new height of the rectangle. /// The modified . - public static Rectangle SetHeight(this Rectangle rect, float height) => new(rect.Location, new Size(rect.Size.Width, height)); + public static Rectangle SetHeight(this in Rectangle rect, float height) => new(rect.Location, new Size(rect.Size.Width, height)); /// /// Calculates the percentage of intersection of a rectangle. /// /// The intersecting rectangle. /// The percentage of intersection. - public static float CalculateIntersectPercentage(this Rectangle rect, in Rectangle intersectingRect) + public static float CalculateIntersectPercentage(this in Rectangle rect, in Rectangle intersectingRect) { if (rect.IsEmpty || intersectingRect.IsEmpty) return 0; @@ -72,7 +72,7 @@ namespace RGB.NET.Core /// /// The intersecting /// A new representing the intersection this and the one provided as parameter. - public static Rectangle CalculateIntersection(this Rectangle rect, in Rectangle intersectingRectangle) + public static Rectangle CalculateIntersection(this in Rectangle rect, in Rectangle intersectingRectangle) { float x1 = Math.Max(rect.Location.X, intersectingRectangle.Location.X); float x2 = Math.Min(rect.Location.X + rect.Size.Width, intersectingRectangle.Location.X + intersectingRectangle.Size.Width); @@ -91,7 +91,7 @@ namespace RGB.NET.Core /// /// The to test. /// true if the rectangle contains the given point; otherwise false. - public static bool Contains(this Rectangle rect, in Point point) => rect.Contains(point.X, point.Y); + public static bool Contains(this in Rectangle rect, in Point point) => rect.Contains(point.X, point.Y); /// /// Determines if the specified location is contained within this . @@ -99,7 +99,7 @@ namespace RGB.NET.Core /// The X-location to test. /// The Y-location to test. /// true if the rectangle contains the given coordinates; otherwise false. - public static bool Contains(this Rectangle rect, float x, float y) => (rect.Location.X <= x) && (x < (rect.Location.X + rect.Size.Width)) + public static bool Contains(this in Rectangle rect, float x, float y) => (rect.Location.X <= x) && (x < (rect.Location.X + rect.Size.Width)) && (rect.Location.Y <= y) && (y < (rect.Location.Y + rect.Size.Height)); /// @@ -107,7 +107,7 @@ namespace RGB.NET.Core /// /// The to test. /// true if the rectangle contains the given rect; otherwise false. - public static bool Contains(this Rectangle rect, in Rectangle rect2) => (rect.Location.X <= rect2.Location.X) && ((rect2.Location.X + rect2.Size.Width) <= (rect.Location.X + rect.Size.Width)) + public static bool Contains(this in Rectangle rect, in Rectangle rect2) => (rect.Location.X <= rect2.Location.X) && ((rect2.Location.X + rect2.Size.Width) <= (rect.Location.X + rect.Size.Width)) && (rect.Location.Y <= rect2.Location.Y) && ((rect2.Location.Y + rect2.Size.Height) <= (rect.Location.Y + rect.Size.Height)); /// @@ -116,7 +116,7 @@ namespace RGB.NET.Core /// The to move. /// The amount to move. /// The moved rectangle. - public static Rectangle Translate(this Rectangle rect, in Point point) => rect.Translate(point.X, point.Y); + public static Rectangle Translate(this in Rectangle rect, in Point point) => rect.Translate(point.X, point.Y); /// /// Moves the specified by the given amount. @@ -125,7 +125,7 @@ namespace RGB.NET.Core /// The x-ammount to move. /// The y-ammount to move. /// The moved rectangle. - public static Rectangle Translate(this Rectangle rect, float x = 0, float y = 0) => new(rect.Location.Translate(x, y), rect.Size); + public static Rectangle Translate(this in Rectangle rect, float x = 0, float y = 0) => new(rect.Location.Translate(x, y), rect.Size); /// /// Rotates the specified by the given amuont around the given origin. @@ -141,7 +141,7 @@ namespace RGB.NET.Core /// The rotation. /// The origin to rotate around. [0,0] if not set. /// A array of containing the new locations of the corners of the original rectangle. - public static Point[] Rotate(this Rectangle rect, in Rotation rotation, in Point origin = new()) + public static Point[] Rotate(this in Rectangle rect, in Rotation rotation, in Point origin = new()) { Point[] points = { rect.Location, // top left diff --git a/RGB.NET.Core/Positioning/Point.cs b/RGB.NET.Core/Positioning/Point.cs index aa533f4..5f04a76 100644 --- a/RGB.NET.Core/Positioning/Point.cs +++ b/RGB.NET.Core/Positioning/Point.cs @@ -13,10 +13,11 @@ namespace RGB.NET.Core { #region Constants + private static readonly Point INVALID = new(float.NaN, float.NaN); /// /// Gets a [NaN,NaN]-Point. /// - public static Point Invalid => new(float.NaN, float.NaN); + public static ref readonly Point Invalid => ref INVALID; #endregion @@ -95,7 +96,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are equal; otherwise, false. - public static bool operator ==(Point point1, Point point2) => point1.Equals(point2); + public static bool operator ==(in Point point1, in Point point2) => point1.Equals(point2); /// /// Returns a value that indicates whether two specified are equal. @@ -103,7 +104,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are not equal; otherwise, false. - public static bool operator !=(Point point1, Point point2) => !(point1 == point2); + public static bool operator !=(in Point point1, in Point point2) => !(point1 == point2); /// /// Returns a new representing the addition of the two provided . @@ -111,7 +112,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the addition of the two provided . - public static Point operator +(Point point1, Point point2) => new(point1.X + point2.X, point1.Y + point2.Y); + public static Point operator +(in Point point1, in Point point2) => new(point1.X + point2.X, point1.Y + point2.Y); /// /// Returns a new created from the provided and . @@ -119,7 +120,7 @@ namespace RGB.NET.Core /// The of the rectangle. /// The of the rectangle. /// The rectangle created from the provided and . - public static Rectangle operator +(Point point, Size size) => new(point, size); + public static Rectangle operator +(in Point point, in Size size) => new(point, size); /// /// Returns a new representing the subtraction of the two provided . @@ -127,7 +128,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the subtraction of the two provided . - public static Point operator -(Point point1, Point point2) => new(point1.X - point2.X, point1.Y - point2.Y); + public static Point operator -(in Point point1, in Point point2) => new(point1.X - point2.X, point1.Y - point2.Y); /// /// Returns a new representing the multiplication of the two provided . @@ -135,7 +136,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the multiplication of the two provided . - public static Point operator *(Point point1, Point point2) => new(point1.X * point2.X, point1.Y * point2.Y); + public static Point operator *(in Point point1, in Point point2) => new(point1.X * point2.X, point1.Y * point2.Y); /// /// Returns a new representing the division of the two provided . @@ -143,7 +144,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the division of the two provided . - public static Point operator /(Point point1, Point point2) + public static Point operator /(in Point point1, in Point point2) { if (point2.X.EqualsInTolerance(0) || point2.Y.EqualsInTolerance(0)) return Invalid; return new Point(point1.X / point2.X, point1.Y / point2.Y); @@ -155,7 +156,7 @@ namespace RGB.NET.Core /// The . /// The . /// A new representing the multiplication of the and the provided . - public static Point operator *(Point point, Scale scale) => new(point.X * scale.Horizontal, point.Y * scale.Vertical); + public static Point operator *(in Point point, in Scale scale) => new(point.X * scale.Horizontal, point.Y * scale.Vertical); #endregion } diff --git a/RGB.NET.Core/Positioning/Rectangle.cs b/RGB.NET.Core/Positioning/Rectangle.cs index eddffb5..3c545e6 100644 --- a/RGB.NET.Core/Positioning/Rectangle.cs +++ b/RGB.NET.Core/Positioning/Rectangle.cs @@ -156,7 +156,7 @@ namespace RGB.NET.Core #region Methods - private static (Point location, Size size) InitializeFromPoints(Point point1, Point point2) + private static (Point location, Size size) InitializeFromPoints(in Point point1, in Point point2) { float posX = Math.Min(point1.X, point2.X); float posY = Math.Min(point1.Y, point2.Y); @@ -212,7 +212,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are equal; otherwise, false. - public static bool operator ==(Rectangle rectangle1, Rectangle rectangle2) => rectangle1.Equals(rectangle2); + public static bool operator ==(in Rectangle rectangle1, in Rectangle rectangle2) => rectangle1.Equals(rectangle2); /// /// Returns a value that indicates whether two specified are equal. @@ -220,10 +220,10 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are not equal; otherwise, false. - public static bool operator !=(Rectangle rectangle1, Rectangle rectangle2) => !(rectangle1 == rectangle2); + public static bool operator !=(in Rectangle rectangle1, in Rectangle rectangle2) => !(rectangle1 == rectangle2); // DarthAffe 20.02.2021: Used for normalization - public static Rectangle operator /(Rectangle rectangle1, Rectangle rectangle2) + public static Rectangle operator /(in Rectangle rectangle1, in Rectangle rectangle2) { float x = rectangle1.Location.X / (rectangle2.Size.Width - rectangle2.Location.X); float y = rectangle1.Location.Y / (rectangle2.Size.Height - rectangle2.Location.Y); diff --git a/RGB.NET.Core/Positioning/Rotation.cs b/RGB.NET.Core/Positioning/Rotation.cs index d0de9d2..39a5315 100644 --- a/RGB.NET.Core/Positioning/Rotation.cs +++ b/RGB.NET.Core/Positioning/Rotation.cs @@ -103,7 +103,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are equal; otherwise, false. - public static bool operator ==(Rotation rotation1, Rotation rotation2) => rotation1.Equals(rotation2); + public static bool operator ==(in Rotation rotation1, in Rotation rotation2) => rotation1.Equals(rotation2); /// /// Returns a value that indicates whether two specified are equal. @@ -111,7 +111,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are not equal; otherwise, false. - public static bool operator !=(Rotation rotation1, Rotation rotation2) => !(rotation1 == rotation2); + public static bool operator !=(in Rotation rotation1, in Rotation rotation2) => !(rotation1 == rotation2); /// /// Returns a new representing the addition of the and the provided value. @@ -119,7 +119,7 @@ namespace RGB.NET.Core /// The . /// The value to add. /// A new representing the addition of the and the provided value. - public static Rotation operator +(Rotation rotation, float value) => new(rotation.Degrees + value); + public static Rotation operator +(in Rotation rotation, float value) => new(rotation.Degrees + value); /// /// Returns a new representing the subtraction of the and the provided value. @@ -127,7 +127,7 @@ namespace RGB.NET.Core /// The . /// The value to substract. /// A new representing the subtraction of the and the provided value. - public static Rotation operator -(Rotation rotation, float value) => new(rotation.Degrees - value); + public static Rotation operator -(in Rotation rotation, float value) => new(rotation.Degrees - value); /// /// Returns a new representing the multiplication of the and the provided value. @@ -135,7 +135,7 @@ namespace RGB.NET.Core /// The . /// The value to multiply with. /// A new representing the multiplication of the and the provided value. - public static Rotation operator *(Rotation rotation, float value) => new(rotation.Degrees * value); + public static Rotation operator *(in Rotation rotation, float value) => new(rotation.Degrees * value); /// /// Returns a new representing the division of the and the provided value. @@ -143,7 +143,7 @@ namespace RGB.NET.Core /// The . /// The value to device with. /// A new representing the division of the and the provided value. - public static Rotation operator /(Rotation rotation, float value) => value.EqualsInTolerance(0) ? new Rotation(0) : new Rotation(rotation.Degrees / value); + public static Rotation operator /(in Rotation rotation, float value) => value.EqualsInTolerance(0) ? new Rotation(0) : new Rotation(rotation.Degrees / value); /// /// Converts a float to a . @@ -155,7 +155,7 @@ namespace RGB.NET.Core /// Converts to a float representing the rotation in degrees. /// /// The rotatio to convert. - public static implicit operator float(Rotation rotation) => rotation.Degrees; + public static implicit operator float(in Rotation rotation) => rotation.Degrees; #endregion } diff --git a/RGB.NET.Core/Positioning/Size.cs b/RGB.NET.Core/Positioning/Size.cs index 690dc6b..3a8c279 100644 --- a/RGB.NET.Core/Positioning/Size.cs +++ b/RGB.NET.Core/Positioning/Size.cs @@ -13,10 +13,11 @@ namespace RGB.NET.Core { #region Constants + private static readonly Size INVALID = new(float.NaN, float.NaN); /// /// Gets a [NaN,NaN]-Size. /// - public static Size Invalid => new(float.NaN, float.NaN); + public static ref readonly Size Invalid => ref INVALID; #endregion @@ -115,7 +116,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are equal; otherwise, false. - public static bool operator ==(Size size1, Size size2) => size1.Equals(size2); + public static bool operator ==(in Size size1, in Size size2) => size1.Equals(size2); /// /// Returns a value that indicates whether two specified are equal. @@ -123,7 +124,7 @@ namespace RGB.NET.Core /// The first to compare. /// The second to compare. /// true if and are not equal; otherwise, false. - public static bool operator !=(Size size1, Size size2) => !(size1 == size2); + public static bool operator !=(in Size size1, in Size size2) => !(size1 == size2); /// /// Returns a new representing the addition of the two provided . @@ -131,7 +132,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the addition of the two provided . - public static Size operator +(Size size1, Size size2) => new(size1.Width + size2.Width, size1.Height + size2.Height); + public static Size operator +(in Size size1, in Size size2) => new(size1.Width + size2.Width, size1.Height + size2.Height); /// /// Returns a new created from the provided and . @@ -139,7 +140,7 @@ namespace RGB.NET.Core /// The of the rectangle. /// The of the rectangle. /// The rectangle created from the provided and . - public static Rectangle operator +(Size size, Point point) => new(point, size); + public static Rectangle operator +(in Size size, in Point point) => new(point, size); /// /// Returns a new representing the subtraction of the two provided . @@ -147,7 +148,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the subtraction of the two provided . - public static Size operator -(Size size1, Size size2) => new(size1.Width - size2.Width, size1.Height - size2.Height); + public static Size operator -(in Size size1, in Size size2) => new(size1.Width - size2.Width, size1.Height - size2.Height); /// /// Returns a new representing the multiplication of the two provided . @@ -155,7 +156,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the multiplication of the two provided . - public static Size operator *(Size size1, Size size2) => new(size1.Width * size2.Width, size1.Height * size2.Height); + public static Size operator *(in Size size1, in Size size2) => new(size1.Width * size2.Width, size1.Height * size2.Height); /// /// Returns a new representing the multiplication of the and the provided factor. @@ -163,7 +164,7 @@ namespace RGB.NET.Core /// The . /// The factor by which the should be multiplied. /// A new representing the multiplication of the and the provided factor. - public static Size operator *(Size size, float factor) => new(size.Width * factor, size.Height * factor); + public static Size operator *(in Size size, float factor) => new(size.Width * factor, size.Height * factor); /// /// Returns a new representing the division of the two provided . @@ -171,7 +172,7 @@ namespace RGB.NET.Core /// The first . /// The second . /// A new representing the division of the two provided . - public static Size operator /(Size size1, Size size2) + public static Size operator /(in Size size1, in Size size2) => size2.Width.EqualsInTolerance(0) || size2.Height.EqualsInTolerance(0) ? Invalid : new Size(size1.Width / size2.Width, size1.Height / size2.Height); @@ -181,7 +182,7 @@ namespace RGB.NET.Core /// The . /// The factor by which the should be divided. /// A new representing the division of the and the provided factor. - public static Size operator /(Size size, float factor) => factor.EqualsInTolerance(0) ? Invalid : new Size(size.Width / factor, size.Height / factor); + public static Size operator /(in Size size, float factor) => factor.EqualsInTolerance(0) ? Invalid : new Size(size.Width / factor, size.Height / factor); /// /// Returns a new representing the multiplication of the and the given . @@ -189,7 +190,7 @@ namespace RGB.NET.Core /// The to scale. /// The scaling factor. /// A new representing the multiplication of the and the given . - public static Size operator *(Size size, Scale scale) => new(size.Width * scale.Horizontal, size.Height * scale.Vertical); + public static Size operator *(in Size size, in Scale scale) => new(size.Width * scale.Horizontal, size.Height * scale.Vertical); #endregion } diff --git a/Tests/RGB.NET.Core.Tests/Color/ColorTest.cs b/Tests/RGB.NET.Core.Tests/Color/ColorTest.cs index 67b5b77..4b22146 100644 --- a/Tests/RGB.NET.Core.Tests/Color/ColorTest.cs +++ b/Tests/RGB.NET.Core.Tests/Color/ColorTest.cs @@ -324,13 +324,13 @@ namespace RGB.NET.Core.Tests.Color Assert.AreEqual(1, color2.A); Core.Color color3 = new(128, 0, 0, 0); - Assert.AreEqual(128 / 255.0, color3.A); + Assert.AreEqual(128 / 255.0f, color3.A); Core.Color color4 = new(30, 0, 0, 0); - Assert.AreEqual(30 / 255.0, color4.A); + Assert.AreEqual(30 / 255.0f, color4.A); Core.Color color5 = new(201, 0, 0, 0); - Assert.AreEqual(201 / 255.0, color5.A); + Assert.AreEqual(201 / 255.0f, color5.A); } [TestMethod] @@ -343,13 +343,13 @@ namespace RGB.NET.Core.Tests.Color Assert.AreEqual(1, color2.R); Core.Color color3 = new(0, 128, 0, 0); - Assert.AreEqual(128 / 255.0, color3.R); + Assert.AreEqual(128 / 255.0f, color3.R); Core.Color color4 = new(0, 30, 0, 0); - Assert.AreEqual(30 / 255.0, color4.R); + Assert.AreEqual(30 / 255.0f, color4.R); Core.Color color5 = new(0, 201, 0, 0); - Assert.AreEqual(201 / 255.0, color5.R); + Assert.AreEqual(201 / 255.0f, color5.R); } [TestMethod] @@ -362,13 +362,13 @@ namespace RGB.NET.Core.Tests.Color Assert.AreEqual(1, color2.G); Core.Color color3 = new(0, 0, 128, 0); - Assert.AreEqual(128 / 255.0, color3.G); + Assert.AreEqual(128 / 255.0f, color3.G); Core.Color color4 = new(0, 0, 30, 0); - Assert.AreEqual(30 / 255.0, color4.G); + Assert.AreEqual(30 / 255.0f, color4.G); Core.Color color5 = new(0, 0, 201, 0); - Assert.AreEqual(201 / 255.0, color5.G); + Assert.AreEqual(201 / 255.0f, color5.G); } [TestMethod] @@ -381,13 +381,13 @@ namespace RGB.NET.Core.Tests.Color Assert.AreEqual(1, color2.B); Core.Color color3 = new(0, 0, 0, 128); - Assert.AreEqual(128 / 255.0, color3.B); + Assert.AreEqual(128 / 255.0f, color3.B); Core.Color color4 = new(0, 0, 0, 30); - Assert.AreEqual(30 / 255.0, color4.B); + Assert.AreEqual(30 / 255.0f, color4.B); Core.Color color5 = new(0, 0, 0, 201); - Assert.AreEqual(201 / 255.0, color5.B); + Assert.AreEqual(201 / 255.0f, color5.B); } #endregion