forked from simoncoulton/uicolor-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIColor+Helper.m
105 lines (82 loc) · 2.58 KB
/
UIColor+Helper.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
//
// UIColor+UIColorEnhanced.m
// GymMotivator
//
// Created by Simon Coulton on 30/03/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "UIColor+Helper.h"
@implementation UIColor (Helper)
#pragma mark ColorSpaceModel
- (CGColorSpaceModel)colorSpaceModel
{
return CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
}
- (BOOL)canProvideRGBComponents
{
switch (self.colorSpaceModel) {
case kCGColorSpaceModelRGB:
case kCGColorSpaceModelMonochrome:
return YES;
default:
return NO;
}
}
#pragma mark Color based getters
- (CGFloat)red
{
NSAssert(self.canProvideRGBComponents, @"Must be an RGB color");
const CGFloat *c = CGColorGetComponents(self.CGColor);
return c[0];
}
- (CGFloat)green
{
NSAssert(self.canProvideRGBComponents, @"Must be an RGB color");
const CGFloat *c = CGColorGetComponents(self.CGColor);
return c[1];
}
- (CGFloat)blue
{
NSAssert(self.canProvideRGBComponents, @"Must be an RGB color");
const CGFloat *c = CGColorGetComponents(self.CGColor);
return c[2];
}
#pragma mark Conversion
- (NSString *)toHexString
{
NSAssert(self.canProvideRGBComponents, @"Must be an RGB color");
CGFloat r, g, b;
r = self.red;
g = self.green;
b = self.blue;
if (r < 0.0f) r = 0.0f;
if (g < 0.0f) g = 0.0f;
if (b < 0.0f) b = 0.0f;
if (r > 1.0f) r = 1.0f;
if (g > 1.0f) g = 1.0f;
if (b > 1.0f) b = 1.0f;
return [NSString stringWithFormat:@"%02X%02X%02X",
(int)(r * 255), (int)(g * 255), (int)(b * 255)];
}
#pragma mark Initialization
+ (UIColor *)colorWithRGBHex:(UInt32)hex alpha:(CGFloat)opacity
{
int r = (hex >> 16) & 0xFF;
int g = (hex >> 8) & 0xFF;
int b = (hex) & 0xFF;
return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:opacity];
}
+ (UIColor *)colorWithHexString:(NSString *)hexToConvert
{
return [UIColor colorWithHexString:hexToConvert alpha:1.0f];
}
+ (UIColor *)colorWithHexString:(NSString *)hexToConvert alpha:(CGFloat)opacity
{
NSString *colorString = [[[hexToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByTrimmingCharactersInSet:[NSCharacterSet symbolCharacterSet]] uppercaseString];
if ([colorString hasPrefix:@"0X"]) colorString = [colorString substringFromIndex:2];
NSScanner *scanner = [NSScanner scannerWithString:colorString];
unsigned hexNum;
if (![scanner scanHexInt:&hexNum]) return nil;
return [UIColor colorWithRGBHex:hexNum alpha:opacity];
}
@end