Permalink
Browse files

Merge pull request #716 from jcbertin/develop

Fixed NSColor compatibility for deployment targets inferior to 10.8.
  • Loading branch information...
2 parents 638b218 + 993bda6 commit 49c4f3a997fcf71fef1b3805c07cf83bff9f1956 @odrobnik odrobnik committed Feb 5, 2014
Showing with 42 additions and 1 deletion.
  1. +42 −1 Core/Source/DTColor+Compatibility.m
@@ -22,9 +22,50 @@ - (CGFloat)alphaComponent
#else
+#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_7
+#import <objc/runtime.h>
+
+@interface NSColor (DTCoreText)
++ (NSColor *)DTCoreText_colorWithCGColor:(CGColorRef)cgColor;
+- (CGColorRef)DTCoreText_CGColor;
+@end
+
+static void DTCoreTextAddMissingSelector(Class aClass, SEL aSelector, SEL implementationSelector)
+{
+ Method method = class_getInstanceMethod(aClass, aSelector);
+ if (method == NULL) {
+ method = class_getInstanceMethod(aClass, implementationSelector);
+ NSCAssert(method != NULL, @"missing implementation method");
+
+ IMP methodImplementation = method_getImplementation(method);
+ const char *methodTypeEncoding = method_getTypeEncoding(method);
+
+#if !defined(NS_BLOCK_ASSERTIONS)
+ BOOL rc = class_addMethod(aClass, aSelector, methodImplementation, methodTypeEncoding);
+ NSCAssert(rc, @"failed to add missing method");
+#else
+ (void) class_addMethod(aClass, aSelector, methodImplementation, methodTypeEncoding);
+#endif
+ }
+}
+
+__attribute__((constructor))
+static void DTCoreTextNSColorInitialization(void)
+{
+ Class NSColorClass = objc_getClass("NSColor");
+ Class NSColorMetaClass = object_getClass(NSColorClass);
+ DTCoreTextAddMissingSelector(NSColorMetaClass, @selector(colorWithCGColor:), @selector(DTCoreText_colorWithCGColor:));
+ DTCoreTextAddMissingSelector(NSColorClass, @selector(CGColor), @selector(DTCoreText_CGColor));
+}
+
+#define colorWithCGColor DTCoreText_colorWithCGColor
+#define CGColor DTCoreText_CGColor
+#define HTML DTCoreText
+#endif
+
@implementation NSColor (HTML)
-#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_7
+#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_7 || MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_7
+ (NSColor *)colorWithCGColor:(CGColorRef)cgColor
{
size_t count = CGColorGetNumberOfComponents(cgColor);

0 comments on commit 49c4f3a

Please sign in to comment.