Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit a8feeb0

Browse files
authored
Merge pull request #182 from vpenades/main
Added System.Numerics.Vectors constructors and operators
2 parents 4ce7c8b + 302d411 commit a8feeb0

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

src/Microsoft.Maui.Graphics/AffineTransform.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Numerics;
23

34
namespace Microsoft.Maui.Graphics
45
{
@@ -52,6 +53,16 @@ public AffineTransform(float[] matrix)
5253
}
5354
}
5455

56+
public AffineTransform(in Matrix3x2 matrix)
57+
{
58+
_m00 = matrix.M11;
59+
_m10 = matrix.M12;
60+
_m01 = matrix.M21;
61+
_m11 = matrix.M22;
62+
_m02 = matrix.M31;
63+
_m12 = matrix.M32;
64+
}
65+
5566
public void SetMatrix(float m00, float m10, float m01, float m11, float m02, float m12)
5667
{
5768
_m00 = m00;
@@ -62,6 +73,16 @@ public void SetMatrix(float m00, float m10, float m01, float m11, float m02, flo
6273
_m12 = m12;
6374
}
6475

76+
public void SetMatrix(in Matrix3x2 matrix)
77+
{
78+
_m00 = matrix.M11;
79+
_m10 = matrix.M12;
80+
_m01 = matrix.M21;
81+
_m11 = matrix.M22;
82+
_m02 = matrix.M31;
83+
_m12 = matrix.M32;
84+
}
85+
6586
public float ScaleX => _m00;
6687

6788
public float ScaleY => _m11;
@@ -102,6 +123,16 @@ public void SetTransform(float m00, float m10, float m01, float m11, float m02,
102123
_m12 = m12;
103124
}
104125

126+
public void SetTransform(in Matrix3x2 matrix)
127+
{
128+
_m00 = matrix.M11;
129+
_m10 = matrix.M12;
130+
_m01 = matrix.M21;
131+
_m11 = matrix.M22;
132+
_m02 = matrix.M31;
133+
_m12 = matrix.M32;
134+
}
135+
105136
public void SetTransform(AffineTransform t)
106137
{
107138
SetTransform(t._m00, t._m10, t._m01, t._m11, t._m02, t._m12);
@@ -361,6 +392,13 @@ public bool OnlyScale()
361392
return !HasRotate() && !HasTranslate();
362393
}
363394

395+
public static implicit operator AffineTransform(Matrix3x2 matrix) => new AffineTransform(matrix);
396+
397+
public static explicit operator Matrix3x2(AffineTransform matrix)
398+
{
399+
return new Matrix3x2(matrix._m00, matrix._m10, matrix._m01, matrix._m11, matrix._m02, matrix._m12);
400+
}
401+
364402
public bool IsIdentity => _m00 == 1.0f && _m11 == 1.0f && _m10 == 0.0f && _m01 == 0.0f && _m02 == 0.0f && _m12 == 0.0f;
365403
}
366404
}

src/Microsoft.Maui.Graphics/Color.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Globalization;
55
using System.Linq;
6+
using System.Numerics;
67

78
namespace Microsoft.Maui.Graphics
89
{
@@ -41,6 +42,14 @@ public Color(float red, float green, float blue, float alpha)
4142
Alpha = alpha.Clamp(0, 1);
4243
}
4344

45+
public Color(Vector4 color)
46+
{
47+
Red = color.X.Clamp(0, 1);
48+
Green = color.Y.Clamp(0, 1);
49+
Blue = color.Z.Clamp(0, 1);
50+
Alpha = color.W.Clamp(0, 1);
51+
}
52+
4453
public override string ToString()
4554
{
4655
return $"[Color: Red={Red}, Green={Green}, Blue={Blue}, Alpha={Alpha}]";
@@ -910,5 +919,7 @@ static double ParseColorValue(string elem, int maxValue, bool acceptPercent)
910919
static double ParseOpacity(string elem)
911920
=> double.Parse(elem, NumberStyles.Number, CultureInfo.InvariantCulture).Clamp(0, 1);
912921

922+
public static implicit operator Color(Vector4 color) => new Color(color);
923+
913924
}
914925
}

src/Microsoft.Maui.Graphics/Point.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.Globalization;
5+
using System.Numerics;
56

67
namespace Microsoft.Maui.Graphics
78
{
@@ -38,6 +39,12 @@ public Point(SizeF sz) : this()
3839
Y = sz.Height;
3940
}
4041

42+
public Point(Vector2 v)
43+
{
44+
X = v.X;
45+
Y = v.Y;
46+
}
47+
4148
public override bool Equals(object o)
4249
{
4350
if (!(o is Point))
@@ -113,6 +120,8 @@ public void Deconstruct(out double x, out double y)
113120

114121
public static implicit operator PointF(Point p) => new PointF((float)p.X, (float)p.Y);
115122

123+
public static implicit operator Point(Vector2 v) => new Point(v);
124+
116125
public static bool TryParse(string value, out Point point)
117126
{
118127
if (!string.IsNullOrEmpty(value))

src/Microsoft.Maui.Graphics/PointF.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.Globalization;
5+
using System.Numerics;
56

67
namespace Microsoft.Maui.Graphics
78
{
@@ -32,6 +33,12 @@ public PointF(SizeF sz) : this()
3233
Y = sz.Height;
3334
}
3435

36+
public PointF(Vector2 v)
37+
{
38+
X = v.X;
39+
Y = v.Y;
40+
}
41+
3542
public override bool Equals(object o)
3643
{
3744
if (!(o is PointF))
@@ -106,6 +113,10 @@ public void Deconstruct(out float x, out float y)
106113
}
107114
public static implicit operator Point(PointF p) => new Point(p.X, p.Y);
108115

116+
public static implicit operator PointF(Vector2 v) => new PointF(v);
117+
118+
public static explicit operator Vector2(PointF p) => new Vector2(p.X, p.Y);
119+
109120
public static bool TryParse(string value, out PointF pointF)
110121
{
111122
if (!string.IsNullOrEmpty(value))

src/Microsoft.Maui.Graphics/Size.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.Globalization;
5+
using System.Numerics;
56

67
namespace Microsoft.Maui.Graphics
78
{
@@ -32,6 +33,16 @@ public Size(double width, double height)
3233
_height = height;
3334
}
3435

36+
public Size(Vector2 vector)
37+
{
38+
if (float.IsNaN(vector.X))
39+
throw new ArgumentException("NaN is not a valid value for X");
40+
if (float.IsNaN(vector.Y))
41+
throw new ArgumentException("NaN is not a valid value for Y");
42+
_width = vector.X;
43+
_height = vector.Y;
44+
}
45+
3546
public bool IsZero => _width == 0 && _height == 0;
3647

3748
[DefaultValue(0d)]

src/Microsoft.Maui.Graphics/SizeF.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel;
33
using System.Diagnostics;
44
using System.Globalization;
5+
using System.Numerics;
56

67
namespace Microsoft.Maui.Graphics
78
{
@@ -32,6 +33,16 @@ public SizeF(float width, float height)
3233
_height = height;
3334
}
3435

36+
public SizeF(Vector2 vector)
37+
{
38+
if (float.IsNaN(vector.X))
39+
throw new ArgumentException("NaN is not a valid value for X");
40+
if (float.IsNaN(vector.Y))
41+
throw new ArgumentException("NaN is not a valid value for Y");
42+
_width = vector.X;
43+
_height = vector.Y;
44+
}
45+
3546
public bool IsZero => _width == 0 && _height == 0;
3647

3748
[DefaultValue(0d)]
@@ -88,6 +99,11 @@ public static explicit operator PointF(SizeF size)
8899
return new PointF(size.Width, size.Height);
89100
}
90101

102+
public static explicit operator Vector2(SizeF size)
103+
{
104+
return new Vector2(size.Width, size.Height);
105+
}
106+
91107
public bool Equals(SizeF other)
92108
{
93109
return _width.Equals(other._width) && _height.Equals(other._height);

0 commit comments

Comments
 (0)