0

I am using NSPopUpButton with title and image. Below is my code:

[self.popup addItemWithTitle:@"Parag"];
[[self.popup lastItem] setImage:[NSImage swatchWithColor:[NSColor greenColor] size:NSMakeSize(10.0, 10.0)]];

Creating NSImage from NSColor

@interface NSImage (ImageAdditions)

+(NSImage *)swatchWithColor:(NSColor *)color size:(NSSize)size;


@end

@implementation NSImage (ImageAdditions)

+(NSImage *)swatchWithColor:(NSColor *)color size:(NSSize)size
{
    NSImage *image = [[NSImage alloc] initWithSize:size];
    [image lockFocus];
    [color drawSwatchInRect:NSMakeRect(0, 0, size.width, size.height)];
    [image unlockFocus];
    return image;
}

@end

Color of image becomes dark if I select popup button:

enter image description here

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144

1 Answers1

0

Have you tried to use NSRectFill to draw the color to the image? As far as I know, drawSwatchInRect does some alpha compositing.

@implementation NSImage (ImageAdditions)

+(NSImage *)swatchWithColor:(NSColor *)color size:(NSSize)size
{
    NSImage *image = [[NSImage alloc] initWithSize:size];
    [image lockFocus];
    [color set];
    NSRectFill(NSMakeRect(0, 0, size.width, size.height));
    [image unlockFocus];
    return image;
}

@end
Stefanf
  • 1,663
  • 13
  • 15