Added NSColor extensions to convert to and from CGColor

This commit is contained in:
Justin Spahr-Summers 2012-07-29 16:22:24 -07:00
Родитель 759407a78f
Коммит 77e70975b7
4 изменённых файлов: 138 добавлений и 0 удалений

Просмотреть файл

@ -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 = "<group>"; };
D0723FBE15C5F9D8004DBDC7 /* RBLView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RBLView.h; sourceTree = "<group>"; };
D0723FBF15C5F9D8004DBDC7 /* RBLView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RBLView.m; sourceTree = "<group>"; };
D0723FC415C5FB68004DBDC7 /* NSColor+RBLAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSColor+RBLAdditions.h"; sourceTree = "<group>"; };
D0723FC515C5FB68004DBDC7 /* NSColor+RBLAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSColor+RBLAdditions.m"; sourceTree = "<group>"; };
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 = "<group>";
};
D0723FC315C5FB54004DBDC7 /* Extensions */ = {
isa = PBXGroup;
children = (
D0723FC415C5FB68004DBDC7 /* NSColor+RBLAdditions.h */,
D0723FC515C5FB68004DBDC7 /* NSColor+RBLAdditions.m */,
);
name = Extensions;
sourceTree = "<group>";
};
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;
};

Просмотреть файл

@ -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 <AppKit/AppKit.h>
/*
* 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

Просмотреть файл

@ -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

Просмотреть файл

@ -6,4 +6,5 @@
// Copyright (c) 2012 GitHub. All rights reserved.
//
#import <Rebel/NSColor+RBLAdditions.h>
#import <Rebel/RBLView.h>