Skip to content

Instantly share code, notes, and snippets.

@ShivaHuang
Created January 11, 2013 16:03
Show Gist options
  • Save ShivaHuang/4511790 to your computer and use it in GitHub Desktop.
Save ShivaHuang/4511790 to your computer and use it in GitHub Desktop.
//
// NSColor+CGColor.m
//
// http://stackoverflow.com/questions/11950173/conditional-categories-in-mountain-lion
//
#import <objc/runtime.h>
static CGColorRef _NSColor_CGColor_(Class self, SEL cmd) {
const NSInteger numberOfComponents = [(id)self numberOfComponents];
CGFloat components[numberOfComponents];
CGColorSpaceRef colorSpace = [[(id)self colorSpace] CGColorSpace];
[(id)self getComponents:(CGFloat *)&components];
return (CGColorRef)[(id)CGColorCreate(colorSpace, components) autorelease];
}
static NSColor* _NSColor_colorWithCGColor_(Class self, SEL cmd, CGColorRef CGColor) {
if (CGColor == NULL) return nil;
return [NSColor colorWithCIColor:[CIColor colorWithCGColor:CGColor]];
}
__attribute__((constructor))
static void initialize_NSColor_CGColorAdditions() {
if (![[NSColor class] respondsToSelector:@selector(colorWithCGColor:)]) {
class_addMethod(objc_getMetaClass("NSColor"), @selector(colorWithCGColor:), (IMP)_NSColor_colorWithCGColor_, "@@:@");
}
if (![[NSColor class] instancesRespondToSelector:@selector(CGColor)]) {
class_addMethod(objc_getClass("NSColor"), @selector(CGColor), (IMP)_NSColor_CGColor_, "@@:");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment