CGShading and printing?
CGShading and printing?
- Subject: CGShading and printing?
- From: Chris Eplett <email@hidden>
- Date: Mon, 25 Apr 2005 09:45:23 -0400
Hi all,
I'm attempting to use a CGShading. It's working great when drawn on
screen, but when I draw to the printer (in my NSView's draw method),
the gradient is being drawn as solid black. Is there anything special I
need to do during printing for the gradient to work?
I've googled this and searched the archives and couldn't find any
applicable info. Is it possible to print CGShadings, and if so, what am
I doing wrong? If not, what techniques have people used to print
gradients?
Any insight is appreciated!
Thanks!
Chris Eplett
Here's my drawing method (called from the drawRect: method of an NSView
subclass. "path" is an NSBezierPath:
if ([[self fill] hasGradient] && (path != nil) && ![path isEmpty])
{
[[NSGraphicsContext currentContext] saveGraphicsState];
[path addClip];
NSRect r = [self rawBounds];
CGPoint gradStartPoint = { NSMinX(r), NSMinY(r) };
CGPoint gradEndPoint = { NSMaxX(r), NSMaxY(r) };
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
NSColor *fillColor = [[self fill] color];
NSColor *gradColor = [[self fill] gradientColor];
float theColors[8] = { [fillColor redComponent], [fillColor
greenComponent],
[fillColor blueComponent], [fillColor alphaComponent],
[gradColor redComponent], [gradColor greenComponent],
[gradColor blueComponent], [gradColor alphaComponent] };
CGFunctionCallbacks callbacks = { 0, &GradientInterpolate, NULL };
size_t components = 1 + CGColorSpaceGetNumberOfComponents (colorspace);
CGFunctionRef gradFunction = CGFunctionCreate((void *)theColors,
1, NULL, components,
NULL, &callbacks);
CGShadingRef shading = CGShadingCreateAxial(colorspace,
gradStartPoint, gradEndPoint,
gradFunction, true, true);
CGContextDrawShading((CGContextRef)[[NSGraphicsContext currentContext]
graphicsPort], shading);
CGShadingRelease(shading);
CGFunctionRelease(gradFunction);
CGColorSpaceRelease(colorspace);
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
here's GradientInterpolate():
void GradientInterpolate(void* info, float const* inData, float*
outData)
{
float *colorInfo = (float*)info;
float a = inData[0];
for(int i = 0; i < 4; i++)
{
outData[i] = (1.0f-a)*colorInfo[i] + a*colorInfo[4+i];
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden