This repository was archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathUIColor+PFColor.m
More file actions
executable file
·104 lines (86 loc) · 2.56 KB
/
UIColor+PFColor.m
File metadata and controls
executable file
·104 lines (86 loc) · 2.56 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
//
// NUColor.m
// Nucleus
//
// Created by Bailey Seymour on 3/18/14.
// Copyright (c) 2014 Bailey Seymour. All rights reserved.
//
#import "UIColor+PFColor.h"
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
UIColor *colorFromDefaultsWithKey(NSString *defaults, NSString *key, NSString *fallback);
UIColor *colorFromHex(NSString *hexString);
#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif
@implementation UIColor (PFColor)
+ (UIColor *)PF_colorWithHex:(NSString *)hexString {
return colorFromHex(hexString);
}
+ (NSString *)hexFromColor:(UIColor *)color {
return [color hexFromColor];
}
- (NSString *)hexFromColor {
const CGFloat *components = CGColorGetComponents(self.CGColor);
CGFloat r = components[0];
CGFloat g = components[1];
CGFloat b = components[2];
return [NSString stringWithFormat:@"#%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
}
#pragma mark Components
- (CGFloat)alpha {
CGFloat a;
[self getWhite:NULL alpha:&a];
return a;
}
- (CGFloat)red {
CGFloat r;
[self getRed:&r green:NULL blue:NULL alpha:NULL];
return r;
}
- (CGFloat)green {
CGFloat g;
[self getRed:NULL green:&g blue:NULL alpha:NULL];
return g;
}
- (CGFloat)blue {
CGFloat b;
[self getRed:NULL green:NULL blue:&b alpha:NULL];
return b;
}
- (CGFloat)hue {
CGFloat h;
[self getHue:&h saturation:NULL brightness:NULL alpha:NULL];
return h;
}
- (CGFloat)saturation {
CGFloat s;
[self getHue:NULL saturation:&s brightness:NULL alpha:NULL];
return s;
}
- (CGFloat)brightness {
CGFloat b;
[self getHue:NULL saturation:NULL brightness:&b alpha:NULL];
return b;
}
#pragma mark Manipulation
- (UIColor *)desaturate:(CGFloat)percent {
return [UIColor colorWithHue:[self hue]
saturation:[self saturation] * (1 - (percent / 100))
brightness:[self brightness]
alpha:[self alpha]];
}
- (UIColor *)lighten:(CGFloat)percent {
return [UIColor colorWithHue:[self hue]
saturation:[self saturation] * (1 - (percent / 100))
brightness:[self brightness] * (1 + (percent / 100))
alpha:[self alpha]];
}
- (UIColor *)darken:(CGFloat)percent {
return [UIColor colorWithHue:[self hue]
saturation:[self saturation] * (1 + (percent / 100))
brightness:[self brightness] * (1 - (percent / 100))
alpha:[self alpha]];
}
@end