-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUIColor+MPColors.m
213 lines (149 loc) · 7.2 KB
/
UIColor+MPColors.m
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// UIColor+MPColors.m
// MPColors
//
// Created by Mike Pesate on 2/8/15.
// Copyright (c) 2015 Mike Pesate.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
#import "UIColor+MPColors.h"
#pragma mark - Static Constants
static float kMP_Base255 = 255.f;
static const int32_t kMP_HexRGB_R = 0xFF0000;
static const int32_t kMP_HexRGB_G = 0xFF00;
static const int32_t kMP_HexRGB_B = 0xFF;
static const uInt kMP_HexShiftRGB_R = 16;
static const uInt kMP_HexShiftRGB_G = 8;
static const uInt kMP_HexShiftRGB_B = 0;
static const int32_t kMP_HexRGBA_R = 0xFF000000;
static const int32_t kMP_HexRGBA_G = 0xFF0000;
static const int32_t kMP_HexRGBA_B = 0xFF00;
static const int32_t kMP_HexRGBA_A = 0xFF;
static const uInt kMP_HexShiftRGBA_R = 24;
static const uInt kMP_HexShiftRGBA_G = 16;
static const uInt kMP_HexShiftRGBA_B = 8;
static const uInt kMP_HexShiftRGBA_A = 0;
@implementation UIColor (MPColors)
#pragma mark - Shorthand Methods
+(UIColor *)mp_shorthandColorWithRGBArray:(NSArray*)rgb{
if (rgb.count != 3) {
return nil;
}
float red = [rgb[0] floatValue];
float green = [rgb[1] floatValue];
float blue = [rgb[2] floatValue];
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
+(UIColor *)mp_shorthandColorWithRGBAArray:(NSArray*)rgba{
if (rgba.count != 4) {
return nil;
}
float red = [rgba[0] floatValue];
float green = [rgba[1] floatValue];
float blue = [rgba[2] floatValue];
float alpha = [rgba[3] floatValue];
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
#pragma mark - Base255 Support Methods
+(UIColor*)mp_colorWithRed255:(uInt)red withGreen255:(uInt)green withBlue255:(uInt)blue{
return [self mp_colorWithRed255:red withGreen255:green withBlue255:blue withAlpha:1.0];
}
+(UIColor*)mp_colorWithRed255:(uInt)red withGreen255:(uInt)green withBlue255:(uInt)blue withAlpha:(CGFloat)alpha{
if (red > 255 || blue >255 || blue > 255 || alpha > 1.f) {
return nil;
}
float fRed = (float)red/kMP_Base255;
float fGreen = (float)green/kMP_Base255;
float fBlue = (float)blue/kMP_Base255;
UIColor* color = [UIColor colorWithRed:fRed green:fGreen blue:fBlue alpha:alpha];
return color;
}
+(UIColor*)mp_colorWithRed255:(uInt)red withGreen255:(uInt)green withBlue255:(uInt)blue withAlpha100:(uInt)alpha{
float fAlpha = (float)alpha/100.f;
return [self mp_colorWithRed255:red withGreen255:green withBlue255:blue withAlpha:fAlpha];
}
+(UIColor*)mp_colorWithRed255WithGreen255WithBlue255FromRBGAString:(NSString*)rgbString{
NSArray* colors = [rgbString componentsSeparatedByString:@","];
if (colors.count < 3) {
return nil;
}
uInt red = (uInt)[(NSString*)[colors objectAtIndex:0] intValue];
uInt green = (uInt)[(NSString*)[colors objectAtIndex:1] intValue];
uInt blue = (uInt)[(NSString*)[colors objectAtIndex:2] intValue];
return [self mp_colorWithRed255:red withGreen255:green withBlue255:blue];
}
+(UIColor*)mp_colorWithRed255WithGreen255WithBlue255WithFloatAlphaFromRBGAString:(NSString*)rgbaString{
NSArray* colors = [rgbaString componentsSeparatedByString:@","];
if (colors.count < 4) {
return nil;
}
uInt red = (uInt)[(NSString*)[colors objectAtIndex:0] intValue];
uInt green = (uInt)[(NSString*)[colors objectAtIndex:1] intValue];
uInt blue = (uInt)[(NSString*)[colors objectAtIndex:2] intValue];
float alpha = (float)[(NSString*)[colors objectAtIndex:3] floatValue];
return [self mp_colorWithRed255:red withGreen255:green withBlue255:blue withAlpha:alpha];
}
+(UIColor*)mp_colorWithRed255WithGreen255WithBlue255WithAlpha100FromRBGAString:(NSString*)rgbaString{
NSArray* colors = [rgbaString componentsSeparatedByString:@","];
if (colors.count < 4) {
return nil;
}
uInt red = (uInt)[(NSString*)[colors objectAtIndex:0] intValue];
uInt green = (uInt)[(NSString*)[colors objectAtIndex:1] intValue];
uInt blue = (uInt)[(NSString*)[colors objectAtIndex:2] intValue];
uInt alpha = (uInt)[(NSString*)[colors objectAtIndex:3] intValue];
return [self mp_colorWithRed255:red withGreen255:green withBlue255:blue withAlpha100:alpha];
}
#pragma mark - HexRGB and HexRGBA Support Methods
+(UIColor *)mp_colorFromHexRBG:(int32_t)rgbHex{
float red = (float)((rgbHex & kMP_HexRGB_R) >> kMP_HexShiftRGB_R) / kMP_Base255;
float green = (float)((rgbHex & kMP_HexRGB_G) >> kMP_HexShiftRGB_G) / kMP_Base255;
float blue = (float)((rgbHex & kMP_HexRGB_B) >> kMP_HexShiftRGB_B) / kMP_Base255;
float alpha = 1.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
+(UIColor *)mp_colorFromHexRBGA:(int32_t)rgbaHex{
float red = (float)((rgbaHex & kMP_HexRGBA_R) >> kMP_HexShiftRGBA_R) / kMP_Base255;
float green = (float)((rgbaHex & kMP_HexRGBA_G) >> kMP_HexShiftRGBA_G) / kMP_Base255;
float blue = (float)((rgbaHex & kMP_HexRGBA_B) >> kMP_HexShiftRGBA_B) / kMP_Base255;
float alpha = (float)((rgbaHex & kMP_HexRGBA_A) >> kMP_HexShiftRGBA_A) / kMP_Base255;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
+(UIColor *)mp_colorFromHexRBGString:(NSString *)rgbHexString{
uint32_t hexRGB = 0;
rgbHexString =[rgbHexString stringByReplacingOccurrencesOfString:@"0x" withString:@""];
NSScanner* scanner = [[NSScanner alloc] initWithString:rgbHexString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
BOOL scanned = [scanner scanHexInt:&hexRGB];
if (!scanned) {
return nil;
}
return [self mp_colorFromHexRBG:hexRGB];
}
+(UIColor *)mp_colorFromHexRBGAString:(NSString *)rgbaHexString{
uint32_t hexRGBA = 0;
rgbaHexString =[rgbaHexString stringByReplacingOccurrencesOfString:@"0x" withString:@""];
NSScanner* scanner = [[NSScanner alloc] initWithString:rgbaHexString];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
BOOL scanned = [scanner scanHexInt:&hexRGBA];
if (!scanned) {
return nil;
}
return [self mp_colorFromHexRBGA:hexRGBA];
}
@end