Re: Drawing gradients in views
Re: Drawing gradients in views
- Subject: Re: Drawing gradients in views
- From: Joseph Jones <email@hidden>
- Date: Mon, 27 Jan 2003 19:42:48 -0800
I am drawing gradients by hand. This seems like something Quartz is
well suited to provide natively, yet I couldn't find anything to
support it.
As far as round rect goes, Scott's book provides a nice category to add
to NSBezierPath that adds round rect funcitonality. It works well and I
use it quite a bit in my app.
Here is the code I have for this:
@implementation NSBezierPath(RoundRect)
- (void) appendBezierPathWithRoundedRectangle:(NSRect)aRect
withRadius:(float) radius
{
NSPoint topMid = NSMakePoint(NSMidX(aRect), NSMaxY(aRect));
NSPoint topLeft = NSMakePoint(NSMinX(aRect), NSMaxY(aRect));
NSPoint topRight = NSMakePoint(NSMaxX(aRect), NSMaxY(aRect));
NSPoint bottomRight = NSMakePoint(NSMaxX(aRect), NSMinY(aRect));
[self moveToPoint:topMid];
[self appendBezierPathWithArcFromPoint:topLeft toPoint:aRect.origin
radius:radius];
[self appendBezierPathWithArcFromPoint:aRect.origin
toPoint:bottomRight radius:radius];
[self appendBezierPathWithArcFromPoint:bottomRight toPoint:topRight
radius:radius];
[self appendBezierPathWithArcFromPoint:topRight toPoint:topLeft
radius:radius];
[self closePath];
}
@end
and here is my implementation of a gradient draw rect function (which I
guess could be added to the NSBezierPath category as well??)
- (void)_drawRect:(NSRect)rect withGradientFrom:(NSColor*)colorStart
to:(NSColor*)colorEnd
{
NSRect t1, t2, t3;
float r, g, b,a;
float rdiff, gdiff, bdiff, adiff;
int i;
int index = rect.size.height;
t1 = rect;
r = [colorStart redComponent];
g = [colorStart greenComponent];
b = [colorStart blueComponent];
a = [colorStart alphaComponent];
rdiff = ([colorEnd redComponent] - r)/index;
gdiff = ([colorEnd greenComponent] - g)/index;
bdiff = ([colorEnd blueComponent] - b)/index;
adiff = ([colorEnd alphaComponent] - a)/index;
for ( i = 0; i < index; i++ )
{
NSDivideRect ( t1, &t2, &t3, 1.0, NSMinYEdge);
[[NSColor colorWithDeviceRed:r green:g blue:b alpha:a] set];
NSRectFillUsingOperation(t2, NSCompositeSourceOver);
r += rdiff;
g += gdiff;
b += bdiff;
a += adiff;
t1 = t3;
}
}
joe
On Monday, January 27, 2003, at 07:26 PM, Matt Gemmell wrote:
>
On Tuesday, January 28, 2003, at 02:52 am, Ian G. Gillespie wrote:
>
>
> Does anyone know if it possible to fill an NSRect with a gradient. I
>
> envision creating a rectangle that looks like the ones in iCal, they
>
> have that smooth color gradient. I have figured out how to create
>
> shadows for my rectangles, but don't know how to do a gradient.
>
>
If you happen to be using an NSButton, or subclass of it, it seems to
>
have a very basic -setGradientType: method, but only for black-white
>
gradients. I think you'd need to draw the gradients manually > otherwise.
>
>
> Also, I have tried to figure out how to draw a rect with rounded
>
> corners but haven't had any luck. I found this
>
> http://lists.apple.com/archives/cocoa-dev/2001/Aug/28/
>
> drawingroundedrect.002.txt in the archives, but it didn't work for
>
> me. My rectangles weren't rectangles at all!
>
>
I'm not experienced in this, but I imagine you'd have to play with
>
NSBezierPath's lineJoinStyle and lineCapStyle, at least for very
>
shallow rounded corners.
>
>
For more pronounced roundness, you'd probably need to use
>
-curveToPoint:... and -relativeCurveToPoint:... and do the rounded
>
bits yourself. See the docs for NSBezierPath for more on this. I'm
>
also fairly sure that Scott's book (Cocoa Programming,
>
www.cocoaprogramming.net) has a specific example of using
>
curveToPoint:... with a certain radius etc.
>
>
Best,
>
-Matt
>
>
--
>
Matt Gemmell
>
Scotland Software
>
http://www.scotlandsoftware.com/
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.