• 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
Re: What is going wrong with my image coloring?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What is going wrong with my image coloring?


  • Subject: Re: What is going wrong with my image coloring?
  • From: Pascal Pochet <email@hidden>
  • Date: Sun, 10 Dec 2006 09:43:10 +0100

Le 08-déc.-06 à 21:34, Alan Smith a écrit :

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:
40mac.com


This email sent to email@hidden

The only images of which the bits are guarantee to be mutable are the ones you create yourself…
or the one you create with initWithContentsOfFile and the likes…


Ones coming from [NSImage imageNamed:…] will probably not be mutable…

Your code works perfectly well on images loaded from disk… with initWithContentsOfFile…



Pascal Pochet
email@hidden


NB
not all colors scheme returns a color with an alpha channel when using colorWithAlphaComponent…




Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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: "Alan Smith" <email@hidden>
References: 
 >What is going wrong with my image coloring? (From: "Alan Smith" <email@hidden>)

  • Prev by Date: Re: Init with something....
  • Next by Date: Re: Init with something....
  • Previous by thread: What is going wrong with my image coloring?
  • Next by thread: Re: What is going wrong with my image coloring?
  • Index(es):
    • Date
    • Thread