Skip to content

Instantly share code, notes, and snippets.

@cwagdev
Created August 2, 2013 09:11
Show Gist options
  • Save cwagdev/6138534 to your computer and use it in GitHub Desktop.
Save cwagdev/6138534 to your computer and use it in GitHub Desktop.
Convert a UIColor color to its grayscale equivalent
#import <UIKit/UIKit.h>
@interface UIColor (Grayscale)
- (UIColor *)grayscale;
@end
#import "UIColor+Grayscale.h"
@implementation UIColor (Grayscale)
- (UIColor *)grayscale {
CGFloat red = 0;
CGFloat blue = 0;
CGFloat green = 0;
CGFloat alpha = 0;
if ([self getRed:&red green:&green blue:&blue alpha:&alpha]) {
return [UIColor colorWithWhite:(0.299*red + 0.587*green + 0.114*blue) alpha:alpha];
} else {
return self;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment