-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathNSGradient.cs
112 lines (98 loc) · 5.66 KB
/
NSGradient.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
109
110
111
112
//
// NSGradient: Extensions to the API for NSGradient
//
// Author:
// Regan Sarwas (find me on gmail as rsarwas)
//
#if !__MACCATALYST__
using System;
using Foundation;
using ObjCRuntime;
using System.Runtime.InteropServices;
#nullable enable
namespace AppKit {
public partial class NSGradient : NSObject {
// The signature of this ObjC method is
// - (id)initWithColorsAndLocations:(NSColor *)firstColor, ... NS_REQUIRES_NIL_TERMINATION;
// where colors and locations (as CGFloats between 0.0 and 1.0) alternate until nil
// ObjC example:
// NSGradient *gradient = [[NSGradient alloc] initWithColorsAndLocations: [NSColor blackColor], 0.0,
// [NSColor blueColor], 0.33,
// [NSColor redColor], 1.0, nil];
// which is a very un-C# thing to do. The best correlation would be
// NSGradient (NSColor[] colors, float[] locations)
// C# example:
// NSGradient gradient = new NSGradient(new[] {NSColor.Black, NSColor.Blue, NSColor.Red},
// new[] { 0.0f, 0.33f, 1.0f});
// Per NSGradient.h, this initializer calls the designated initializer (below) with a
// color space of NSColorSpace.GenericRGBColorSpace, so we will do the same.
/// <summary>Create a new <see cref="NSGradient" /> instance with the specified colors and color locations.</summary>
/// <param name="colors">The colors for the new gradient.</param>
/// <param name="locations">The locations for each color in the new gradient. Each location must be a value between 0.0 and 1.0.</param>
/// <remarks>This gradient is created in the <a cref="NSColorSpace.GenericRGBColorSpace" /> generic RGB color space.</remarks>
public NSGradient (NSColor [] colors, float [] locations) :
this (colors, locations, NSColorSpace.GenericRGBColorSpace)
{
}
/// <summary>Create a new <see cref="NSGradient" /> instance with the specified colors and color locations.</summary>
/// <param name="colors">The colors for the new gradient.</param>
/// <param name="locations">The locations for each color in the new gradient. Each location must be a value between 0.0 and 1.0.</param>
/// <remarks>This gradient is created in the <a cref="NSColorSpace.GenericRGBColorSpace" /> generic RGB color space.</remarks>
public NSGradient (NSColor [] colors, double [] locations) :
this (colors, locations, NSColorSpace.GenericRGBColorSpace)
{
}
/// <summary>Create a new <see cref="NSGradient" /> instance with the specified colors and color locations.</summary>
/// <param name="colors">The colors for the new gradient.</param>
/// <param name="locations">The locations for each color in the new gradient. Each location must be a value between 0.0 and 1.0.</param>
/// <remarks>This gradient is created in the <a cref="NSColorSpace.GenericRGBColorSpace" /> generic RGB color space.</remarks>
public NSGradient (NSColor [] colors, nfloat [] locations)
: this (colors, locations, NSColorSpace.GenericRGBColorSpace)
{
}
/// <summary>Create a new <see cref="NSGradient" /> instance with the specified colors and color locations.</summary>
/// <param name="colors">The colors for the new gradient.</param>
/// <param name="locations">The locations for each color in the new gradient. Each location must be a value between 0.0 and 1.0.</param>
/// <param name="colorSpace">The color space for the new gradient.</param>
public NSGradient (NSColor [] colors, float [] locations, NSColorSpace colorSpace)
: this (colors, Array.ConvertAll<float, nfloat> (locations, new Converter<float, nfloat> (a => (nfloat) a)), colorSpace)
{
}
/// <summary>Create a new <see cref="NSGradient" /> instance with the specified colors and color locations.</summary>
/// <param name="colors">The colors for the new gradient.</param>
/// <param name="locations">The locations for each color in the new gradient. Each location must be a value between 0.0 and 1.0.</param>
/// <param name="colorSpace">The color space for the new gradient.</param>
public NSGradient (NSColor [] colors, double [] locations, NSColorSpace colorSpace) : base (NSObjectFlag.Empty)
{
unsafe {
fixed (void* locationPtr = locations) {
Initialize (colors, locations?.Length, (IntPtr) locationPtr, colorSpace);
}
}
}
/// <summary>Create a new <see cref="NSGradient" /> instance with the specified colors and color locations.</summary>
/// <param name="colors">The colors for the new gradient.</param>
/// <param name="locations">The locations for each color in the new gradient. Each location must be a value between 0.0 and 1.0.</param>
/// <param name="colorSpace">The color space for the new gradient.</param>
public NSGradient (NSColor [] colors, nfloat [] locations, NSColorSpace colorSpace)
: base (NSObjectFlag.Empty)
{
unsafe {
fixed (void* locationPtr = locations) {
Initialize (colors, locations?.Length, (IntPtr) locationPtr, colorSpace);
}
}
}
void Initialize (NSColor [] colors, int? locationCount, IntPtr locations, NSColorSpace colorSpace)
{
if (colors is null)
ThrowHelper.ThrowArgumentNullException (nameof (colors));
if (locationCount is null)
ThrowHelper.ThrowArgumentNullException (nameof (locations));
if (colors.Length != locationCount)
ThrowHelper.ThrowArgumentOutOfRangeException (nameof (locations), string.Format ("The number of colors ({0}) and the number of locations ({1}) must be equal.", colors.Length, locationCount));
InitializeHandle (_InitWithColorsAtLocationsAndColorSpace (colors, locations, colorSpace), "initWithColors:atLocations:colorSpace:");
}
}
}
#endif // !__MACCATALYST__