• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
What is going wrong with my image coloring?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

What is going wrong with my image coloring?


  • Subject: What is going wrong with my image coloring?
  • From: "Alan Smith" <email@hidden>
  • Date: Fri, 8 Dec 2006 15:34:46 -0500

Hello everyone,

I'm in need of a method to color a NSImage. In the end I want it to be
a category on NSImage. Here is the code so far, it works great.

- (NSImage*)colorImage:(NSImage*)image withColor:(NSColor*)color
alpha:(float)alpha
{
	NSSize iconSize = [image size];
	NSRect iconRect = NSMakeRect(0, 0, iconSize.width, iconSize.height);
	NSImage *scratchImage = [[NSImage alloc] initWithSize: iconSize];

	[scratchImage lockFocus];

	[image drawInRect:iconRect fromRect:NSZeroRect
operation:NSCompositeCopy fraction: 1.0];

	[[color colorWithAlphaComponent: alpha] set];

	NSRectFillUsingOperation(iconRect, NSCompositeSourceAtop);

	[scratchImage unlockFocus];

	return scratchImage;
}

But I want to remove the copying, I want to edit the original. So, I tried this:

- (NSImage*)colorImage:(NSImage*)image withColor:(NSColor*)color
alpha:(float)alpha
{
	NSSize iconSize = [image size];
	NSRect iconRect = NSMakeRect(0, 0, iconSize.width, iconSize.height);

	[image lockFocus];

	[[color colorWithAlphaComponent: alpha] set];

	NSRectFillUsingOperation(iconRect, NSCompositeSourceAtop);

	[image unlockFocus];

	return image;
}

This *doesn't* work, for reasons unknown. It returns the image without
error but it is not colored. This was not originally my code so you
can't blame me for freaky code. :P

I'm not well versed in graphic guff so go easy on me if the solution
is obvious to you.

Thanks, Alan

--
// Quotes from yours truly -------------------------
"You don't forget, you just don't remember."
"Maturity resides in the mind."
"Silence is the Universe's greatest gift."
"When the World realizes that religion really is unnecessary, then it
shall evolve."
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: What is going wrong with my image coloring?
      • From: Pascal Pochet <email@hidden>
  • Prev by Date: Re: NSTextView Not Drawing Text
  • Next by Date: CurrencyConverter not precise
  • Previous by thread: Mapping OBJC problem...
  • Next by thread: Re: What is going wrong with my image coloring?
  • Index(es):
    • Date
    • Thread