If I'm not really fussy about the exact tone etc of the dimmed image, is there a quick and dirty way to draw an NSImage slightly dimmed, like this? I've searched online and can't really find what I'm looking for (but I'm not very good when it comes to understanding graphics and the correct technical terms).
I'm actually dimming icons for the same use-case as Xcode dimming icons like above (i.e. document has unsaved changes).
This works fine for me:
NSImage *iconImage = [NSImage imageNamed:@"Icon"];
NSSize iconSize = [iconImage size];
NSRect iconRect = NSMakeRect(0.0, 0.0, iconSize.width, iconSize.height);
[iconImage lockFocus];
[[NSColor colorWithCalibratedWhite:0.0 alpha:0.33] set];
NSRectFillUsingOperation(iconRect, NSCompositeSourceAtop);
[iconImage unlockFocus];
[iconImage drawInRect:iconRect
fromRect:iconRect
operation:NSCompositeSourceOver
fraction:0.75];
Basically I'm adding a black layer with 33% opacity on top of the actual icon (masking it with NSCompositeSourceAtop
). And then I'm just drawing the dimmed icon with an opacity of 75%.
[Edit: got rid of temporary black image by using NSRectFillUsingOperation(...), as advised by Nikolai Ruhe]