Search code examples
cocoaopacityalphanscolornscolorpanel

How can you get the shared NSColorPanel to an show alpha / opacity slider?


When you add an NSColorWell control, and click it, it displays the shared instance of NSColorPanel. Unfortunately, by default it does not show the alpha / opacity slider. This is also true when it is invoked from the default MainMenu > Format > Font > Show Colors


Solution

  • Simply call the following line at any time in your app.

    [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];

    You can call it once in applicationDidFinishLaunching: or you can easily tie it to a switch like an NSButton checkbox with a simple IBAction method like this:

    - (IBAction)showAlphaSliderInColorPanel:(id)sender {
        if ([sender state] == NSOnState) {
            [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
        } else {
            [[NSColorPanel sharedColorPanel] setShowsAlpha:NO];
        }
    }
    

    Just connect that to the Sent Actions selector item in the Connections Inspector with for a button configured to have an on / off state.

    The change will occur live as you click.

    A great example of how awesome Cocoa is when you want it to be.