This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathBrushExtensions.shared.cs
97 lines (78 loc) · 2.22 KB
/
BrushExtensions.shared.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
using System.Collections.Generic;
using System.Linq;
using CoreGraphics;
using Foundation;
#if __MOBILE__
namespace Xamarin.Forms.Platform.iOS
#else
namespace Xamarin.Forms.Platform.MacOS
#endif
{
public partial class BrushExtensions
{
static CGPoint GetRadialGradientBrushEndPoint(Point startPoint, double radius)
{
double x = startPoint.X == 1 ? (startPoint.X - radius) : (startPoint.X + radius);
if (x < 0)
x = 0;
if (x > 1)
x = 1;
double y = startPoint.Y == 1 ? (startPoint.Y - radius) : (startPoint.Y + radius);
if (y < 0)
y = 0;
if (y > 1)
y = 1;
return new CGPoint(x, y);
}
static NSNumber[] GetCAGradientLayerLocations(List<GradientStop> gradientStops)
{
if (gradientStops == null || gradientStops.Count == 0)
return new NSNumber[0];
if (gradientStops.Count > 1 && gradientStops.Any(gt => gt.Offset != 0))
return gradientStops.Select(x => new NSNumber(x.Offset)).ToArray();
else
{
int itemCount = gradientStops.Count;
int index = 0;
float step = 1.0f / itemCount;
NSNumber[] locations = new NSNumber[itemCount];
foreach (var gradientStop in gradientStops)
{
float location = step * index;
bool setLocation = !gradientStops.Any(gt => gt.Offset > location);
if (gradientStop.Offset == 0 && setLocation)
locations[index] = new NSNumber(location);
else
locations[index] = new NSNumber(gradientStop.Offset);
index++;
}
return locations;
}
}
static CGColor[] GetCAGradientLayerColors(List<GradientStop> gradientStops)
{
if (gradientStops == null || gradientStops.Count == 0)
return new CGColor[0];
CGColor[] colors = new CGColor[gradientStops.Count];
int index = 0;
foreach (var gradientStop in gradientStops)
{
if (gradientStop.Color == Color.Transparent)
{
var color = gradientStops[index == 0 ? index + 1 : index - 1].Color;
CGColor nativeColor;
#if __MOBILE__
nativeColor = color.ToUIColor().ColorWithAlpha(0.0f).CGColor;
#else
nativeColor = color.ToNSColor().ColorWithAlphaComponent(0.0f).CGColor;
#endif
colors[index] = nativeColor;
}
else
colors[index] = gradientStop.Color.ToCGColor();
index++;
}
return colors;
}
}
}