NSColor to CGColor

  • Hi,

    Couldn't find any pointers in the Apple docs (other than that the two
    types appear not to be toll-free bridged), and amazingly searching in
    cocoabuilder on "CGColor" yields no results at all. Nada. So no info
    on converting an NSColor to a CGColor. Googling for these two words
    yields no usable results either, so hence the question here: What is
    the best/easiest way to convert an NSColor to a CGColor?

    Thanks,
    António

    -----------------------------------------------------------
    And could you keep your heart in wonder
    at the daily miracles of your life,
    your pain would not seem less wondrous
    than your joy.

    --Kahlil Gibran
    -----------------------------------------------------------
  • On 12 nov 2006, at 16.32, Antonio Nunes wrote:

    > Couldn't find any pointers in the Apple docs (other than that the
    > two types appear not to be toll-free bridged), and amazingly
    > searching in cocoabuilder on "CGColor" yields no results at all.
    > Nada. So no info on converting an NSColor to a CGColor. Googling
    > for these two words yields no usable results either, so hence the
    > question here: What is the best/easiest way to convert an NSColor
    > to a CGColor?

    I also bumped into this problem recently and ended up implementing
    this function:

    static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef
    colorSpace, NSColor *color)
    {
    NSColor *deviceColor = [color colorUsingColorSpaceName:
    NSDeviceRGBColorSpace];

    float components[4];
    [deviceColor getRed: &components[0] green: &components[1] blue:
    &components[2] alpha: &components[3]];

    return CGColorCreate (colorSpace, components);
    }

    That I call like this:

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
    CGColorRef cgColor = CGColorCreateFromNSColor (colorSpace, nsColor);
    CGColorSpaceRelease (colorSpace);
    // Do stuff with cgColor
    CGColorRelease (cgColor);

    j o a r
  • On Nov 12, 2006, at 10:32 AM, Antonio Nunes wrote:
    > Hi,
    >
    > Couldn't find any pointers in the Apple docs (other than that the
    > two types appear not to be toll-free bridged), and amazingly
    > searching in cocoabuilder on "CGColor" yields no results at all.
    > Nada. So no info on converting an NSColor to a CGColor. Googling
    > for these two words yields no usable results either, so hence the
    > question here: What is the best/easiest way to convert an NSColor
    > to a CGColor?
    >
    > Thanks,
    > António

    When I've had to do this I've just init'ed a new CGColor based on the
    components of the NSColor. Something like:

    NSColor* someNSColor = [NSColor redColor];
    CGColorRef someCGColor = NULL;
    CGColorSpaceRef genericRGBSpace = CGColorSpaceCreateWithName
    (kCGColorSpaceGenericRGB);
    if (genericRGBSpace != NULL)
    {
    float colorComponents[4] =  = {[someNSColor redComponent],
    [someNSColor greenComponent], [someNSColor blueComponent],
    [someNSColor alphaComponent]};
    someCGColor = CGColorCreate(genericRGBSpace, colorComponents);
    CGColorSpaceRelease(genericRGBSpace);
    }
    // got someCGColor? have fun...

    Apply colorspace finickiness as necessary...

    Daniel
  • On Nov 12, 2006, at 10:48 AM, j o a r wrote:

    > static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef
    > colorSpace, NSColor *color)
    > {
    > NSColor *deviceColor = [color colorUsingColorSpaceName:
    > NSDeviceRGBColorSpace];
    >
    > float components[4];
    > [deviceColor getRed: &components[0] green: &components[1] blue:
    > &components[2] alpha: &components[3]];
    >
    > return CGColorCreate (colorSpace, components);
    > }

    Joar's is much smarter and generically applicable than the example I
    pasted....

    Daniel
  • On 12 nov 2006, at 16.58, Daniel Jalkut wrote:

    > Joar's is much smarter and generically applicable than the example
    > I pasted....

    Thanx!

    Though, I should note that my version is not universally applicable -
    for example it doesn't work for all types of NSColor! The return
    value from "-[NSColor colorUsingColorSpaceName:]" should be checked
    (as it can be nil), and depending on how this code will be used,
    there might be other places where you would want to check return values.

    j o a r
  • On 12 Nov 2006, at 15:48, j o a r wrote:

    > I also bumped into this problem recently and ended up implementing
    > this function:
    >
    >
    > static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef
    > colorSpace, NSColor *color)
    > {
    > NSColor *deviceColor = [color colorUsingColorSpaceName:
    > NSDeviceRGBColorSpace];
    >
    > float components[4];
    > [deviceColor getRed: &components[0] green: &components[1] blue:
    > &components[2] alpha: &components[3]];
    >
    > return CGColorCreate (colorSpace, components);
    > }
    >
    >
    > That I call like this:
    >
    >
    > CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
    > CGColorRef cgColor = CGColorCreateFromNSColor (colorSpace, nsColor);
    > CGColorSpaceRelease (colorSpace);
    > // Do stuff with cgColor
    > CGColorRelease (cgColor);

    Thanks Joar (and Daniel),

    The above should do just fine for my purposes. I had a feeling it
    might be something of the sort, but wanted to make sure I wasn't
    overlooking anything.

    Cheers,
    António

    -----------------------------------------------------------
    And could you keep your heart in wonder
    at the daily miracles of your life,
    your pain would not seem less wondrous
    than your joy.

    --Kahlil Gibran
    -----------------------------------------------------------
  • Am 12.11.2006 um 16:48 schrieb j o a r:
    > I also bumped into this problem recently and ended up implementing
    > this function:
    >
    > static CGColorRef CGColorCreateFromNSColor (CGColorSpaceRef
    > colorSpace, NSColor *color)
    > {
    > NSColor *deviceColor = [color colorUsingColorSpaceName:
    > NSDeviceRGBColorSpace];
    >
    > float components[4];
    > [deviceColor getRed: &components[0] green: &components[1] blue:
    > &components[2] alpha: &components[3]];
    >
    > return CGColorCreate (colorSpace, components);
    > }

    Well, if someone passes in a CMYK color, this may cause the color to
    change slightly. So, it may be desirable to check color's
    colorSpaceName and, where possible, use an equivalent CGColorSpaceRef
    etc.

    Cheers,
    -- M. Uli Kusterer
    http://www.zathras.de
previous month november 2006 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
Go to today
MindNode
MindNode offered a free license !