Re: Darken image
Re: Darken image
- Subject: Re: Darken image
- From: Tom Waters <email@hidden>
- Date: Thu, 19 Jul 2001 14:12:48 -0700
Here's how I do it in FileView (one of the many "everybody else" apps)
You need to make a copy of the image, using NSCompositeCopy, then do a
rectFill over the top of it using semi-transparent black (or any other
color if you want to tint it) using NSCompositeSourceAtop, then use that
copy of the image just as you would the original.
Here's my code with some other stuff hacked out of it, but I think it
should still compile. (note that I cache that image alloc/init in a
singleton, but i just left it in here to make the example simpler)
- (void)drawInteriorWithFrame:(NSRect)cellFrame
inView:(NSView *)controlView
{
NSImage *icon = [self image];
if ([self isHighlighted]) {
NSSize iconSize = [icon size];
NSRect iconRect = {NSZeroPoint, iconSize};
id im = [[NSImage alloc] initWithSize: iconSize];
[im lockFocus];
// copy the icon into the new one
[icon drawInRect: iconRect fromRect: NSZeroRect
operation: NSCompositeCopy fraction: 1.0];
// paint 50% black atop it to make it dark.
[[[NSColor blackColor] colorWithAlphaComponent: .5] set];
NSRectFillUsingOperation(iconRect, NSCompositeSourceAtop);
[im unlockFocus];
icon = im;
}
[icon drawInRect: cellFrame fromRect: NSZeroRect
operation: NSCompositeSourceOver fraction: 1.0];
}
On Thursday, July 19, 2001, at 01:49 PM, Andreas Monitzer wrote:
I've got an NSImage (a file icon) I'm successfully drawing using
[[self image] drawInRect:imageRect
fromRect:theRect
operation:NSCompositeSourceOver
fraction:1.0];
Now I want to "select" it (like NSToolbar, the Finder, the Dock and
everybody else does), which means making it darker. How should I do
that, while preserving transparency? I've tried several combinations of
operations and fractions, nothing did what I need.
andy
References: | |
| >Darken image (From: Andreas Monitzer <email@hidden>) |