Created
October 17, 2011 07:43
-
-
Save Shilo/1292151 to your computer and use it in GitHub Desktop.
A SPTexture category for Sparrow that will replace or remove colors.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// SPTexture+Additions.h | |
// Sparrow | |
// | |
// Created by Shilo White on 10/15/11. | |
// Copyright 2011 Shilocity Productions. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import "SPTexture.h" | |
#import "SPStage.h" | |
#import "SPUtils.h" | |
#import "SPMacros.h" | |
@interface SPTexture (Additions) | |
- (id)initWithContentsOfFile:(NSString *)path removeColor:(uint)color; | |
- (id)initWithContentsOfFile:(NSString *)path removeColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor; | |
- (id)initWithContentsOfFile:(NSString *)path replaceColor:(uint)color withColor:(uint)newColor; | |
- (id)initWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor; | |
- (id)initWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor andAlpha:(float)alpha; | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path removeColor:(uint)color; | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path removeColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor; | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path replaceColor:(uint)color withColor:(uint)newColor; | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor; | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor andAlpha:(float)alpha; | |
@end | |
@implementation SPTexture (Additions) | |
- (id)initWithContentsOfFile:(NSString *)path removeColor:(uint)color { | |
return [self initWithContentsOfFile:path removeColorsWithMinColor:color maxColor:color]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path removeColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor { | |
return [self initWithContentsOfFile:path replaceColorsWithMinColor:minColor maxColor:maxColor withColor:0 andAlpha:0]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path replaceColor:(uint)color withColor:(uint)newColor { | |
return [self initWithContentsOfFile:path replaceColorsWithMinColor:color maxColor:color withColor:newColor]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor { | |
return [self initWithContentsOfFile:path replaceColorsWithMinColor:minColor maxColor:maxColor withColor:newColor andAlpha:1.0f]; | |
} | |
- (id)initWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor andAlpha:(float)alpha { | |
float contentScaleFactor = [SPStage contentScaleFactor]; | |
NSString *fullPath = [SPUtils absolutePathToFile:path withScaleFactor:contentScaleFactor]; | |
if (!fullPath) [NSException raise:SP_EXC_FILE_NOT_FOUND format:@"file '%@' not found", path]; | |
UIImage *image = [UIImage imageWithContentsOfFile:fullPath]; | |
float width = image.size.width; | |
float height = image.size.height; | |
uint minRed = SP_COLOR_PART_RED(minColor); | |
uint minGreen = SP_COLOR_PART_GREEN(minColor); | |
uint minBlue = SP_COLOR_PART_BLUE(minColor); | |
uint maxRed = SP_COLOR_PART_RED(maxColor); | |
uint maxGreen = SP_COLOR_PART_GREEN(maxColor); | |
uint maxBlue = SP_COLOR_PART_BLUE(maxColor); | |
float newRed = SP_COLOR_PART_RED(newColor)/255.0f; | |
float newGreen = SP_COLOR_PART_GREEN(newColor)/255.0f; | |
float newBlue = SP_COLOR_PART_BLUE(newColor)/255.0f; | |
return [[SPTexture alloc] initWithWidth:width height:height draw:^(CGContextRef context) { | |
CGRect bounds = CGRectMake(0, 0, width, height); | |
if (alpha) { | |
CGContextSetRGBFillColor(context, newRed, newGreen, newBlue, alpha); | |
CGContextFillRect(context, bounds); | |
} | |
float maskingColors[6] = {minRed, maxRed, minGreen, maxGreen, minBlue, maxBlue}; | |
CGImageRef maskedImage = CGImageCreateWithMaskingColors(image.CGImage, maskingColors); | |
CGContextTranslateCTM(context, 0, height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGContextDrawImage(context, bounds, maskedImage); | |
CGImageRelease(maskedImage); | |
}]; | |
} | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path removeColor:(uint)color { | |
return [[[self alloc] initWithContentsOfFile:path removeColor:color] autorelease]; | |
} | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path removeColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor { | |
return [[[self alloc] initWithContentsOfFile:path removeColorsWithMinColor:minColor maxColor:maxColor] autorelease]; | |
} | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path replaceColor:(uint)color withColor:(uint)newColor { | |
return [[[self alloc] initWithContentsOfFile:path replaceColor:color withColor:newColor] autorelease]; | |
} | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor { | |
return [[[self alloc] initWithContentsOfFile:path replaceColorsWithMinColor:minColor maxColor:maxColor withColor:newColor] autorelease]; | |
} | |
+ (SPTexture *)textureWithContentsOfFile:(NSString *)path replaceColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor andAlpha:(float)alpha { | |
return [[[self alloc] initWithContentsOfFile:path replaceColorsWithMinColor:minColor maxColor:maxColor withColor:newColor andAlpha:alpha] autorelease]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment