Re: Darken image
Re: Darken image
- Subject: Re: Darken image
- From: Tom Waters <email@hidden>
- Date: Thu, 19 Jul 2001 17:19:50 -0700
Ok, now I'm getting carried away!
I made the example use the same code for the drawRect and the
dragImage...
Plus, I made it use an arbitrary color to do the highlighting... (not
because it looks very good, but because you can!) When you drag it
around the view, it is green and if you press command and drag, it sets
the drag image to be a blue highlighted version of the image... also it
has an alpha parameter so the darkened/colored image can be
semi-transparent.
The cmd-drag pasteboard contains the TIFF of the application icon that
you can drop into Textedit or Mail.
I hope this helps.
-Tom
ps. I put the project in a dmg at
http://www.whidbeysoft.com/downloads/TestSelectedIcon.dmg
#import <Cocoa/Cocoa.h>
@interface IconSelectView : NSView
{
BOOL highlighted;
NSRect imageRect;
}
@end
@implementation IconSelectView
- (void)awakeFromNib
{
imageRect = NSMakeRect(100,100,48,48);
}
- (NSImage *)image
{
return [NSImage imageNamed:@"NSApplicationIcon"];
}
- (BOOL)isHighlighted
{
return highlighted;
}
- (NSImage *)scratchImage:(NSSize)size
{
static NSImage *im;
if (im == nil || NSEqualSizes(size, [im size])) {
[im release];
im = [[NSImage alloc] initWithSize:size];
}
return im;
}
- (NSImage *)coloredImage:(NSColor *)color withAlpha:(float)alpha
{
NSImage *icon = [self image];
NSSize iconSize = [icon size];
NSRect iconRect = {NSZeroPoint, iconSize};
id im = [self scratchImage:iconSize];
[im lockFocus];
// copy the icon into the new one
[icon drawInRect:iconRect fromRect:NSZeroRect
operation:NSCompositeCopy fraction:alpha];
// paint 50% transparent color atop it to tint it.
[[color colorWithAlphaComponent:.5] set];
NSRectFillUsingOperation(iconRect, NSCompositeSourceAtop);
[im unlockFocus];
return im;
}
- (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)) {
if ([e modifierFlags] & NSCommandKeyMask) {
NSPasteboard *pboard = [NSPasteboard
pasteboardWithName:NSDragPboard];
[pboard declareTypes:[NSArray
arrayWithObject:NSTIFFPboardType] owner:self];
[pboard set
Data:[[self image] TIFFRepresentation]
forType:NSTIFFPboardType];
[self dragImage:[self coloredImage:[NSColor blueColor] withAlpha:0.6]
at:NSMakePoint(p.x - offset.width, p.y - offset.height)
offset:offset
event:e
pasteboard:pboard
source:self
slideBack:YES];
} else {
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 isHighlighted] ?
[self coloredImage:[NSColor greenColor] withAlpha:0.8] :
[self image];
[icon drawInRect:imageRect fromRect:NSZeroRect
operation:NSCompositeSourceOver fraction:1.0];
}
@end
On Thursday, July 19, 2001, at 03:44 PM, Tom Waters wrote:
>
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.
>
--8-<-- snipped old code -->-8--
>
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?