Created
August 2, 2013 09:11
-
-
Save cwagdev/6138534 to your computer and use it in GitHub Desktop.
Convert a UIColor color to its grayscale equivalent
This file contains hidden or 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
#import <UIKit/UIKit.h> | |
@interface UIColor (Grayscale) | |
- (UIColor *)grayscale; | |
@end |
This file contains hidden or 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
#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