forked from vilanovi/UIColor-Additions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIColor+Additions.m
346 lines (266 loc) · 11 KB
/
UIColor+Additions.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// UIColor+Additions.m
// Created by Joan Martin.
// Take a look to my repos at http://github.com/vilanovi
//
// Copyright (c) 2013 Joan Martin, [email protected].
//
// 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+Additions.h"
#define ADD_RED_MASK 0xFF0000
#define ADD_GREEN_MASK 0xFF00
#define ADD_BLUE_MASK 0xFF
#define ADD_ALPHA_MASK 0xFF000000
#define ADD_RED_SHIFT 16
#define ADD_GREEN_SHIFT 8
#define ADD_BLUE_SHIFT 0
#define ADD_ALPHA_SHIFT 24
#define ADD_COLOR_SIZE 255.0
#define ADD_LUM_RED 0.2989
#define ADD_LUM_GREEN 0.5870
#define ADD_LUM_BLUE 0.1140
@implementation UIColor (Additions)
+ (UIColor*)add_colorWithRGBHexValue:(ADDColorType)rgbValue
{
return [UIColor colorWithRed:((CGFloat)((rgbValue & ADD_RED_MASK) >> ADD_RED_SHIFT))/ADD_COLOR_SIZE
green:((CGFloat)((rgbValue & ADD_GREEN_MASK) >> ADD_GREEN_SHIFT))/ADD_COLOR_SIZE
blue:((CGFloat)((rgbValue & ADD_BLUE_MASK) >> ADD_BLUE_SHIFT))/ADD_COLOR_SIZE
alpha:1.0];
}
+ (UIColor*)add_colorWithRGBAHexValue:(ADDColorType)rgbaValue
{
return [UIColor colorWithRed:((CGFloat)((rgbaValue & ADD_RED_MASK) >> ADD_RED_SHIFT))/ADD_COLOR_SIZE
green:((CGFloat)((rgbaValue & ADD_GREEN_MASK) >> ADD_GREEN_SHIFT))/ADD_COLOR_SIZE
blue:((CGFloat)((rgbaValue & ADD_BLUE_MASK) >> ADD_BLUE_SHIFT))/ADD_COLOR_SIZE
alpha:((CGFloat)((rgbaValue & ADD_ALPHA_MASK) >> ADD_ALPHA_SHIFT))/ADD_COLOR_SIZE];
}
+ (UIColor*)add_colorWithRGBHexString:(NSString*)rgbStrValue
{
ADDColorType rgbHexValue;
NSScanner* scanner = [NSScanner scannerWithString:rgbStrValue];
BOOL successful = [scanner scanHexInt:&rgbHexValue];
if (!successful)
return nil;
return [self add_colorWithRGBHexValue:rgbHexValue];
}
+ (UIColor*)add_colorWithRGBAHexString:(NSString*)rgbaStrValue
{
ADDColorType rgbHexValue;
NSScanner *scanner = [NSScanner scannerWithString:rgbaStrValue];
BOOL successful = [scanner scanHexInt:&rgbHexValue];
if (!successful)
return nil;
return [self add_colorWithRGBAHexValue:rgbHexValue];
}
- (BOOL)add_getRGBHexValue:(ADDColorType*)rgbHex
{
size_t numComponents = CGColorGetNumberOfComponents(self.CGColor);
CGFloat const * components = CGColorGetComponents(self.CGColor);
if (numComponents == 4)
{
CGFloat rFloat = components[0]; // red
CGFloat gFloat = components[1]; // green
CGFloat bFloat = components[2]; // blue
ADDColorType r = (ADDColorType)roundf(rFloat*ADD_COLOR_SIZE);
ADDColorType g = (ADDColorType)roundf(gFloat*ADD_COLOR_SIZE);
ADDColorType b = (ADDColorType)roundf(bFloat*ADD_COLOR_SIZE);
*rgbHex = (r << ADD_RED_SHIFT) + (g << ADD_GREEN_SHIFT) + (b << ADD_BLUE_SHIFT);
return YES;
}
else if (numComponents == 2)
{
CGFloat gFloat = components[0]; // gray
ADDColorType g = (ADDColorType)roundf(gFloat*ADD_COLOR_SIZE);
*rgbHex = (g << ADD_RED_SHIFT) + (g << ADD_GREEN_SHIFT) + (g << ADD_BLUE_SHIFT);
return YES;
}
return NO;
}
- (BOOL)add_getRGBAHexValue:(ADDColorType*)rgbaHex;
{
size_t numComponents = CGColorGetNumberOfComponents(self.CGColor);
CGFloat const * components = CGColorGetComponents(self.CGColor);
if (numComponents == 4)
{
CGFloat rFloat = components[0]; // red
CGFloat gFloat = components[1]; // green
CGFloat bFloat = components[2]; // blue
CGFloat aFloat = components[3]; // alpha
ADDColorType r = (ADDColorType)roundf(rFloat*ADD_COLOR_SIZE);
ADDColorType g = (ADDColorType)roundf(gFloat*ADD_COLOR_SIZE);
ADDColorType b = (ADDColorType)roundf(bFloat*ADD_COLOR_SIZE);
ADDColorType a = (ADDColorType)roundf(aFloat*ADD_COLOR_SIZE);
*rgbaHex = (r << ADD_RED_SHIFT) + (g << ADD_GREEN_SHIFT) + (b << ADD_BLUE_SHIFT) + (a << ADD_ALPHA_SHIFT);
return YES;
}
else if (numComponents == 2)
{
CGFloat gFloat = components[0]; // gray
CGFloat aFloat = components[1]; // alpha
ADDColorType g = (ADDColorType)roundf(gFloat*ADD_COLOR_SIZE);
ADDColorType a = (ADDColorType)roundf(aFloat *ADD_COLOR_SIZE);
*rgbaHex = (g << ADD_RED_SHIFT) + (g << ADD_GREEN_SHIFT) + (g << ADD_BLUE_SHIFT) + (a << ADD_ALPHA_SHIFT);
return YES;
}
return NO;
}
- (ADDColorType)add_RGBHexValue
{
return 0;
}
- (ADDColorType)add_RGBAHexValue
{
return 0;
}
- (NSString*)add_RGBHexString
{
ADDColorType value = 0;
BOOL compatible = [self add_getRGBHexValue:&value];
if (!compatible)
return nil;
return [NSString stringWithFormat:@"%x", value];
}
- (NSString*)add_RGBAHexString
{
ADDColorType value = 0;
BOOL compatible = [self add_getRGBAHexValue:&value];
if (!compatible)
return nil;
return [NSString stringWithFormat:@"%x", value];
}
+ (UIColor*)add_colorWithRed255:(CGFloat)red green255:(CGFloat)green blue255:(CGFloat)blue
{
return [self add_colorWithRed255:red green255:green blue255:blue alpha255:ADD_COLOR_SIZE];
}
+ (UIColor*)add_colorWithRed255:(CGFloat)red green255:(CGFloat)green blue255:(CGFloat)blue alpha255:(CGFloat)alpha
{
return [UIColor colorWithRed:red/ADD_COLOR_SIZE green:green/ADD_COLOR_SIZE blue:blue/ADD_COLOR_SIZE alpha:alpha/ADD_COLOR_SIZE];
}
- (UIColor*)add_grayColor
{
size_t totalComponents = CGColorGetNumberOfComponents(self.CGColor);
BOOL isGreyscale = (totalComponents == 2) ? YES : NO;
CGFloat const * components = (CGFloat *)CGColorGetComponents(self.CGColor);
CGFloat luminiscence = 0.0;
CGFloat alpha = 1.0;
if (isGreyscale)
{
luminiscence = components[0];
alpha = components[1];
}
else
{
luminiscence = components[0]*ADD_LUM_RED + components[1]*ADD_LUM_GREEN + components[2]*ADD_LUM_BLUE;
alpha = components[3];
}
return [UIColor colorWithWhite:luminiscence alpha:alpha];
}
- (UIColor*)add_colorWithSaturation:(CGFloat)newSaturation
{
CGFloat hue,saturation,brightness,alpha;
[self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
return [UIColor colorWithHue:hue saturation:newSaturation brightness:brightness alpha:alpha];
}
- (UIColor*)add_colorWithBrightness:(CGFloat)newBrightness
{
CGFloat hue,saturation,brightness,alpha;
[self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
return [UIColor colorWithHue:hue saturation:saturation brightness:newBrightness alpha:alpha];
}
- (UIColor*)add_lighterColorWithValue:(CGFloat)value
{
size_t totalComponents = CGColorGetNumberOfComponents(self.CGColor);
BOOL isGreyscale = (totalComponents == 2) ? YES : NO;
CGFloat const * oldComponents = (CGFloat *)CGColorGetComponents(self.CGColor);
CGFloat newComponents[4];
CGFloat (^actionBlock)(CGFloat component) = ^CGFloat(CGFloat component) {
CGFloat offset = 1.0 - component;
CGFloat newComponent = component + offset*value;
// CGFloat newComponent = component + value > 1.0 ? 1.0 : component + value;
return newComponent;
};
if (isGreyscale)
{
newComponents[0] = actionBlock(oldComponents[0]);
newComponents[1] = actionBlock(oldComponents[0]);
newComponents[2] = actionBlock(oldComponents[0]);
newComponents[3] = actionBlock(oldComponents[1]);
}
else
{
newComponents[0] = actionBlock(oldComponents[0]);
newComponents[1] = actionBlock(oldComponents[1]);
newComponents[2] = actionBlock(oldComponents[2]);
newComponents[3] = actionBlock(oldComponents[3]);
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef newColor = CGColorCreate(colorSpace, newComponents);
CGColorSpaceRelease(colorSpace);
UIColor *retColor = [UIColor colorWithCGColor:newColor];
CGColorRelease(newColor);
return retColor;
}
- (UIColor*)add_darkerColorWithValue:(CGFloat)value
{
size_t totalComponents = CGColorGetNumberOfComponents(self.CGColor);
BOOL isGreyscale = (totalComponents == 2) ? YES : NO;
CGFloat const * oldComponents = (CGFloat *)CGColorGetComponents(self.CGColor);
CGFloat newComponents[4];
CGFloat (^actionBlock)(CGFloat component) = ^CGFloat(CGFloat component) {
CGFloat newComponent = component * (1.0 - value);
// CGFloat newComponent = component - value < 0.0 ? 0.0 : component - value;
return newComponent;
};
if (isGreyscale)
{
newComponents[0] = actionBlock(oldComponents[0]);
newComponents[1] = actionBlock(oldComponents[0]);
newComponents[2] = actionBlock(oldComponents[0]);
newComponents[3] = actionBlock(oldComponents[1]);
}
else
{
newComponents[0] = actionBlock(oldComponents[0]);
newComponents[1] = actionBlock(oldComponents[1]);
newComponents[2] = actionBlock(oldComponents[2]);
newComponents[3] = actionBlock(oldComponents[3]);
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef newColor = CGColorCreate(colorSpace, newComponents);
CGColorSpaceRelease(colorSpace);
UIColor *retColor = [UIColor colorWithCGColor:newColor];
CGColorRelease(newColor);
return retColor;
}
- (BOOL)add_isLightColor
{
size_t totalComponents = CGColorGetNumberOfComponents(self.CGColor);
BOOL isGreyscale = (totalComponents == 2) ? YES : NO;
CGFloat const * components = (CGFloat *)CGColorGetComponents(self.CGColor);
CGFloat sum;
if (isGreyscale)
sum = components[0];
else
sum = components[0]*ADD_LUM_RED + components[1]*ADD_LUM_GREEN + components[2]*ADD_LUM_BLUE;
return (sum > 0.8f);
}
- (BOOL)add_isDarkColor
{
return ![self add_isLightColor];
}
@end