/
Webcolor.m
244 lines (207 loc) Β· 7.14 KB
/
Webcolor.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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "Webcolor.h"
#import "TiBase.h"
#import "TiUtils.h"
NSString * const IOS_COLOR_SCROLLVIEW_TEXTURED_BACKGROUND = @"scrollview_textured";
NSString * const IOS_COLOR_VIEW_FLIPSIDE_BACKGROUND = @"view_flipside";
NSString * const IOS_COLOR_GROUP_TABLEVIEW_BACKGROUND = @"group_tableview";
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
NSString * const IOS_COLOR_UNDER_PAGE_BACKGROUND = @"under_page";
#endif
UIColor * checkmarkColor = nil;
NSMutableDictionary * colorLookup = nil;
BOOL isASCIIHexDigit(unichar c) { return (c >= '0' && c <= '9') || ((c | 0x20) >= 'a' && (c | 0x20) <= 'f'); }
int toASCIIHexValue(unichar c) {return (c & 0xF) + (c < 'A' ? 0 : 9); }
@implementation Webcolor
+(UIColor*)checkmarkColor
{
if(checkmarkColor==nil)
{
checkmarkColor = RGBACOLOR(55.0,79.0,130.0,1);
}
return checkmarkColor;
}
+(UIColor*)webColorNamed:(NSString*)colorName
{
if (![colorName isKindOfClass:[NSString class]])
{
return nil;
}
colorName = [colorName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (colorLookup == nil)
{
UIColor * white = [UIColor whiteColor];
UIColor * black = [UIColor blackColor];
colorLookup = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
black,@"black",
[UIColor grayColor],@"gray",
[UIColor darkGrayColor],@"darkgray",
[UIColor lightGrayColor],@"lightgray",
white,@"white",
[UIColor redColor],@"red",
[UIColor greenColor],@"green",
[UIColor blueColor],@"blue",
[UIColor cyanColor],@"cyan",
[UIColor yellowColor],@"yellow",
[UIColor magentaColor],@"magenta",
[UIColor orangeColor],@"orange",
[UIColor purpleColor],@"purple",
[UIColor brownColor],@"brown",
[UIColor clearColor],@"transparent",
[UIColor groupTableViewBackgroundColor],@"stripped",
[UIColor groupTableViewBackgroundColor],IOS_COLOR_GROUP_TABLEVIEW_BACKGROUND,
[UIColor scrollViewTexturedBackgroundColor],IOS_COLOR_SCROLLVIEW_TEXTURED_BACKGROUND,
[UIColor viewFlipsideBackgroundColor],IOS_COLOR_VIEW_FLIPSIDE_BACKGROUND,
// these are also defined by the W3C HTML spec so we support them
[Webcolor colorForHex:@"0ff"],@"aqua",
[Webcolor colorForHex:@"f0f"],@"fuchsia",
[Webcolor colorForHex:@"0f0"],@"lime",
[Webcolor colorForHex:@"800"],@"maroon",
[Webcolor colorForHex:@"FFC0CB"],@"pink",
[Webcolor colorForHex:@"000080"],@"navy",
[Webcolor colorForHex:@"c0c0c0"],@"silver",
[Webcolor colorForHex:@"808000"],@"olive",
[Webcolor colorForHex:@"008080"],@"teal",
white,@"fff",
white,@"ffff",
white,@"ffffff",
white,@"ffffffff",
black,@"000",
black,@"f000",
black,@"000000",
black,@"ff000000",
nil];
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
if ([TiUtils isIOS5OrGreater])
{
[colorLookup setObject:[UIColor underPageBackgroundColor] forKey:IOS_COLOR_UNDER_PAGE_BACKGROUND];
}
#endif
if ([colorName hasPrefix:@"#"])
{
colorName = [colorName substringFromIndex:1];
}
colorName = [colorName lowercaseString];
UIColor * result = [colorLookup objectForKey:colorName];
if (result != nil)
{
return result;
}
result = [Webcolor colorForHex:colorName];
if (result == nil)
{
result = [Webcolor colorForRGBFunction:colorName];
}
if (result != nil)
{
[colorLookup setObject:result forKey:colorName];
}
return result;
}
+(UIColor*)colorForRGBFunction:(NSString*)functionString
{
int stringLength=[functionString length];
NSRange openParensRange = [functionString rangeOfString:@"("];
if (openParensRange.location == NSNotFound)
{
return nil;
}
//Last char must be terminating ).
if ([functionString characterAtIndex:stringLength-1] != ')')
{
return nil;
}
NSRange searchRange;
NSRange nextTokenRange;
int segmentLength;
searchRange.location = openParensRange.location + 1; //Skipping starting (
searchRange.length = stringLength - searchRange.location - 1; //-1 for terminating ).
nextTokenRange = [functionString rangeOfString:@"," options:NSLiteralSearch range:searchRange];
if (nextTokenRange.location == NSNotFound)
{
return nil;
}
segmentLength = nextTokenRange.location - searchRange.location; //This does NOT include a comma.
float firstArg = [[functionString substringWithRange:NSMakeRange(searchRange.location, segmentLength)] floatValue];
searchRange.location += segmentLength + 1; searchRange.length -= segmentLength + 1;
nextTokenRange = [functionString rangeOfString:@"," options:NSLiteralSearch range:searchRange];
if (nextTokenRange.location == NSNotFound)
{
return nil;
}
segmentLength = nextTokenRange.location - searchRange.location; //This does NOT include a comma.
float secondArg = [[functionString substringWithRange:NSMakeRange(searchRange.location, segmentLength)] floatValue];
searchRange.location += segmentLength + 1; searchRange.length -= segmentLength + 1;
nextTokenRange = [functionString rangeOfString:@"," options:NSLiteralSearch range:searchRange];
float thirdArg, fourthArg = 1.0;
if (nextTokenRange.location == NSNotFound)
{
thirdArg = [[functionString substringWithRange:searchRange] floatValue];
}
else
{
segmentLength = nextTokenRange.location - searchRange.location;
thirdArg = [[functionString substringWithRange:NSMakeRange(searchRange.location, segmentLength)] floatValue];
fourthArg = [[functionString substringWithRange:NSMakeRange(nextTokenRange.location+1,searchRange.length - segmentLength - 1)] floatValue];
}
return RGBACOLOR(firstArg,secondArg,thirdArg,fourthArg);
}
+(UIColor*)colorForHex:(NSString*)hexCode
{
unsigned length = [hexCode length];
float alpha = 1.0;
if ((length != 3) && (length != 4) && (length != 6) && (length!=7) && (length != 8))
{
DebugLog(@"[WARN] Hex color passed looks invalid: %@",hexCode);
return nil;
}
unsigned value = 0;
for (size_t i = 0; i < length; ++i)
{
unichar thisChar = [hexCode characterAtIndex:i];
if (thisChar=='#') continue;
if (!isASCIIHexDigit(thisChar))
{
return nil;
}
value <<= 4;
value |= toASCIIHexValue(thisChar);
}
if (length < 6)
{
value = ((value & 0xF000) << 16) |
((value & 0xFF00) << 12) |
((value & 0xFF0) << 8) |
((value & 0xFF) << 4) |
(value & 0xF);
}
if((length % 4)==0)
{
alpha = ((value >> 24) & 0xFF) / 255.0;
}
int red = (value >> 16) & 0xFF;
int green = (value >> 8) & 0xFF;
int blue = value & 0xFF;
return RGBACOLOR(red,green,blue,alpha);
}
+(void)flushCache
{
RELEASE_TO_NIL(colorLookup);
RELEASE_TO_NIL(checkmarkColor);
}
+(BOOL)isDarkColor:(UIColor*)color
{
const CGFloat * components = CGColorGetComponents([color CGColor]);
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];
CGFloat formula = (red*299) + (green*587) + (blue*114) / 1000;
return formula < 125;
}
@end