Example: How to Tint an Image
Example: How to Tint an Image
- Subject: Example: How to Tint an Image
- From: Philip Weaver <email@hidden>
- Date: Mon, 18 Mar 2002 20:28:18 -0600
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.