Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A UIImage category that will replace or remove colors. This allows multiple colors to be changed on a single image, until it has alpha values.
//
// UIImage+Additions.h
// Sparrow
//
// Created by Shilo White on 10/16/11.
// Copyright 2011 Shilocity Productions. All rights reserved.
//
#define COLOR_PART_RED(color) (((color) >> 16) & 0xff)
#define COLOR_PART_GREEN(color) (((color) >> 8) & 0xff)
#define COLOR_PART_BLUE(color) ( (color) & 0xff)
#import <Foundation/Foundation.h>
@interface UIImage (Additions)
- (UIImage *)imageByRemovingColor:(uint)color;
- (UIImage *)imageByRemovingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor;
- (UIImage *)imageByReplacingColor:(uint)color withColor:(uint)newColor;
- (UIImage *)imageByReplacingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor;
- (UIImage *)imageByReplacingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor andAlpha:(float)alpha;
@end
@implementation UIImage (Additions)
- (UIImage *)imageByRemovingColor:(uint)color {
return [self imageByRemovingColorsWithMinColor:color maxColor:color];
}
- (UIImage *)imageByRemovingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor {
return [self imageByReplacingColorsWithMinColor:minColor maxColor:maxColor withColor:0 andAlpha:0];
}
- (UIImage *)imageByReplacingColor:(uint)color withColor:(uint)newColor {
return [self imageByReplacingColorsWithMinColor:color maxColor:color withColor:newColor];
}
- (UIImage *)imageByReplacingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor {
return [self imageByReplacingColorsWithMinColor:minColor maxColor:maxColor withColor:newColor andAlpha:1.0f];
}
- (UIImage *)imageByReplacingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor andAlpha:(float)alpha {
CGImageRef imageRef = self.CGImage;
float width = CGImageGetWidth(imageRef);
float height = CGImageGetHeight(imageRef);
CGRect bounds = CGRectMake(0, 0, width, height);
uint minRed = COLOR_PART_RED(minColor);
uint minGreen = COLOR_PART_GREEN(minColor);
uint minBlue = COLOR_PART_BLUE(minColor);
uint maxRed = COLOR_PART_RED(maxColor);
uint maxGreen = COLOR_PART_GREEN(maxColor);
uint maxBlue = COLOR_PART_BLUE(maxColor);
float newRed = COLOR_PART_RED(newColor)/255.0f;
float newGreen = COLOR_PART_GREEN(newColor)/255.0f;
float newBlue = COLOR_PART_BLUE(newColor)/255.0f;
CGContextRef context = nil;
if (alpha) {
context = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef));
CGContextSetRGBFillColor(context, newRed, newGreen, newBlue, alpha);
CGContextFillRect(context, bounds);
}
float maskingColors[6] = {minRed, maxRed, minGreen, maxGreen, minBlue, maxBlue};
CGImageRef maskedImageRef = CGImageCreateWithMaskingColors(imageRef, maskingColors);
if (!maskedImageRef) return nil;
if (alpha) CGContextDrawImage(context, bounds, maskedImageRef);
CGImageRef newImageRef = (alpha)?CGBitmapContextCreateImage(context):maskedImageRef;
if (context) CGContextRelease(context);
if (newImageRef != maskedImageRef) CGImageRelease(maskedImageRef);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
return newImage;
}
@end
@danielphillips

This comment has been minimized.

Copy link

danielphillips commented Jul 25, 2012

HI there, great category, I'm getting a context error however when using - (UIImage *)imageByReplacingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor withColor:(uint)newColor; could you give me an example snippet of usage code, I fear I'm not doing something correctly.

I have a grey image with some transparent, and I want to catch all the grey areas (code 239) and replace to white (255), to be sure to capture all the grey I used value 230:

`UIImage *waveNew = [wave imageByReplacingColorsWithMinColor:230 maxColor:254 withColor:255];`

But I had this error:

<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 7200 bytes/row.

@reflectivist

This comment has been minimized.

Copy link

reflectivist commented Feb 11, 2013

I haven't gotten the example to work yet... I've tweaked it to take an NSImage, produce an NSImage, replace all opaque areas of the image with a color, but haven't debugged it yet... I read somewhere (and it seams to work to create the context) that replacing the "CGImageGetColorSpace()' call with the constant 'kCGImageAlphaNoneSkipLast' resolved it.
The original poster of that tip didn't know why it works, anyway, it does make the error go away.

@rahulvyas

This comment has been minimized.

Copy link

rahulvyas commented Oct 1, 2016

I am always getting nil in the maskedImageRef
CGImageRef maskedImageRef = CGImageCreateWithMaskingColors(imageRef, maskingColors);

Could you modify the code please also is it possible to pass UIColor object instead of uint value ?
I am trying to change grey pixel to blue in one image but no luck till now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.