-
Notifications
You must be signed in to change notification settings - Fork 21
/
UIColor+uiGradients.m.erb
59 lines (51 loc) · 2.6 KB
/
UIColor+uiGradients.m.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#import "UIColor+uiGradients.h"
@implementation UIColor (uiGradients)<% @colors.each do |color| %>
# pragma mark - <%= color["name"]%>
+ (UIColor *)uig_<%= color["camelized_name"]%>StartColor {
return [UIColor uig_colorWithHexString:@"<%= color["start_color_hex"]%>"];
}
+ (UIColor *)uig_<%= color["camelized_name"]%>EndColor {
return [UIColor uig_colorWithHexString:@"<%= color["end_color_hex"]%>"];
}<% end %>
# pragma mark - Helpers
+ (CGFloat)uig_colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger)length {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
+ (UIColor *)uig_colorWithHexString:(NSString *)hexString {
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
switch ([colorString length]) {
case 3: // #RGB
alpha = 1.0f;
red = [self uig_colorComponentFrom: colorString start: 0 length: 1];
green = [self uig_colorComponentFrom: colorString start: 1 length: 1];
blue = [self uig_colorComponentFrom: colorString start: 2 length: 1];
break;
case 4: // #ARGB
alpha = [self uig_colorComponentFrom: colorString start: 0 length: 1];
red = [self uig_colorComponentFrom: colorString start: 1 length: 1];
green = [self uig_colorComponentFrom: colorString start: 2 length: 1];
blue = [self uig_colorComponentFrom: colorString start: 3 length: 1];
break;
case 6: // #RRGGBB
alpha = 1.0f;
red = [self uig_colorComponentFrom: colorString start: 0 length: 2];
green = [self uig_colorComponentFrom: colorString start: 2 length: 2];
blue = [self uig_colorComponentFrom: colorString start: 4 length: 2];
break;
case 8: // #AARRGGBB
alpha = [self uig_colorComponentFrom: colorString start: 0 length: 2];
red = [self uig_colorComponentFrom: colorString start: 2 length: 2];
green = [self uig_colorComponentFrom: colorString start: 4 length: 2];
blue = [self uig_colorComponentFrom: colorString start: 6 length: 2];
break;
default:
return nil;
}
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
@end