I'm trying to change the colour of an image with the switching of dark/light mode in an NSViewController
.
I'm using this code for changing the colour of the image:
- (NSImage *)image:(NSImage *)image withColour:(NSColor *)colour
{
NSImage *img = image.copy;
[img lockFocus];
[colour set];
NSRect imageRect = NSMakeRect(0, 0, img.size.width, img.size.height);
NSRectFillUsingOperation(imageRect, NSCompositingOperationSourceAtop);
[img unlockFocus];
return img;
}
I've tried calling this method from viewWillLayout
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
but it seems the system color always returns the same RGB values.
I've also tried listening for the notification AppleInterfaceThemeChangedNotification
but even in here it seems the RGB values stay the same 1.000000 0.231373 0.188235
.
[[NSDistributedNotificationCenter defaultCenter] addObserverForName:@"AppleInterfaceThemeChangedNotification"
object:nil
queue:nil
usingBlock:^(NSNotification * _Nonnull note) {
NSLog(@"AppleInterfaceThemeChangedNotification");
self.help1Image.image = [self image:self.help1Image.image withColour:[NSColor systemRedColor]];
NSColorSpace *colorSpace = [NSColorSpace sRGBColorSpace];
NSColor *testColor = [[NSColor systemBlueColor] colorUsingColorSpace:colorSpace];
CGFloat red = [testColor redComponent];
CGFloat green = [testColor greenComponent];
CGFloat blue = [testColor blueComponent];
NSLog(@"%f %f %f", red, green, blue);
}];
I have the working fine in an NSButtonCell
sublass and overriding layout
but can't get it working in an NSViewController