Mailing Lists: Apple Mailing Lists

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

Re: Darken image



well, i just wrote a tiny test harness for it, and it works as advertised...

this is a simple nsview subclass that lets you drag around the application icon... it shows the darkened one while you are dragging...

let me know if it doesn't work for you.


#import <Cocoa/Cocoa.h>

@interface IconSelectView : NSView
{
BOOL highlighted;
NSRect imageRect;
}
@end

@implementation IconSelectView

- (void)awakeFromNib
{
imageRect = NSMakeRect(100,100,32,32);
}

- (NSImage *)image
{
return [NSImage imageNamed:@"NSApplicationIcon"];
}

- (BOOL)isHighlighted
{
return highlighted;
}

- (void)mouseDown:(NSEvent *)e
{
NSPoint p = [self convertPoint: [e locationInWindow] fromView: nil];
NSSize offset = NSMakeSize(p.x - imageRect.origin.x, p.y - imageRect.origin.y);

if (NSPointInRect(p, imageRect)) {
highlighted = YES;
[self setNeedsDisplayInRect: imageRect];
do {
e = [[self window] nextEventMatchingMask:
NSLeftMouseDraggedMask | NSLeftMouseUpMask];
p = [self convertPoint: [e locationInWindow] fromView: nil];
[self setNeedsDisplayInRect: imageRect];
imageRect.origin.x = p.x - offset.width;
imageRect.origin.y = p.y - offset.height;
[self setNeedsDisplayInRect: imageRect];
} while ([e type] == NSLeftMouseDragged);
highlighted = NO;
}
}

- (void)drawRect:(NSRect)cellFrame
{
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: imageRect fromRect: NSZeroRect
operation: NSCompositeSourceOver fraction: 1.0];
}

@end

On Thursday, July 19, 2001, at 03:18 PM, Andreas Monitzer wrote:

On Thursday, July 19, 2001, at 11:12 , Tom Waters wrote:

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)

Hm... using your example, the image looks pretty well when used for NSView'
s dragImage:at:offset:event:pasteboard:source:slideBack:, but draws a black background when used in NSView's drawRect:. Looks pretty strange to me, where's the difference?

andy
--
"He was addicted to life. But we cured him"
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev


References: 
 >Re: Darken image (From: Andreas Monitzer <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.