API  0.9.10
CPGraphics.j
Go to the documentation of this file.
1 /*
2  * CPGraphics.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2010, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 CPCalibratedWhiteColorSpace = @"CalibratedWhiteColorSpace";
25 CPCalibratedBlackColorSpace = @"CalibratedBlackColorSpace";
26 CPCalibratedRGBColorSpace = @"CalibratedRGBColorSpace";
27 CPDeviceWhiteColorSpace = @"DeviceWhiteColorSpace";
28 CPDeviceBlackColorSpace = @"DeviceBlackColorSpace";
29 CPDeviceRGBColorSpace = @"DeviceRGBColorSpace";
30 CPDeviceCMYKColorSpace = @"DeviceCMYKColorSpace";
31 CPNamedColorSpace = @"NamedColorSpace";
32 CPPatternColorSpace = @"PatternColorSpace";
33 CPCustomColorSpace = @"CustomColorSpace";
34 
36  /* CGRect */ boundsRect,
37  /* CGRect */ clipRect,
38  /* CPRectEdge[] */ sides,
39  /* float[] */ grays)
40 {
41  if (sides.length != grays.length)
42  [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and grays (length: " + grays.length + ") must have the same length."];
43 
44  var colors = [grays arrayByApplyingBlock:function(gray)
45  {
46  return [CPColor colorWithCalibratedWhite:gray alpha:1.0];
47  }];
48 
49  return CPDrawColorTiledRects(boundsRect, clipRect, sides, colors);
50 }
51 
53  /* CGRect */ boundsRect,
54  /* CGRect */ clipRect,
55  /* CPRectEdge[] */ sides,
56  /* CPColor[] */ colors)
57 {
58  if (sides.length != colors.length)
59  [CPException raise:CPInvalidArgumentException reason:@"sides (length: " + sides.length + ") and colors (length: " + colors.length + ") must have the same length."];
60 
61  var resultRect = CGRectMakeCopy(boundsRect),
62  slice = CGRectMakeZero(),
63  remainder = CGRectMakeZero(),
65 
66  CGContextSaveGState(context);
67  CGContextSetLineWidth(context, 1.0);
68 
69  for (var sideIndex = 0; sideIndex < sides.length; ++sideIndex)
70  {
71  var side = sides[sideIndex];
72 
73  CGRectDivide(resultRect, slice, remainder, 1.0, side);
74  resultRect = remainder;
75  slice = CGRectIntersection(slice, clipRect);
76 
77  // Cocoa docs say that only slices that are within the clipRect are actually drawn
78  if (CGRectIsEmpty(slice))
79  continue;
80 
81  var minX,
82  maxX,
83  minY,
84  maxY;
85 
86  if (side == CPMinXEdge || side == CPMaxXEdge)
87  {
88  // Make sure we have at least 1 pixel to draw a line
89  if (CGRectGetWidth(slice) < 1.0)
90  continue;
91 
92  minX = CGRectGetMinX(slice) + 0.5;
93  maxX = minX;
94  minY = CGRectGetMinY(slice);
95  maxY = CGRectGetMaxY(slice);
96  }
97  else // CPMinYEdge || CPMaxYEdge
98  {
99  // Make sure we have at least 1 pixel to draw a line
100  if (CGRectGetHeight(slice) < 1.0)
101  continue;
102 
103  minX = CGRectGetMinX(slice);
104  maxX = CGRectGetMaxX(slice);
105  minY = CGRectGetMinY(slice) + 0.5;
106  maxY = minY;
107  }
108 
109  CGContextBeginPath(context);
110  CGContextMoveToPoint(context, minX, minY);
111  CGContextAddLineToPoint(context, maxX, maxY);
112  CGContextSetStrokeColor(context, colors[sideIndex]);
113  CGContextStrokePath(context);
114  }
115 
116  CGContextRestoreGState(context);
117 
118  return resultRect;
119 }
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
CPCustomColorSpace
Definition: CPGraphics.j:33
CPDeviceRGBColorSpace
Definition: CPGraphics.j:29
CPCalibratedRGBColorSpace
Definition: CPGraphics.j:26
CPRectEdge CPMinXEdge
Definition: CPGeometry.j:25
CPGraphicsContext currentContext()
function CGContextSetStrokeColor(aContext, aColor)
Definition: CGContext.j:675
CPNamedColorSpace
Definition: CPGraphics.j:31
function CGContextRestoreGState(aContext)
Definition: CGContext.j:156
function CPDrawColorTiledRects(boundsRect, clipRect, sides, colors)
Definition: CPGraphics.j:52
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
function CGContextAddLineToPoint(aContext, x, y)
Definition: CGContext.j:247
function CGContextStrokePath(aContext)
Definition: CGContext.j:619
function CGContextSetLineWidth(aContext, aLineWidth)
Definition: CGContext.j:177
CPColor colorWithCalibratedWhite:alpha:(float white, [alpha] float alpha)
Definition: CPColor.j:172
CPCalibratedBlackColorSpace
Definition: CPGraphics.j:25
CPPatternColorSpace
Definition: CPGraphics.j:32
function CPDrawTiledRects(boundsRect, clipRect, sides, grays)
Definition: CPGraphics.j:35
function CGContextBeginPath(aContext)
Definition: CGContext.j:311
CPDeviceWhiteColorSpace
Definition: CPGraphics.j:27
CPMaxXEdge
Definition: CPGeometry.j:27
function CGContextSaveGState(aContext)
Definition: CGContext.j:146
CPCalibratedWhiteColorSpace
Definition: CPGraphics.j:24
CPDeviceBlackColorSpace
Definition: CPGraphics.j:28
CPDeviceCMYKColorSpace
Definition: CPGraphics.j:30
function CGContextMoveToPoint(aContext, x, y)
Definition: CGContext.j:344