• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSColor opacity
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSColor opacity


  • Subject: Re: NSColor opacity
  • From: email@hidden
  • Date: Tue, 30 Jan 2007 18:34:24 +1100

Well I figured out how to do the transparency thing using CGxxx functions. Here's the solution for anyone else who might be interested

NSRect rect = [self bounds];
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSetRGBFillColor (context, 0,1,0, .2);
CGContextFillRect (context, CGRectMake (0, 0, rect.size.width, rect.size.height));


This draws a 20% green screen over the entire bounds rect;

I suspect that depends on the blend mode of the context... although that doesn't seem like something that'll trip you up (blend mode != compositing mode; the latter is your main problem). The problem with your original code is that you're using NSRectFill, which has a default and fixed compositing mode of NSCompositeCopy. Thus the behaviour you're seeing; you want NSCompositeSourceOver instead. To specify that, simply replace your NSRectFill call with NSRectFillUsingOperation, and specify the mode you want. See:


http://developer.apple.com/documentation/Cocoa/Reference/ ApplicationKit/Miscellaneous/AppKit_Functions/Reference/ reference.html#//apple_ref/c/func/NSRectFillUsingOperation

	// overlay with hover color
	[tempColor set];
	NSRectFill([self bounds]);

	[context restoreGraphicsState];
}

What I would expect is a 10 percent green screen overlayong the specified rect What I get however is a 90 percent black rectangle.

Also tried changing the composite method by adding following line before the [tempColor set] command to no effect.

[context setCompositingOperation: NSCompositeSourceAtop];

NSFillRect ignores the context's compositing mode, and uses NSCompositeCopy as noted. Maybe it shouldn't - it probably seemed like a sensible default back when drawing with alpha was relatively expensive, and given most uses of NSFillRect may have been for opaque rects anyway - but it does.


Also note that NSCompositeSourceAtop probably isn't what you want - see the examples at:

http://developer.apple.com/documentation/Cocoa/Conceptual/ CocoaDrawingGuide/GraphicsContexts/chapter_3_section_3.html#// apple_ref/doc/uid/TP40003290-CH203-BCIDBEEB

Wade Tregaskis

    ICQ: 40056898
    AIM, Yahoo & Skype: wadetregaskis
    MSN: email@hidden
    iChat & email: email@hidden
    Jabber: email@hidden
    Google Talk: email@hidden

    http://homepage.mac.com/wadetregaskis/

-- Sed quis custodiet ipsos custodes?


_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


References: 
 >NSColor opacity (From: Ken Tozier <email@hidden>)
 >Re: NSColor opacity (From: "I. Savant" <email@hidden>)
 >Re: NSColor opacity (From: Ken Tozier <email@hidden>)
 >Re: NSColor opacity (From: Ken Tozier <email@hidden>)

  • Prev by Date: Re: NSColor opacity
  • Next by Date: Re: NSColor opacity
  • Previous by thread: Re: NSColor opacity
  • Next by thread: Re: NSColor opacity
  • Index(es):
    • Date
    • Thread