Re: Example: How to Tint an Image
Re: Example: How to Tint an Image
- Subject: Re: Example: How to Tint an Image
- From: Alex Keresztes <email@hidden>
- Date: Sun, 17 Mar 2002 23:01:36 -0600
I just do the following and it works fine:
- (void)tintDockIcon: (NSColor *)color withImage: (NSImage *)image
{
if (color) {
NSImage *icon = [image copy];
NSSize iconSize = [icon size];
NSRect iconRect = {NSZeroPoint, iconSize};
[icon lockFocus];
[[color colorWithAlphaComponent: 0.5] set];
NSRectFillUsingOperation(iconRect, NSCompositeSourceAtop);
[icon unlockFocus];
[NSApp setApplicationIconImage: icon];
[icon release];
}else{
[NSApp setApplicationIconImage: image];
}
}
- (void)awakeFromNib
{
NSImage *appImage = [[NSApp applicationIconImage] copy];
// tint to red color
[self tintDockIcon: [NSColor redColor] withImage: appImage];
[appImage release];
}
I dont know if it produces better or worse results because I havent
tried the code u posted.
Alex
On Monday, March 18, 2002, at 08:28 PM, Philip Weaver wrote:
Was searching the archives (http://cocoa.mamasam.com/) for clues on how
to tint an image with transparent surroundings and didn't find too
much. I was specifically trying to tint the application's dock icon.
So, posting this one for the archives unless you're interested.
Below, tintImage is an opaque rectangle of the color in which to tint.
tintMaskImage is a mask (filled outline) of the application icon of the
tint color. Then, tintMaskImage is composited over a dissolved copy of
the application image.
The method assumes that you've captured (and copied) the application's
icon image earlier, say at startup. If you do not store a copy, your
dock image will eventually look like mud. So use:
originalApplicationIconImage = [[[NSApplication sharedApplication]
applicationIconImage] copy];
[originalApplicationIconImage retain];
BTW, let me know if there's a way to do this without the mask image.
Philip
--------
SOURCE
--------
- (void)tintDockIcon:(NSColor *)tintColor
{
NSApplication *theApp = [NSApplication sharedApplication];
NSImage *tintImage;
NSImage *tintMaskImage;
NSImage *newDockImage;
NSBezierPath *thePath;
float fWidth = 128.0;
float fHeight = 128.0;
float fDissolve = 0.6;
if ([tintColor isEqual:[NSColor whiteColor]]) { // no tint,
restore original app icon
[theApp setApplicationIconImage:[[originalApplicationIconImage
copy] autorelease]];
} else { // do the tinting
tintImage = [[[NSImage alloc] initWithSize:NSMakeSize(fWidth,
fHeight)] autorelease];
[tintImage lockFocus];
thePath = [NSBezierPath bezierPathWithRect:NSMakeRect(0, 0,
fWidth, fHeight)];
[tintColor set];
[thePath fill];
[tintImage unlockFocus];
tintMaskImage = [[[NSImage alloc]
initWithSize:NSMakeSize(fWidth, fHeight)] autorelease];
[tintMaskImage lockFocus];
[originalApplicationIconImage
compositeToPoint:NSMakePoint(0,0) operation:NSCompositeCopy];
[tintImage compositeToPoint:NSMakePoint(0, 0)
operation:NSCompositeSourceIn];
[tintMaskImage unlockFocus];
newDockImage = [[[NSImage alloc]
initWithSize:NSMakeSize(fWidth, fHeight)] autorelease];
[newDockImage lockFocus];
[originalApplicationIconImage
dissolveToPoint:NSMakePoint(0,0) fraction:fDissolve];
[tintMaskImage compositeToPoint:NSMakePoint(0, 0)
operation:NSCompositeDestinationAtop];
[newDockImage unlockFocus];
[theApp setApplicationIconImage:newDockImage];
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.