Cappuccino  1.0.0
 All Classes Files Functions Variables Typedefs Macros Groups Pages
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(),
64  context = [[CPGraphicsContext currentContext] graphicsPort];
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 }