Starter CPGradient with drawInRect:angle:.

This commit is contained in:
Alexander Ljungberg
2012-03-10 11:55:13 +00:00
parent c4a16d10bc
commit 3d9ae42dbf
2 changed files with 62 additions and 0 deletions
+1
View File
@@ -56,6 +56,7 @@
@import "CPFont.j"
@import "CPFontManager.j"
@import "CPGeometry.j"
@import "CPGradient.j"
@import "CPGraphics.j"
@import "CPImage.j"
@import "CPImageView.j"
+61
View File
@@ -0,0 +1,61 @@
/*
* CPGradient.j
* AppKit
*
* Created by Alexander Ljungberg.
* Copyright 2012, SlevenBits Ltd.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*!
A class for drawing linear and radial gradients with a convenient API.
*/
@implementation CPGradient : CPObject
{
CGGradient _gradient;
}
- (id)initWithColors:(CPArray)someColors atLocations:(CPArray)someLocations colorSpace:(CPColorSpace)aColorSpace
{
if (self = [super init])
{
var cgColors = [],
count = [someColors count],
colorspace = CGColorSpaceCreateDeviceRGB;
while (count--)
cgColors.push(CGColorCreate(colorspace, [someColors[count] components]));
_gradient = CGGradientCreateWithColors(aColorSpace, cgColors, someLocations);
}
return self;
}
- (void)drawInRect:(CGRect)rect angle:(float)angle
{
if (angle !== 0)
[CPException raise:CPInvalidArgumentException reason:@"angle != 0 not yet implemented"];
var ctx = [[CPGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(ctx);
CGContextAddRect(ctx, rect);
CGContextDrawLinearGradient(ctx, _gradient, rect.origin, CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect)), 0);
CGContextRestoreGState(ctx);
//[self drawFromPoint:rect.origin toPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect)) options:0];
}
@end