From 77e70975b7b146acfb34b89801f1df8102ef8fd0 Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Sun, 29 Jul 2012 16:22:24 -0700 Subject: [PATCH] Added NSColor extensions to convert to and from CGColor --- Rebel.xcodeproj/project.pbxproj | 16 ++++++ Rebel/NSColor+RBLAdditions.h | 30 +++++++++++ Rebel/NSColor+RBLAdditions.m | 91 +++++++++++++++++++++++++++++++++ Rebel/Rebel.h | 1 + 4 files changed, 138 insertions(+) create mode 100644 Rebel/NSColor+RBLAdditions.h create mode 100644 Rebel/NSColor+RBLAdditions.m diff --git a/Rebel.xcodeproj/project.pbxproj b/Rebel.xcodeproj/project.pbxproj index 11a179a..0d95bfe 100644 --- a/Rebel.xcodeproj/project.pbxproj +++ b/Rebel.xcodeproj/project.pbxproj @@ -12,6 +12,8 @@ D0723FC015C5F9D8004DBDC7 /* RBLView.h in Headers */ = {isa = PBXBuildFile; fileRef = D0723FBE15C5F9D8004DBDC7 /* RBLView.h */; settings = {ATTRIBUTES = (Public, ); }; }; D0723FC115C5F9D8004DBDC7 /* RBLView.m in Sources */ = {isa = PBXBuildFile; fileRef = D0723FBF15C5F9D8004DBDC7 /* RBLView.m */; }; D0723FC215C5FA13004DBDC7 /* Rebel.h in Headers */ = {isa = PBXBuildFile; fileRef = D09AE4EF15C5F45200ECAD10 /* Rebel.h */; settings = {ATTRIBUTES = (Public, ); }; }; + D0723FC615C5FB68004DBDC7 /* NSColor+RBLAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = D0723FC415C5FB68004DBDC7 /* NSColor+RBLAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; }; + D0723FC715C5FB68004DBDC7 /* NSColor+RBLAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = D0723FC515C5FB68004DBDC7 /* NSColor+RBLAdditions.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; D09AE4E315C5F45200ECAD10 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D09AE4E215C5F45200ECAD10 /* Cocoa.framework */; }; D09AE4ED15C5F45200ECAD10 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = D09AE4EB15C5F45200ECAD10 /* InfoPlist.strings */; }; D09AE4F915C5F45300ECAD10 /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D09AE4F815C5F45300ECAD10 /* SenTestingKit.framework */; }; @@ -122,6 +124,8 @@ D04844EC15C5F8DA00834EDE /* RebelTests-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "RebelTests-Prefix.pch"; sourceTree = ""; }; D0723FBE15C5F9D8004DBDC7 /* RBLView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RBLView.h; sourceTree = ""; }; D0723FBF15C5F9D8004DBDC7 /* RBLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RBLView.m; sourceTree = ""; }; + D0723FC415C5FB68004DBDC7 /* NSColor+RBLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSColor+RBLAdditions.h"; sourceTree = ""; }; + D0723FC515C5FB68004DBDC7 /* NSColor+RBLAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSColor+RBLAdditions.m"; sourceTree = ""; }; D09AE4DF15C5F45200ECAD10 /* Rebel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Rebel.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D09AE4E215C5F45200ECAD10 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; D09AE4E515C5F45200ECAD10 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; @@ -255,6 +259,15 @@ name = Classes; sourceTree = ""; }; + D0723FC315C5FB54004DBDC7 /* Extensions */ = { + isa = PBXGroup; + children = ( + D0723FC415C5FB68004DBDC7 /* NSColor+RBLAdditions.h */, + D0723FC515C5FB68004DBDC7 /* NSColor+RBLAdditions.m */, + ); + name = Extensions; + sourceTree = ""; + }; D09AE4D315C5F45200ECAD10 = { isa = PBXGroup; children = ( @@ -302,6 +315,7 @@ children = ( D09AE4EF15C5F45200ECAD10 /* Rebel.h */, D0723FBD15C5F9CD004DBDC7 /* Classes */, + D0723FC315C5FB54004DBDC7 /* Extensions */, D09AE4E915C5F45200ECAD10 /* Supporting Files */, ); path = Rebel; @@ -345,6 +359,7 @@ files = ( D0723FC215C5FA13004DBDC7 /* Rebel.h in Headers */, D0723FC015C5F9D8004DBDC7 /* RBLView.h in Headers */, + D0723FC615C5FB68004DBDC7 /* NSColor+RBLAdditions.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -527,6 +542,7 @@ buildActionMask = 2147483647; files = ( D0723FC115C5F9D8004DBDC7 /* RBLView.m in Sources */, + D0723FC715C5FB68004DBDC7 /* NSColor+RBLAdditions.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Rebel/NSColor+RBLAdditions.h b/Rebel/NSColor+RBLAdditions.h new file mode 100644 index 0000000..1fa20ee --- /dev/null +++ b/Rebel/NSColor+RBLAdditions.h @@ -0,0 +1,30 @@ +// +// NSColor+RBLAdditions.h +// Rebel +// +// Created by Justin Spahr-Summers on 01.12.11. +// Copyright (c) 2012 GitHub. All rights reserved. +// +// Portions copyright (c) 2011 Bitswift. All rights reserved. +// See the LICENSE file for more information. +// + +#import + +/* + * Extensions to NSColor for interoperability with CGColor. + */ +@interface NSColor (RBLAdditions) + +/* + * The CGColor corresponding to the receiver. + */ +@property (nonatomic, readonly) CGColorRef rbl_CGColor; + +/* + * Returns an NSColor corresponding to the given CGColor. + * + * This currently does not handle pattern colors. + */ ++ (NSColor *)rbl_colorWithCGColor:(CGColorRef)color; +@end diff --git a/Rebel/NSColor+RBLAdditions.m b/Rebel/NSColor+RBLAdditions.m new file mode 100644 index 0000000..26ba771 --- /dev/null +++ b/Rebel/NSColor+RBLAdditions.m @@ -0,0 +1,91 @@ +// +// NSColor+RBLAdditions.m +// Rebel +// +// Created by Justin Spahr-Summers on 01.12.11. +// Copyright (c) 2012 GitHub. All rights reserved. +// +// Portions copyright (c) 2011 Bitswift. All rights reserved. +// See the LICENSE file for more information. +// + +#import "NSColor+RBLAdditions.h" + +static void drawCGImagePattern (void *info, CGContextRef context) { + CGImageRef image = info; + + size_t width = CGImageGetWidth(image); + size_t height = CGImageGetHeight(image); + + CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); +} + +static void releasePatternInfo (void *info) { + CFRelease(info); +} + +@implementation NSColor (RBLAdditions) ++ (NSColor *)rbl_colorWithCGColor:(CGColorRef)color { + if (color == nil) { + return nil; + } + + CGColorSpaceRef colorSpaceRef = CGColorGetColorSpace(color); + + NSColorSpace *colorSpace = [[NSColorSpace alloc] initWithCGColorSpace:colorSpaceRef]; + NSColor *result = [self colorWithColorSpace:colorSpace components:CGColorGetComponents(color) count:(size_t)CGColorGetNumberOfComponents(color)]; + [colorSpace release]; + + return result; +} + +- (CGColorRef)rbl_CGColor { + if ([self.colorSpaceName isEqualToString:NSPatternColorSpace]) { + CGImageRef patternImage = [self.patternImage CGImageForProposedRect:NULL context:nil hints:nil]; + if (patternImage == NULL) { + return NULL; + } + + size_t width = CGImageGetWidth(patternImage); + size_t height = CGImageGetHeight(patternImage); + + CGRect patternBounds = CGRectMake(0, 0, width, height); + CGPatternRef pattern = CGPatternCreate( + // Released in releasePatternInfo(). + (void *)CFRetain(patternImage), + patternBounds, + CGAffineTransformIdentity, + width, + height, + kCGPatternTilingConstantSpacingMinimalDistortion, + YES, + &(CGPatternCallbacks){ + .version = 0, + .drawPattern = &drawCGImagePattern, + .releaseInfo = &releasePatternInfo + } + ); + + CGColorSpaceRef colorSpaceRef = CGColorSpaceCreatePattern(NULL); + + CGColorRef result = CGColorCreateWithPattern(colorSpaceRef, pattern, (CGFloat[]){ 1.0 }); + + CGColorSpaceRelease(colorSpaceRef); + CGPatternRelease(pattern); + + return (CGColorRef)[(id)result autorelease]; + } + + NSColorSpace *colorSpace = [NSColorSpace genericRGBColorSpace]; + NSColor *color = [self colorUsingColorSpace:colorSpace]; + + CGFloat components[color.numberOfComponents]; + [color getComponents:components]; + + CGColorSpaceRef colorSpaceRef = colorSpace.CGColorSpace; + CGColorRef result = CGColorCreate(colorSpaceRef, components); + + return (CGColorRef)[(id)result autorelease]; +} + +@end diff --git a/Rebel/Rebel.h b/Rebel/Rebel.h index bf41513..19430b8 100644 --- a/Rebel/Rebel.h +++ b/Rebel/Rebel.h @@ -6,4 +6,5 @@ // Copyright (c) 2012 GitHub. All rights reserved. // +#import #import