Cocoa: modal NSColorPanel dialog.

Apple, Cocoa, General

Hi all,
I’ve seen some people request how to do a modal color panel. Well, carbon has GetColor(..) which ultimately uses the same NSColorPanel. So, you can set it to begin a modal session before you call GetColor(..). Here’s a code snippet:

#import <Carbon/Carbon.h>

– (IBAction)buttonClick:(id)sender

{

    NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel];

    NSModalSession session = [NSApp beginModalSessionForWindow:colorPanel];

    

    RGBColor inColor = {128, 128, 128};

    RGBColor outColor = {0, 0, 0};

    Point point = {0, 0};

    if (GetColor(point, “\p”, &inColor, &outColor)) {

        NSColor *color = [NSColor colorWithCalibratedRed:(float)outColor.red / (float)UINT16_MAX green:(float)outColor.green / (float)UINT16_MAX blue:(float)outColor.blue / (float)UINT16_MAX alpha:1.0];

        [colorWell setColor:color];        

    }

    

    [NSApp endModalSession:session];    

}



Subscribe
Notify of
guest

1 Comment
Inline Feedbacks
View all comments

Subscribe to new posts:

You'll get an email whenever a I publish a new post to my blog and nothing more. -- Corbin

As an Amazon Associate I earn from qualifying purchases.

(c) 2008-2024 Corbin Dunn

Privacy Policy

Subscribe to RSS feeds for entries.

72 queries. 0.206 seconds.

Log in