diff --git a/src/Controls/src/Core/Brush/Brush.cs b/src/Controls/src/Core/Brush/Brush.cs index 8d8cee876d40..71602682ff83 100644 --- a/src/Controls/src/Core/Brush/Brush.cs +++ b/src/Controls/src/Core/Brush/Brush.cs @@ -62,7 +62,9 @@ public static implicit operator Paint(Brush brush) for (int i = 0; i < gradientStopCollection.Count; i++) { var gs = gradientStopCollection[i]; - gradientStops[i] = new GraphicsGradientStop(gs.Offset, gs.Color); + + if (gs is not null) + gradientStops[i] = new GraphicsGradientStop(gs.Offset, gs.Color); } if (gradientBrush is LinearGradientBrush linearGradientBrush) diff --git a/src/Controls/src/Core/GradientBrush.cs b/src/Controls/src/Core/GradientBrush.cs index e1baf4896c74..a33119ab6b5d 100644 --- a/src/Controls/src/Core/GradientBrush.cs +++ b/src/Controls/src/Core/GradientBrush.cs @@ -62,8 +62,11 @@ void UpdateGradientStops(GradientStopCollection oldCollection, GradientStopColle foreach (var newStop in newCollection) { - newStop.Parent = this; - newStop.PropertyChanged += OnGradientStopPropertyChanged; + if (newStop is not null) + { + newStop.Parent = this; + newStop.PropertyChanged += OnGradientStopPropertyChanged; + } } } diff --git a/src/Controls/tests/Core.UnitTests/LinearGradientBrushTests.cs b/src/Controls/tests/Core.UnitTests/LinearGradientBrushTests.cs index 833852348da0..d60028723411 100644 --- a/src/Controls/tests/Core.UnitTests/LinearGradientBrushTests.cs +++ b/src/Controls/tests/Core.UnitTests/LinearGradientBrushTests.cs @@ -75,7 +75,7 @@ public void TestNullOrEmptyLinearGradientBrush() } [Fact] - public void TestNullOrEmptyLinearGradientPaint() + public void TestNullOrEmptyLinearGradientPaintWithEmptyGradientStop() { LinearGradientBrush linearGradientBrush = new LinearGradientBrush { @@ -93,6 +93,45 @@ public void TestNullOrEmptyLinearGradientPaint() Assert.True(linearGradientPaint.IsNullOrEmpty()); } + [Fact] + public void TestNullOrEmptyLinearGradientPaintWithNullGradientStop() + { + LinearGradientBrush linearGradientBrush = new LinearGradientBrush + { + StartPoint = new Point(0, 0), + EndPoint = new Point(1, 0), + GradientStops = new GradientStopCollection + { + null, + null + } + }; + + Paint linearGradientPaint = linearGradientBrush; + + Assert.True(linearGradientPaint.IsNullOrEmpty()); + } + + [Fact] + public void TestNullGradientStopLinearGradientPaint() + { + LinearGradientBrush linearGradientBrush = new LinearGradientBrush + { + StartPoint = new Point(0, 0), + EndPoint = new Point(1, 0), + GradientStops = new GradientStopCollection + { + new GradientStop { Color = Colors.Red, Offset = 0.1f }, + null, + new GradientStop { Color = Colors.Blue, Offset = 1.0f } + } + }; + + Paint linearGradientPaint = linearGradientBrush; + + Assert.False(linearGradientPaint.IsNullOrEmpty()); + } + [Fact] public void TestLinearGradientBrushPoints() { diff --git a/src/Controls/tests/Core.UnitTests/RadialGradientBrushTests.cs b/src/Controls/tests/Core.UnitTests/RadialGradientBrushTests.cs index 8348afe73592..d26e30391544 100644 --- a/src/Controls/tests/Core.UnitTests/RadialGradientBrushTests.cs +++ b/src/Controls/tests/Core.UnitTests/RadialGradientBrushTests.cs @@ -15,6 +15,44 @@ public void TestConstructor() Assert.Equal(0, gradientStops); } + [Fact] + public void TestNullOrEmptyRadialGradientPaintWithEmptyGradientStop() + { + RadialGradientBrush radialGradientBrush = new RadialGradientBrush + { + Center = new Point(0, 0), + Radius = 10, + GradientStops = new GradientStopCollection + { + new GradientStop(), + new GradientStop() + } + }; + + Paint radialGradientPaint = radialGradientBrush; + + Assert.True(radialGradientPaint.IsNullOrEmpty()); + } + + [Fact] + public void TestNullOrEmptyRadialGradientPaintWithNullGradientStop() + { + RadialGradientBrush radialGradientBrush = new RadialGradientBrush + { + Center = new Point(0, 0), + Radius = 10, + GradientStops = new GradientStopCollection + { + null, + null + } + }; + + Paint radialGradientPaint = radialGradientBrush; + + Assert.True(radialGradientPaint.IsNullOrEmpty()); + } + [Fact] public void TestConstructorUsingGradientStopCollection() { diff --git a/src/Graphics/src/Graphics/GradientPaint.cs b/src/Graphics/src/Graphics/GradientPaint.cs index 1dd5752609cc..e0ba68d1f03e 100644 --- a/src/Graphics/src/Graphics/GradientPaint.cs +++ b/src/Graphics/src/Graphics/GradientPaint.cs @@ -39,7 +39,7 @@ public PaintGradientStop[] GradientStops public Color StartColor { - get => _gradientStops[StartColorIndex].Color; + get => _gradientStops[StartColorIndex]?.Color; set { var startColorIndex = StartColorIndex; @@ -49,7 +49,7 @@ public Color StartColor public Color EndColor { - get => _gradientStops[EndColorIndex].Color; + get => _gradientStops[EndColorIndex]?.Color; set { var endColorIndex = EndColorIndex; @@ -66,7 +66,7 @@ public int StartColorIndex for (var i = 0; i < _gradientStops.Length; i++) { - if (_gradientStops[i].Offset <= offset) + if (_gradientStops[i] is not null && _gradientStops[i].Offset <= offset) { index = i; offset = _gradientStops[i].Offset; @@ -86,7 +86,7 @@ public int EndColorIndex for (var i = 0; i < _gradientStops.Length; i++) { - if (_gradientStops[i].Offset >= offset) + if (_gradientStops[i] is not null && _gradientStops[i].Offset >= offset) { index = i; offset = _gradientStops[i].Offset; @@ -103,7 +103,7 @@ public override bool IsTransparent { foreach (var stop in GradientStops) { - if (stop.Color != null && stop.Color.Alpha < 1) + if (stop is not null && stop.Color is not null && stop.Color.Alpha < 1) { return true; }