-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIColor+Helpers.m
82 lines (63 loc) · 2.11 KB
/
UIColor+Helpers.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
//
// UIColor+Hex.m
// Blank
//
// Created by Aymeric Gallissot on 05/11/13.
// Copyright (c) 2013 Blank. All rights reserved.
//
#import "UIColor+Helpers.h"
@implementation UIColor (Helpers)
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert alpha:(float)alpha
{
NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
// String should be 6 or 8 characters
if ([cString length] < 6)
return [UIColor blackColor];
// strip 0X if it appears
if ([cString hasPrefix:@"0X"])
cString = [cString substringFromIndex:2];
// strip # if it appears
if ([cString hasPrefix:@"#"]) {
cString = [cString substringFromIndex:1];
}
if ([cString length] != 6)
return [UIColor blackColor];
// Separate into r, g, b substrings
NSRange range;
range.location = 0;
range.length = 2;
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:alpha];
}
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert
{
return [self colorWithHexString:stringToConvert alpha:1.0f];
}
- (UIColor *)darkerColorWithBrightness:(CGFloat)brightness
{
CGFloat h, s, b, a;
if ([self getHue:&h saturation:&s brightness:&b alpha:&a]) {
return [UIColor colorWithHue:h saturation:s brightness:b * brightness alpha:a];
}
return self;
}
- (UIColor *)lighterWithColorWithBrightness:(CGFloat)brightness
{
CGFloat r, g, b, a;
if ([self getRed:&r green:&g blue:&b alpha:&a]) {
return [UIColor colorWithRed:MIN(r + brightness, 1.0) green:MIN(g + brightness, 1.0) blue:MIN(b + brightness, 1.0) alpha:a];
}
return self;
}
@end