Suhas Aithal
Suhas Aithal

Reputation: 852

How can i convert NSNamedColorSpace colour to NSCalibratedRGBColorSpace colour? then to CGColor in lion?

I want to convert any colour which comes as a input to CGColor . Everything works fine until in comming colour is of space NSCalibratedRGBColorSpace, but when it is of type NSNamedColorSpace (NSSelectedMenuItemColor) it won't produce a proper result in lion. Tried getting patternImage too but NSNamedColorSpace colours does not respond to that API. I am sure many had faced this issue, kindly help me if you can.

Part of code where i am trying to covert

       if ([colorSpaceName isEqualToString:NSNamedColorSpace])
        {
            color = [color colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
        }

        NSColorSpace *colorSpaceNS = [color colorSpace];
        colorSpace = [colorSpaceNS CGColorSpace];

        size_t numberOfComponents = CGColorSpaceGetNumberOfComponents(colorSpace);
        CGFloat components[numberOfComponents];
        [color getComponents:components];
        requiredColor = CGColorCreate(colorSpace, components);
        CGColorSpaceRelease(colorSpace);

Upvotes: 0

Views: 1767

Answers (1)

Nikolai Nagornyi
Nikolai Nagornyi

Reputation: 1461

The simplest way (OS X 10.8 or later):

if ([colorSpaceName isEqualToString:NSNamedColorSpace])
        {
            color = [color colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
        }

        requiredColor = [color CGColor];

Second way (OS X 10.0 or later):

if ([colorSpaceName isEqualToString:NSNamedColorSpace])        {
      color = [color colorUsingColorSpace:[NSColorSpace genericRGBColorSpace]];
}

CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];

requiredColor = CGColorCreateGenericRGB(red, green, blue, alpha);

Upvotes: 1

Related Questions