-
Notifications
You must be signed in to change notification settings - Fork 3
/
UIColor+Dejal.m
199 lines (151 loc) · 6.19 KB
/
UIColor+Dejal.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
//
// UIColor+Dejal.m
// Dejal Open Source Categories
//
// Created by David Sinclair on 2008-12-01.
// Copyright (c) 2008-2015 Dejal Systems, LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "UIColor+Dejal.h"
@implementation UIColor (Dejal)
/**
Returns the standard light blue color used for non-editable prompts in fields and table cells.
@author DJS 2009-03.
@version DJS 2011-11: changed to tweak the shade.
*/
+ (instancetype)dejal_promptTextColor;
{
return [self colorWithRed:0.322 green:0.400 blue:0.569 alpha:1.000];
}
/**
Returns the standard blue color used for text on rows with a checkmark accessory.
@author DJS 2012-01.
*/
+ (instancetype)dejal_checkmarkTextColor;
{
return [UIColor colorWithRed:0.220 green:0.329 blue:0.529 alpha:1.000];
}
/**
Returns the standard blue color used for table cell selections.
@author DJS 2009-04.
*/
+ (instancetype)dejal_tableSelectionColor;
{
return [self colorWithRed:0.0 green:0.45 blue:0.93 alpha:1.0];
}
/**
Returns my favorite dark green color.
@author DJS 2012-05.
*/
+ (instancetype)dejal_darkGreenColor;
{
return [self colorWithRed:0.106 green:0.686 blue:0.204 alpha:1.0];
}
/**
Returns a color using an image with the specified name, including a platform component. For example, pass @"Foo" and "png" to use an image named "Foo-iPad.png" on an iPad, or "Foo-iPhone.png" on an iPhone or iPod touch.
@author DJS 2011-11.
*/
+ (instancetype)dejal_colorWithPlatformSpecificImageNamed:(NSString *)name extension:(NSString *)extension;
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
name = [name stringByAppendingFormat:@"-iPad.%@", extension];
else
name = [name stringByAppendingFormat:@"-iPhone.%@", extension];
return [UIColor colorWithPatternImage:[UIImage imageNamed:name]];
}
/**
Given an integer that encapsulates a BGR-format color (as used on Windows), returns the corresponding color.
@author DJS 2012-07.
*/
+ (instancetype)dejal_colorWithBGRColor:(NSInteger)bgrColor;
{
// BGR format: 0xFFFFFF(16777215) = white, 0 = black, (255,128,0) 0xFF8000 = blue:
CGFloat red = bgrColor & 0xFF;
CGFloat green = (bgrColor >> 8) & 0xFF;
CGFloat blue = (bgrColor >> 16) & 0xFF;
return [self colorWithRed:red / 255 green:green / 255 blue:blue / 255 alpha:1.0];
}
/**
Given an integer that encapsuates a RGB-format color (as used on the web), returns the corresponding color.
@param hexColor An integer representation of a color, typically from a hex number or string. See the following methods.
@param alpha The alpha value to use for the color, e.g. 1.0 for opaque.
@returns A new color instance.
@author DJS 2014-02.
*/
+ (UIColor *)dejal_colorWithHex:(NSUInteger)hexColor alpha:(CGFloat)alpha;
{
CGFloat red = (hexColor >> 16) & 0xFF;
CGFloat green = (hexColor >> 8) & 0xFF;
CGFloat blue = hexColor & 0xFF;
return [self colorWithRed:red / 255 green:green / 255 blue:blue / 255 alpha:alpha];
}
/**
Given a hex string representing a color, optionally with a "0x" or "#" prefix, returns the corresponding color.
@param hexStr A hex string, e.g. "123ABC", "#123ABC", or "0x123ABC".
@param alpha The alpha value to use for the color, e.g. 1.0 for opaque.
@returns A new color instance.
@author DJS 2014-02.
*/
+ (UIColor *)dejal_colorWithHexString:(NSString *)hexStr alpha:(CGFloat)alpha;
{
return [self dejal_colorWithHex:[self dejal_hexColorWithHexString:hexStr] alpha:alpha];
}
/**
Given a hex string representing a color, optionally with a "0x" or "#" prefix, returns the corresponding integer value.
@param hexStr A hex string, e.g. "123ABC", "#123ABC", or "0x123ABC".
@returns The corresponding integer representation.
@author DJS 2014-02.
*/
+ (NSUInteger)dejal_hexColorWithHexString:(NSString *)hexStr;
{
NSUInteger hexColor = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexStr];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"#"]];
[scanner scanHexInt:(unsigned int *)&hexColor];
return hexColor;
}
/**
Returns a lighter variation of the receiver, or the same color if the hue etc couldn't be obtained.
@author DJS 2014-02.
*/
- (UIColor *)dejal_lighterColor;
{
CGFloat hue, saturation, brightness, alpha;
if ([self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha])
return [UIColor colorWithHue:hue saturation:saturation brightness:MIN(brightness * 1.25, 1.0) alpha:alpha];
else
return self;
}
/**
Returns a darker variation of the receiver, or the same color if the hue etc couldn't be obtained.
@author DJS 2014-02.
*/
- (UIColor *)dejal_darkerColor;
{
CGFloat hue, saturation, brightness, alpha;
if ([self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha])
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness * 0.75 alpha:alpha];
else
return self;
}
@end