using OS X NSColorspace to toggle colorsync profiles

446 Views Asked by At

When I try calling setColorSpace on my NSWindow object I get no change in color. I was under the impression that I could dynamically change the way color is rendered.

Here is the .h file for my controller

#import <Cocoa/Cocoa.h>

@interface MainWindow : NSWindowController <NSTextFieldDelegate>
{

}
@property (strong) IBOutlet NSWindow *theWindow;
@property (weak) IBOutlet NSTextField *RedField;
@property (weak) IBOutlet NSTextField *GreenField;
@property (weak) IBOutlet NSTextField *BlueField;
@property (weak) IBOutlet NSTextField *PatternField;
@property (weak) IBOutlet NSButton *ICCBox;
- (IBAction)UpdateICC:(id)sender;

@end

Here is the .m file for my controller

#import "MainWindow.h"
#import <AppKit/AppKit.h>

@interface MainWindow ()

@end

@implementation MainWindow


- (id)init
{
   self = [super init];

    return self;
}

- (void)awakeFromNib
{
    [_RedField setDelegate:self];
    [_GreenField setDelegate:self];
    [_BlueField setDelegate:self];
}


-(void) controlTextDidChange:(NSNotification *) note {

    float redByte = [_RedField floatValue];
    float redF = redByte/255.0;

    float greenByte = [_GreenField floatValue];
    float greenF = greenByte/255.0;

    float blueByte = [_BlueField floatValue];
    float blueF = blueByte/255.0;

    _PatternField.backgroundColor = [NSColor colorWithCalibratedRed:redF green:greenF blue:blueF alpha:1];
}

- (IBAction)UpdateICC:(id)sender {

    NSColorSpace *acs = [NSColorSpace adobeRGB1998ColorSpace];
    NSColorSpace *scs = [NSColorSpace sRGBColorSpace];
    NSColorSpace *dcs = [NSColorSpace deviceRGBColorSpace];

    if(_ICCBox.state == NSOnState)
    {
        [_theWindow setColorSpace:scs];
    }
    else
    {
        [_theWindow setColorSpace:dcs];
    }

}
@end

Any idea why this isn't working?

1

There are 1 best solutions below

0
Yuriy Kalychak On

You should post specified notification to default NotificationCenter to immediately apply changes (cause redraw window with new color profile).

- (IBAction)UpdateICC:(id)sender {

    NSColorSpace *acs = [NSColorSpace adobeRGB1998ColorSpace];
    NSColorSpace *scs = [NSColorSpace sRGBColorSpace];
    NSColorSpace *dcs = [NSColorSpace deviceRGBColorSpace];

    if(_ICCBox.state == NSOnState)
    {
        [_theWindow setColorSpace:scs];
    }
    else
    {
        [_theWindow setColorSpace:dcs];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:NSWindowDidChangeScreenNotification object:_theWindow];
    // In some cases additional call needed:
    [_theWindow.contentView viewDidChangeBackingProperties];
}