-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathRadialGradientPaint.cs
More file actions
183 lines (154 loc) · 7.05 KB
/
RadialGradientPaint.cs
File metadata and controls
183 lines (154 loc) · 7.05 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// The MIT License(MIT)
//
// Copyright(c) 2021 Alberto Rodriguez Orozco & LiveCharts Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using LiveChartsCore.Drawing;
using LiveChartsCore.Painting;
using LiveChartsCore.SkiaSharpView.Drawing;
using SkiaSharp;
namespace LiveChartsCore.SkiaSharpView.Painting;
/// <summary>
/// Defines a set of geometries that will be painted using a radial gradient shader.
/// </summary>
/// <seealso cref="SkiaPaint" />
public class RadialGradientPaint : SkiaPaint
{
private SKShader? _shader;
private SKRect _activeClip = new();
private readonly SKColor[] _gradientStops;
private SKPoint _center;
private float _radius;
private readonly float[]? _colorPos;
private readonly SKShaderTileMode _tileMode;
/// <summary>
/// Initializes a new instance of the <see cref="RadialGradientPaint"/> class.
/// </summary>
/// <param name="gradientStops">The gradient stops.</param>
/// <param name="center">
/// The center point of the gradient, both X and Y in the range of 0 to 1, where 0 is the start of the axis and 1 the end,
/// default is (0.5, 0.5).
/// </param>
/// <param name="radius">
/// The radius, in the range of 0 to 1, where 1 is the minimum of both Width and Height of the chart, default is 0.5.
/// </param>
/// <param name="colorPos">
/// An array of integers in the range of 0 to 1.
/// These integers indicate the relative positions of the colors, You can set that argument to null to equally
/// space the colors, default is null.
/// </param>
/// <param name="tileMode">
/// The shader tile mode, default is <see cref="SKShaderTileMode.Clamp"/>.
/// </param>
public RadialGradientPaint(
SKColor[] gradientStops,
SKPoint? center = null,
float radius = 0.5f,
float[]? colorPos = null,
SKShaderTileMode tileMode = SKShaderTileMode.Clamp)
{
_gradientStops = gradientStops;
center ??= new SKPoint(0.5f, 0.5f);
_center = center.Value;
_radius = radius;
_colorPos = colorPos;
_tileMode = tileMode;
}
/// <summary>
/// Initializes a new instance of the <see cref="RadialGradientPaint"/> class.
/// </summary>
/// <param name="centerColor">Color of the center.</param>
/// <param name="outerColor">Color of the outer.</param>
public RadialGradientPaint(SKColor centerColor, SKColor outerColor)
: this([centerColor, outerColor]) { }
/// <inheritdoc cref="Paint.CloneTask" />
public override Paint CloneTask()
{
var clone = new RadialGradientPaint(_gradientStops, _center, _radius, _colorPos, _tileMode);
Map(this, clone);
return clone;
}
internal override void OnPaintStarted(DrawingContext drawingContext, IDrawnElement? drawnElement)
{
var skiaContext = (SkiaSharpDrawingContext)drawingContext;
_skiaPaint = UpdateSkiaPaint(skiaContext, drawnElement);
var bounds = skiaContext.Canvas.LocalClipBounds;
_activeClip = new SKRect(bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
_skiaPaint.Shader = GetShader();
}
internal override void ApplyOpacityMask(DrawingContext context, float opacity, IDrawnElement? drawnElement)
{
if (_skiaPaint is null || opacity > 0.99) return;
_skiaPaint.ColorFilter =
SKColorFilter.CreateBlendMode(
new SKColor(255, 255, 255, (byte)(255 * opacity)),
SKBlendMode.DstIn);
}
internal override void RestoreOpacityMask(DrawingContext context, float opacity, IDrawnElement? drawnElement)
{
if (_skiaPaint is null) return;
_skiaPaint.ColorFilter = null;
}
internal override Paint Transitionate(float progress, Paint target)
{
if (target is not RadialGradientPaint toPaint) return target;
if (toPaint._gradientStops.Length != _gradientStops.Length)
throw new ArgumentException("The gradient stops must be the same length.");
for (var i = 0; i < _gradientStops.Length; i++)
_gradientStops[i] = new SKColor(
(byte)(_gradientStops[i].Red + progress * (toPaint._gradientStops[i].Red - _gradientStops[i].Red)),
(byte)(_gradientStops[i].Green + progress * (toPaint._gradientStops[i].Green - _gradientStops[i].Green)),
(byte)(_gradientStops[i].Blue + progress * (toPaint._gradientStops[i].Blue - _gradientStops[i].Blue)),
(byte)(_gradientStops[i].Alpha + progress * (toPaint._gradientStops[i].Alpha - _gradientStops[i].Alpha)));
_center = new SKPoint(
_center.X + progress * (toPaint._center.X - _center.X),
_center.Y + progress * (toPaint._center.Y - _center.Y));
_radius = _radius + progress * (toPaint._radius - _radius);
if (_colorPos is not null && toPaint._colorPos is not null)
{
if (_colorPos is null || _colorPos.Length != _colorPos.Length)
throw new ArgumentException("The color positions must be the same length.");
for (var i = 0; i < _colorPos.Length; i++)
_colorPos[i] = _colorPos[i] + progress * (_colorPos[i] - _colorPos[i]);
}
_shader?.Dispose();
_shader = null;
_skiaPaint?.Shader = GetShader();
return this;
}
internal override void DisposeTask()
{
base.DisposeTask();
_shader?.Dispose();
_shader = null;
}
private SKShader GetShader()
{
if (_shader is not null)
return _shader;
var center = new SKPoint(_activeClip.Location.X + _center.X * _activeClip.Width, _activeClip.Location.Y + _center.Y * _activeClip.Height);
var r = _activeClip.Location.X + _activeClip.Width > _activeClip.Location.Y + _activeClip.Height
? _activeClip.Location.Y + _activeClip.Height
: _activeClip.Location.X + _activeClip.Width;
r *= _radius;
return
_shader = SKShader.CreateRadialGradient(center, r, _gradientStops, _colorPos, _tileMode);
}
}