forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTPlatformSpecificFunctions.m
More file actions
83 lines (70 loc) · 2.22 KB
/
Copy pathCPTPlatformSpecificFunctions.m
File metadata and controls
83 lines (70 loc) · 2.22 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
#import "CPTPlatformSpecificFunctions.h"
#pragma mark Graphics Context
// linked list to store saved contexts
static NSMutableArray *pushedContexts = nil;
/** @brief Pushes the current AppKit graphics context onto a stack and replaces it with the given Core Graphics context.
* @param newContext The graphics context.
**/
void CPTPushCGContext(CGContextRef newContext)
{
if ( newContext ) {
if ( !pushedContexts ) {
pushedContexts = [[NSMutableArray alloc] init];
}
[pushedContexts addObject:[NSGraphicsContext currentContext]];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:newContext flipped:NO]];
}
}
/**
* @brief Pops the top context off the stack and restores it to the AppKit graphics context.
**/
void CPTPopCGContext(void)
{
if ( pushedContexts.count > 0 ) {
[NSGraphicsContext setCurrentContext:pushedContexts.lastObject];
[pushedContexts removeLastObject];
}
}
#pragma mark -
#pragma mark Context
/**
* @brief Get the default graphics context
**/
CGContextRef CPTGetCurrentContext(void)
{
return [[NSGraphicsContext currentContext] graphicsPort];
}
#pragma mark -
#pragma mark Colors
/** @brief Creates a @ref CGColorRef from an NSColor.
*
* The caller must release the returned @ref CGColorRef. Pattern colors are not supported.
*
* @param nsColor The NSColor.
* @return The @ref CGColorRef.
**/
CGColorRef CPTCreateCGColorFromNSColor(NSColor *nsColor)
{
NSColor *rgbColor = [nsColor colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
CGFloat r, g, b, a;
[rgbColor getRed:&r green:&g blue:&b alpha:&a];
return CGColorCreateGenericRGB(r, g, b, a);
}
/** @brief Creates a CPTRGBAColor from an NSColor.
*
* Pattern colors are not supported.
*
* @param nsColor The NSColor.
* @return The CPTRGBAColor.
**/
CPTRGBAColor CPTRGBAColorFromNSColor(NSColor *nsColor)
{
CGFloat red, green, blue, alpha;
[[nsColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
CPTRGBAColor rgbColor;
rgbColor.red = red;
rgbColor.green = green;
rgbColor.blue = blue;
rgbColor.alpha = alpha;
return rgbColor;
}