Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 173 lines (149 sloc) 6.227 kB
8ec8e9e @Machx moar MIT License headers on source code files
authored
1 /*
060e1b7 @Machx removing CW_CURRENT_CONTEXT in macros and adding CWCurrentCGContext()…
authored
2 // CWGraphicsFoundation.m
3 // Zangetsu
4 //
5 // Created by Colin Wheeler on 12/24/10.
6 // Copyright 2010. All rights reserved.
7 //
8
8ec8e9e @Machx moar MIT License headers on source code files
authored
9 Copyright (c) 2011 Colin Wheeler
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 THE SOFTWARE.
28 */
29
060e1b7 @Machx removing CW_CURRENT_CONTEXT in macros and adding CWCurrentCGContext()…
authored
30 #import "CWGraphicsFoundation.h"
31
8db2f25 @Machx minor documentation updates
authored
32 /**
33 Easy way to return the CGContextRef inside a NSView
34 */
dd2e158 @Machx minor formatting adjustments
authored
35 inline CGContextRef CWCurrentCGContext()
36 {
89feeb2 @Machx more iOS Compatibility
authored
37 #if !(TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
060e1b7 @Machx removing CW_CURRENT_CONTEXT in macros and adding CWCurrentCGContext()…
authored
38 return (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
89feeb2 @Machx more iOS Compatibility
authored
39 #else
40 return UIGraphicsGetCurrentContext();
41 #endif
060e1b7 @Machx removing CW_CURRENT_CONTEXT in macros and adding CWCurrentCGContext()…
authored
42 }
ccb06d1 @Machx adding an old code snippet to the framework CWAddRoundedRectToPath()
authored
43
9beb73a @Machx minor CWCenteredRect() refactoring in multiply by 0.5 instead of divi…
authored
44 CGRect CWCenteredRect(CGRect smallRect, CGRect largeRect)
45 {
46 CGRect centeredRect;
47 centeredRect.size = smallRect.size;
48 centeredRect.origin.x = (largeRect.size.width - smallRect.size.width) * 0.5;
49 centeredRect.origin.y = (largeRect.size.height - smallRect.size.height) * 0.5;
50 return centeredRect;
51 }
52
899ae6e @Machx documenting CWExecuteAndRestoreCGContext and CWAddRoundedRectToPath
authored
53 /**
54 Adds a rounded rect path to a CGContextRef
55 */
ccb06d1 @Machx adding an old code snippet to the framework CWAddRoundedRectToPath()
authored
56 void CWAddRoundedRectToPath(CGContextRef context,
57 CGRect rect,
58 float ovalWidth,
dd2e158 @Machx minor formatting adjustments
authored
59 float ovalHeight)
60 {
ccb06d1 @Machx adding an old code snippet to the framework CWAddRoundedRectToPath()
authored
61 if (ovalWidth == 0 || ovalHeight == 0) {
62 CGContextAddRect(context, rect);
63 return;
64 }
65
9219082 @Machx slight name change CWSaveAndRestoreContextState() -> CWSaveAndRestore…
authored
66 CWSaveAndRestoreCGContextState(context, ^{
07b306f @Machx rewriting part of CWAddRoundedRectToPath to use CWSaveAndRestoreConte…
authored
67 float fw, fh;
68
ced32f0 @Machx minor reformatting to make the code a bit more readable
authored
69 CGContextTranslateCTM (context, CGRectGetMinX(rect),CGRectGetMinY(rect));
70 CGContextScaleCTM (context, ovalWidth, ovalHeight);
71
72 fw = CGRectGetWidth (rect) / ovalWidth;
73 fh = CGRectGetHeight (rect) / ovalHeight;
74
75 CGContextMoveToPoint(context, fw, fh/2);
76 CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
77 CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
78 CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
79 CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
80 CGContextClosePath(context);
07b306f @Machx rewriting part of CWAddRoundedRectToPath to use CWSaveAndRestoreConte…
authored
81 });
ccb06d1 @Machx adding an old code snippet to the framework CWAddRoundedRectToPath()
authored
82 }
a07e458 @Machx new api CWSaveAndRestoreContextState(ctx,block);
authored
83
89b571f @Machx documenting CWSaveAndRestoreCGContextState() and CWCreateCGColor()
authored
84 /**
85 Saves the CGContext state, executes the block and then restores the context State.
86
87 @param ctx a CGContextRef you wish to operate on
88 @param block a block containing code you wish to execute between saving & restoring the CGContextRef state
89 */
dd2e158 @Machx minor formatting adjustments
authored
90 void CWSaveAndRestoreCGContextState(CGContextRef ctx, void(^block)(void))
91 {
a07e458 @Machx new api CWSaveAndRestoreContextState(ctx,block);
authored
92 CGContextSaveGState(ctx);
93 block();
94 CGContextRestoreGState(ctx);
95 }
03d5a12 @Machx CWCreateCGColor(r,g,b,a,space); API
authored
96
10e72cd @Machx documenting CWContextDrawLinearGradientBetweenPoints()
authored
97 /**
98 Draws a Linear gradient between 2 points using Core Graphics
99
100 @param context the CGContextRef the drawing is to occurr on
101 @param point1 a CGPoint, the starting point for where the gradient should be drawn
102 @param point2 a CGPoint, the ending point for where the gradient should be drawn
103 @param colora the starting Color for the gradient
104 @param colorb the ending Color for the gradient
105 */
0b8552a @Machx changing the argument order & argument names a bit on CWContextDrawLi…
authored
106 void CWContextDrawLinearGradientBetweenPoints(CGContextRef context,
107 CGPoint point1, CGFloat color1[4],
108 CGPoint point2, CGFloat color2[4])
a4e0297 @Machx new api CWContextDrawLinearGradientBetweenPoints()
authored
109 {
110 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
0b8552a @Machx changing the argument order & argument names a bit on CWContextDrawLi…
authored
111 CGFloat components[] = { color1[0], color1[1], color1[2], color1[3], color2[0], color2[1], color2[2], color2[3] };
a4e0297 @Machx new api CWContextDrawLinearGradientBetweenPoints()
authored
112 CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components, NULL, 2);
113 CGContextDrawLinearGradient(context, gradient, point1, point2, 0);
114 CGGradientRelease(gradient);
115 CGColorSpaceRelease(space);
116 }
117
686dd62 @Machx documenting CWCreateCGColor()
authored
118 /**
119 Easy way to Create a CGColorRef using the Device RGB Colorspace.
120
121 @param r a CGFloat representing the red component of the CGColorRef
122 @param g a CGFloat representing the blue component of the CGColorRef
123 @param b a CGFloat representing the green component of the CGColorRef
124 @param a a CGFloat representing the alpha component of the CGColorRef
125 @return a CGColorRef object created with the components specified in the parameters, you must release this object when done with it
126 */
e86cece @Machx CWCreateCGColor no longer takes a CGColorSpaceRef arg, new api CWCrea…
authored
127 CGColorRef CWCreateCGColor(CGFloat r, CGFloat g, CGFloat b, CGFloat a)
128 {
129 CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
130 CGFloat components[4];
131 components[0] = r; components[1] = g; components[2] = b; components[3] = a;
132 CGColorRef colorRef = CGColorCreate(space, components);
133 CGColorSpaceRelease(space);
134 return colorRef;
135 }
136
89b571f @Machx documenting CWSaveAndRestoreCGContextState() and CWCreateCGColor()
authored
137 /**
138 Easy way to Create a CGColorRef
139
140 @param r a CGFloat representing the red component of the CGColorRef
141 @param g a CGFloat representing the blue component of the CGColorRef
142 @param b a CGFloat representing the green component of the CGColorRef
143 @param a a CGFloat representing the alpha component of the CGColorRef
29f5b75 @Machx fixing a minor spelling error
authored
144 @param cspace the CGColorSpaceRef you want the CGColorRef to be created with. This cannot be NULL, if it is NULL this method returns NULL.
89b571f @Machx documenting CWSaveAndRestoreCGContextState() and CWCreateCGColor()
authored
145 @return a CGColorRef object created with the components specified in the parameters, you must release this object when done with it
146 */
e86cece @Machx CWCreateCGColor no longer takes a CGColorSpaceRef arg, new api CWCrea…
authored
147 CGColorRef CWCreateCGColorWithSpace(CGFloat r, CGFloat g, CGFloat b, CGFloat a, CGColorSpaceRef cspace)
03d5a12 @Machx CWCreateCGColor(r,g,b,a,space); API
authored
148 {
0c642e6 @Machx refactoring CWCreateCGColor() a bit & making it simpler
authored
149 if(cspace == NULL) { return NULL; }
03d5a12 @Machx CWCreateCGColor(r,g,b,a,space); API
authored
150 CGFloat components[4];
151 components[0] = r; components[1] = g; components[2] = b; components[3] = a;
0c642e6 @Machx refactoring CWCreateCGColor() a bit & making it simpler
authored
152 CGColorRef colorRef = CGColorCreate(cspace, components);
03d5a12 @Machx CWCreateCGColor(r,g,b,a,space); API
authored
153 return colorRef;
154 }
86c6028 @Machx convenience functions for some colors
authored
155
ae176ad @Machx using a macro to make it easy to implement predefined color methods
authored
156 #define CWCGColor(_name_,red,blue,green) \
157 CGColorRef CWCGColor##_name_(CGFloat alpha) \
158 { \
159 CGFloat r = CWCGColorFloat(red); \
160 CGFloat g = CWCGColorFloat(green); \
161 CGFloat b = CWCGColorFloat(blue); \
162 CGFloat a = alpha; \
163 CGColorRef color = CWCreateCGColor(r,g,b,a); \
164 return color; \
86c6028 @Machx convenience functions for some colors
authored
165 }
166
ae176ad @Machx using a macro to make it easy to implement predefined color methods
authored
167 CWCGColor(LightGray, 211, 211, 211);
168 CWCGColor(Gray,190,190,190);
169 CWCGColor(DarkGray,105,105,105);
170 CWCGColor(Blue,0,0,255);
fee9d03 @Machx new api CWCGColorDarkGray()
authored
171
ae176ad @Machx using a macro to make it easy to implement predefined color methods
authored
172 #undef CWCGColor
Something went wrong with that request. Please try again.