-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorFactory.m
More file actions
141 lines (111 loc) · 4.64 KB
/
Copy pathColorFactory.m
File metadata and controls
141 lines (111 loc) · 4.64 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// ColorFactory.m
//
// Created by Dan Reed on 3/31/12.
// Copyright (c) 2012 danreed.net. All rights reserved.
//
/*
* ----------------------------------------------------------------------------
* "BEER-WARE LICENSE":
* <dan@danreed.net> wrote this class. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day (and you think
* this stuff is worth it) you can buy me a beer in return --Dan Reed
* ----------------------------------------------------------------------------
*/
#import "ColorFactory.h"
@implementation ColorFactory
@synthesize primary;
@synthesize secondary;
@synthesize primaryShadow;
@synthesize secondaryShadow;
@synthesize primaryBold;
@synthesize secondaryBold;
@synthesize primaryLight;
@synthesize secondaryLight;
@synthesize primaryHighlight;
@synthesize secondaryHighlight;
+(ColorFactory *)sharedFactory {
static dispatch_once_t pred;
static ColorFactory *shared = nil;
dispatch_once(&pred, ^{
shared = [[ColorFactory alloc] init];
});
return shared;
}
-(ColorFactory *)init{
self = [super init];
if(self){
//init global colors here
primary = [UIColor colorFromHex:PRIMARY_COLOR];
secondary = [UIColor colorFromHex:SECONDARY_COLOR];
primaryShadow = [UIColor colorFromHex:PRIMARY_SHADOW_COLOR];
secondaryShadow = [UIColor colorFromHex:SECONDARY_SHADOW_COLOR];
primaryBold = [UIColor colorFromHex:PRIMARY_BOLD_COLOR];
secondaryBold = [UIColor colorFromHex:SECONDARY_BOLD_COLOR];
primaryLight = [UIColor colorFromHex:PRIMARY_LIGHT_COLOR];
secondaryLight = [UIColor colorFromHex:SECONDARY_LIGHT_COLOR];
primaryHighlight = [UIColor colorFromHex:PRIMARY_HIGHLIGHT_COLOR];
secondaryHighlight = [UIColor colorFromHex:SECONDARY_HIGHLIGHT_COLOR];
}
return self;
}
@end
@implementation UIColor (ColorFromHex)
+ (UIColor *) colorFromHex:(NSString *)hex {
NSString *colorString = [[hex uppercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;
if ([colorString length] < 6)
return [UIColor grayColor];
if ([colorString hasPrefix:@"0X"])
colorString = [colorString substringFromIndex:2];
else if ([colorString hasPrefix:@"#"])
colorString = [colorString substringFromIndex:1];
else if ([colorString length] != 6)
return [UIColor grayColor];
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [colorString substringWithRange:range];
range.location += 2;
NSString *gString = [colorString substringWithRange:range];
range.location += 2;
NSString *bString = [colorString substringWithRange:range];
unsigned int red, green, blue;
[[NSScanner scannerWithString:rString] scanHexInt:&red];
[[NSScanner scannerWithString:gString] scanHexInt:&green];
[[NSScanner scannerWithString:bString] scanHexInt:&blue];
return [UIColor colorWithRed:((float) red / 255.0f)
green:((float) green / 255.0f)
blue:((float) blue / 255.0f)
alpha:1.0f];
}
+ (UIColor *) colorFromHex:(NSString *)hex withAlpha:(float)alphaValue{
float theAlpha = alphaValue;
if(theAlpha > 1.0f)
theAlpha = 1.0f;
NSString *colorString = [[hex uppercaseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;
if ([colorString length] < 6)
return [UIColor grayColor];
if ([colorString hasPrefix:@"0X"])
colorString = [colorString substringFromIndex:2];
else if ([colorString hasPrefix:@"#"])
colorString = [colorString substringFromIndex:1];
else if ([colorString length] != 6)
return [UIColor grayColor];
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [colorString substringWithRange:range];
range.location += 2;
NSString *gString = [colorString substringWithRange:range];
range.location += 2;
NSString *bString = [colorString substringWithRange:range];
unsigned int red, green, blue;
[[NSScanner scannerWithString:rString] scanHexInt:&red];
[[NSScanner scannerWithString:gString] scanHexInt:&green];
[[NSScanner scannerWithString:bString] scanHexInt:&blue];
return [UIColor colorWithRed:((float) red / 255.0f)
green:((float) green / 255.0f)
blue:((float) blue / 255.0f)
alpha:theAlpha];
}
@end