-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
RadialGradientBrush.cs
108 lines (94 loc) · 4.46 KB
/
RadialGradientBrush.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//// UWP Replacement for WPF RadialGradientBrush: https://msdn.microsoft.com/en-us/library/system.windows.media.radialgradientbrush(v=vs.110).aspx.
using System;
using System.Numerics;
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml.Media;
namespace Microsoft.Toolkit.Uwp.UI.Media
{
/// <summary>
/// RadialGradientBrush - This GradientBrush defines its Gradient as an interpolation
/// within an Ellipse.
/// </summary>
[Obsolete("Please migrate to the RadialGradientBrush control from WinUI, this control will be removed in a future release. https://aka.ms/winui")]
[ContentProperty(Name = nameof(GradientStops))]
public partial class RadialGradientBrush : CanvasBrushBase
{
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var brush = (RadialGradientBrush)d;
// We need to recreate the brush on any property change.
brush.OnDisconnected();
brush.OnConnected();
}
/// <summary>
/// Initializes a new instance of the <see cref="RadialGradientBrush"/> class.
/// </summary>
public RadialGradientBrush()
{
// Rendering surface size, if this is too small the gradient will be pixelated.
// Larger targets aren't effected as one would expect unless the gradient is very complex.
// This seems like a good compromise.
SurfaceWidth = 512;
SurfaceHeight = 512;
GradientStops = new GradientStopCollection();
}
/// <summary>
/// Initializes a new instance of the <see cref="RadialGradientBrush"/> class
/// with two colors specified for GradientStops at
/// offsets 0.0 and 1.0.
/// </summary>
/// <param name="startColor"> The Color at offset 0.0. </param>
/// <param name="endColor"> The Color at offset 1.0. </param>
public RadialGradientBrush(Color startColor, Color endColor)
: this()
{
GradientStops.Add(new GradientStop() { Color = startColor, Offset = 0.0 });
GradientStops.Add(new GradientStop() { Color = endColor, Offset = 1.0 });
}
/// <summary>
/// Initializes a new instance of the <see cref="RadialGradientBrush"/> class with GradientStops set to the passed-in collection.
/// </summary>
/// <param name="gradientStopCollection"> GradientStopCollection to set on this brush. </param>
public RadialGradientBrush(GradientStopCollection gradientStopCollection)
: this()
{
GradientStops = gradientStopCollection;
}
/// <inheritdoc/>
protected override bool OnDraw(CanvasDevice device, CanvasDrawingSession session, Vector2 size)
{
// Create our Brush
if (GradientStops != null && GradientStops.Count > 0)
{
var gradientBrush = new CanvasRadialGradientBrush(
device,
GradientStops.ToWin2DGradientStops(),
SpreadMethod.ToEdgeBehavior(),
(CanvasAlphaMode)(int)AlphaMode,
ColorInterpolationMode.ToCanvasColorSpace(),
CanvasColorSpace.Srgb,
CanvasBufferPrecision.Precision8UIntNormalized)
{
// Calculate Surface coordinates from 0.0-1.0 range given in WPF brush
RadiusX = size.X * (float)RadiusX,
RadiusY = size.Y * (float)RadiusY,
Center = size * Center.ToVector2(),
// Calculate Win2D Offset from origin/center used in WPF brush
OriginOffset = size * (GradientOrigin.ToVector2() - Center.ToVector2()),
};
// Use brush to draw on our canvas
session.FillRectangle(size.ToRect(), gradientBrush);
gradientBrush.Dispose();
return true;
}
return false;
}
}
}