Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CILinearGradient drawing backwards in 10.5



As of Leopard, when I attempt to draw a linear gradient (CILinearGradient) via any kind of context--an NSImage, an NSView drawing itself (flipped or not), etc--the gradient start and end colours are backwards. That is, if I start the gradient at black and end it at white, it will appear on-screen as though I started at white and ended at black. In Tiger, I did not have this problem.

I draw the gradient inside of an NSBezierPath category:

- (void)fillUsingLinearGradientWithStartColour:(NSColor *)startColour startVector:(NSPoint)startVector endColour:(NSColor *)endColour endVector:(NSPoint)endVector
{
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
NSRect pathBounds;
CIColor *ciStartColour;
CIColor *ciEndColour;
CIVector *ciStartVector;
CIVector *ciEndVector;
CIFilter *gradientFilter;
CIImage *gradientImage;
CIContext *ciGraphicsContext;

pathBounds = [self bounds];

// convert colour space of NSColors
// core image expects calibrated RGB colours
startColour = [startColour colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
endColour = [endColour colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

// convert NSColors to CIColors
ciStartColour = [CIColor colorWithRed:[startColour redComponent] green:[startColour greenComponent] blue:[startColour blueComponent] alpha:[startColour alphaComponent]];
ciEndColour = [CIColor colorWithRed:[endColour redComponent] green: [endColour greenComponent] blue:[endColour blueComponent] alpha: [endColour alphaComponent]];

// create CIVectors from the NSPoints
ciStartVector = [CIVector vectorWithX:startVector.x Y:startVector.y];
ciEndVector = [CIVector vectorWithX:endVector.x Y:endVector.y];

// create and configure the gradient filter
gradientFilter = [CIFilter filterWithName:@"CILinearGradient"];
[gradientFilter setDefaults];
[gradientFilter setValue:ciStartColour forKey:@"inputColor0"];
[gradientFilter setValue:ciStartVector forKey:@"inputPoint0"];
[gradientFilter setValue:ciEndColour forKey:@"inputColor1"];
[gradientFilter setValue:ciEndVector forKey:@"inputPoint1"];

// extract the CIImage from the filter pipeline
gradientImage = [gradientFilter valueForKey:@"outputImage"];

// save the current graphics state and push a new drawing context onto the stack
[currentContext saveGraphicsState];

// obtain the current graphics context
ciGraphicsContext = [currentContext CIContext];

// clip the context to the bezier path
[self addClip];

// draw into the current graphics context
[ciGraphicsContext drawImage:gradientImage atPoint:CGPointMake (pathBounds.origin.x, pathBounds.origin.y) fromRect:CGRectMake (pathBounds.origin.x, pathBounds.origin.y, pathBounds.size.width, pathBounds.size.height)];

// restore previous graphics state
[currentContext restoreGraphicsState];
}


What am I doing incorrectly that just happens to work in Tiger, but not in Leopard? If I'm drawing an image that an NSView renders, or using this code with an NSView that's drawing itself, it doesn't seem to make a difference if the view is flipped or not. The gradient is always "backwards".



--
mikey-san



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.