Re: -[NSColor setBackgroundColor:] not working in 10.6
Re: -[NSColor setBackgroundColor:] not working in 10.6
- Subject: Re: -[NSColor setBackgroundColor:] not working in 10.6
- From: Ken Ferry <email@hidden>
- Date: Thu, 7 Oct 2010 03:14:34 -0700
The problem here is the use of NSCompositeCopy. Try NSCompositeSourceOver.
When you draw from a source image into a destination, NSCompositeCopy
completely _replaces_ the pixel color in the destination with the color from
the source. If you draw a clear image into an orange context with composite
copy, the context will become clear. You may see black, or junk data, if
something is claiming to be opaque, and the system is taking a shortcut and
not drawing whatever's behind the thing. A window by default is assumed to
be opaque, so that's probably it. If you called [window setOpaque:NO] you'd
probably see a hole in your window instead of black. In the layer backed
case, you're making the layer clear.
NSCompositeSourceOver performs what you might think of as normal blending.
A clear image on an orange context produces an orange result.
The image's background color is barely used, and is not relevant in this
situation. The background color is a fallback that is only relevant in a
situation where there is no concept of transparency.
-Ken
On Wed, Oct 6, 2010 at 12:34 AM, Hamish Sanderson <
email@hidden> wrote:
> Hi,
>
> I have a simple test app that reads a PDF file into an NSImage, sets its
> background color to white, and draws it into an NSView subclass:
>
> @implementation CTView
>
> - (id)initWithFrame:(NSRect)frame {
> self = [super initWithFrame:frame];
> if (self) {
> NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
> pathForResource:@"img" ofType:@"pdf"]];
> img = [[NSImage alloc] initWithContentsOfURL: url];
> [img setBackgroundColor: [NSColor whiteColor]];
> }
> return self;
> }
>
> - (void)drawRect:(NSRect)rect {
> NSRect bounds = {{0,0}, {[img size].width * 1.5, [img size].height *
> 1.5}};
> [[NSColor blueColor] drawSwatchInRect:rect];
> [img drawInRect:rect fromRect:bounds operation:NSCompositeCopy
> fraction:1];
> }
>
> @end
>
> When run on 10.5, the transparent portions of the PDF are filled with white
> as intended. On 10.6, however, the transparent portions appear black (or
> transparent if the view is CA-enabled). Any ideas why it's not behaving on
> 10.6?
>
> Thanks,
>
> Hamish
> --
>
> Hamish Sanderson
> Production Workflow Developer
> Sun Branding Solutions Ltd
> Tel: +44(0)1274 200 700
> www.sunbrandingsolutions.com
>
>
>
>
>
> _______________________________________________
>
> 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:
>
> This email sent to email@hidden
>
_______________________________________________
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:
This email sent to email@hidden