PageRenderTime 48ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/MGBox2-master/MGBox/Categories/UIColor+MGExpanded.m

https://gitlab.com/praveenvelanati/ios-demo
Objective C | 618 lines | 468 code | 113 blank | 37 comment | 63 complexity | 53a39e874f620064521d1dc8a32376e5 MD5 | raw file
  1. //
  2. // a fork of UIColor+MGExpanded-MGExpanded, originally by Erica Sadun
  3. // license is apparently BSD (original includes no stated license)
  4. //
  5. #import "UIColor+MGExpanded.h"
  6. #import "MGBase.h"
  7. #define EPSILON 0.001f
  8. #define MIN3(x,y,z) ((y) <= (z) ? ((x) <= (y) ? (x) : (y)) : ((x) <= (z) ? (x) : (z)))
  9. #define MAX3(x,y,z) ((y) >= (z) ? ((x) >= (y) ? (x) : (y)) : ((x) >= (z) ? (x) : (z)))
  10. #define EQUALS(x,y) (fabsf((x) - (y)) < (EPSILON))
  11. #define SQRT3 1.732050807568877f
  12. // Static cache of looked up color names. Used with +colorWithName:
  13. static NSMutableDictionary *colorNameCache = nil;
  14. #if SUPPORTS_UNDOCUMENTED_API
  15. // Undocumented methods of UIColor+MGExpanded
  16. @interface UIColor+MGExpanded (Undocumented)
  17. - (NSString *)styleString;
  18. @end
  19. #endif // SUPPORTS_UNDOCUMENTED_API
  20. @interface UIColor (Expanded_Support)
  21. + (UIColor *)searchForColorByName:(NSString *)cssColorName;
  22. @end
  23. #pragma mark -
  24. @implementation UIColor (MGExpanded)
  25. - (CGColorSpaceModel)colorSpaceModel {
  26. return CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
  27. }
  28. - (NSString *)colorSpaceString {
  29. switch (self.colorSpaceModel) {
  30. case kCGColorSpaceModelUnknown:
  31. return @"kCGColorSpaceModelUnknown";
  32. case kCGColorSpaceModelMonochrome:
  33. return @"kCGColorSpaceModelMonochrome";
  34. case kCGColorSpaceModelRGB:
  35. return @"kCGColorSpaceModelRGB";
  36. case kCGColorSpaceModelCMYK:
  37. return @"kCGColorSpaceModelCMYK";
  38. case kCGColorSpaceModelLab:
  39. return @"kCGColorSpaceModelLab";
  40. case kCGColorSpaceModelDeviceN:
  41. return @"kCGColorSpaceModelDeviceN";
  42. case kCGColorSpaceModelIndexed:
  43. return @"kCGColorSpaceModelIndexed";
  44. case kCGColorSpaceModelPattern:
  45. return @"kCGColorSpaceModelPattern";
  46. default:
  47. return @"Not a valid color space";
  48. }
  49. }
  50. - (BOOL)canProvideRGBComponents {
  51. switch (self.colorSpaceModel) {
  52. case kCGColorSpaceModelRGB:
  53. case kCGColorSpaceModelMonochrome:
  54. return YES;
  55. default:
  56. return NO;
  57. }
  58. }
  59. - (NSArray *)arrayFromRGBAComponents {
  60. NSAssert(self.canProvideRGBComponents, @"Must be an RGB color to use -arrayFromRGBAComponents");
  61. CGFloat r, g, b, a;
  62. if (![self red:&r green:&g blue:&b alpha:&a]) {
  63. return nil;
  64. }
  65. return @[@(r), @(g), @(b), @(a)];
  66. }
  67. - (BOOL)red:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue
  68. alpha:(CGFloat *)alpha {
  69. const CGFloat *components = CGColorGetComponents(self.CGColor);
  70. CGFloat r, g, b, a;
  71. switch (self.colorSpaceModel) {
  72. case kCGColorSpaceModelMonochrome:
  73. r = g = b = components[0];
  74. a = components[1];
  75. break;
  76. case kCGColorSpaceModelRGB:
  77. r = components[0];
  78. g = components[1];
  79. b = components[2];
  80. a = components[3];
  81. break;
  82. default: // We don't know how to handle this model
  83. return NO;
  84. }
  85. if (red) {
  86. *red = r;
  87. }
  88. if (green) {
  89. *green = g;
  90. }
  91. if (blue) {
  92. *blue = b;
  93. }
  94. if (alpha) {
  95. *alpha = a;
  96. }
  97. return YES;
  98. }
  99. - (CGFloat)red {
  100. NSAssert(self.canProvideRGBComponents, @"Must be an RGB color to use -red");
  101. const CGFloat *c = CGColorGetComponents(self.CGColor);
  102. return c[0];
  103. }
  104. - (CGFloat)green {
  105. NSAssert(self.canProvideRGBComponents, @"Must be an RGB color to use -green");
  106. const CGFloat *c = CGColorGetComponents(self.CGColor);
  107. if (self.colorSpaceModel == kCGColorSpaceModelMonochrome) {
  108. return c[0];
  109. }
  110. return c[1];
  111. }
  112. - (CGFloat)blue {
  113. NSAssert(self.canProvideRGBComponents, @"Must be an RGB color to use -blue");
  114. const CGFloat *c = CGColorGetComponents(self.CGColor);
  115. if (self.colorSpaceModel == kCGColorSpaceModelMonochrome) {
  116. return c[0];
  117. }
  118. return c[2];
  119. }
  120. - (CGFloat)white {
  121. NSAssert(self.colorSpaceModel
  122. == kCGColorSpaceModelMonochrome, @"Must be a Monochrome color to use -white");
  123. const CGFloat *c = CGColorGetComponents(self.CGColor);
  124. return c[0];
  125. }
  126. - (CGFloat)alpha {
  127. return CGColorGetAlpha(self.CGColor);
  128. }
  129. - (UInt32)rgbHex {
  130. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use rgbHex");
  131. CGFloat r, g, b, a;
  132. if (![self red:&r green:&g blue:&b alpha:&a]) {
  133. return 0;
  134. }
  135. r = CLAMP(self.red, 0, 1);
  136. g = CLAMP(self.green, 0, 1);
  137. b = CLAMP(self.blue, 0, 1);
  138. return (((int)roundf(r * 255)) << 16) | (((int)roundf(g * 255)) << 8)
  139. | (((int)roundf(b * 255)));
  140. }
  141. - (BOOL)hue:(CGFloat *)hue saturation:(CGFloat *)saturation
  142. brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha {
  143. if (hue) {
  144. *hue = self.hue;
  145. }
  146. if (saturation) {
  147. *saturation = self.saturation;
  148. }
  149. if (brightness) {
  150. *brightness = self.brightness;
  151. }
  152. if (alpha) {
  153. *alpha = self.alpha;
  154. }
  155. return YES;
  156. }
  157. - (CGFloat)hue {
  158. NSAssert (self.canProvideRGBComponents, @"Must be a RGB color to use -hue, -saturation, -brightness");
  159. const CGFloat *c = CGColorGetComponents(self.CGColor);
  160. if ([self colorSpaceModel] == kCGColorSpaceModelMonochrome) {
  161. return 0.0f;
  162. }
  163. CGFloat angle = atan2f(SQRT3 * (c[1] - c[2]), 2 * c[0] - c[1] - c[2]);
  164. if (angle < 0.0f) {
  165. angle += 2 * M_PI;
  166. }
  167. return (angle / (2 * M_PI));
  168. }
  169. - (CGFloat)saturation {
  170. NSAssert (self.canProvideRGBComponents, @"Must be a RGB color to use -hue, -saturation, -brightness");
  171. const CGFloat *c = CGColorGetComponents(self.CGColor);
  172. if (self.colorSpaceModel == kCGColorSpaceModelMonochrome) {
  173. return 0.0f;
  174. }
  175. float max = MAX3(c[0], c[1], c[2]);
  176. float min = MIN3(c[0], c[1], c[2]);
  177. if (EQUALS(max, 0.0f)) {
  178. return 0.0f;
  179. }
  180. return ((max - min) / max);
  181. }
  182. - (CGFloat)brightness {
  183. NSAssert (self.canProvideRGBComponents, @"Must be a RGB color to use -hue, -saturation, -brightness");
  184. const CGFloat *c = CGColorGetComponents(self.CGColor);
  185. if (self.colorSpaceModel == kCGColorSpaceModelMonochrome) {
  186. return c[0];
  187. }
  188. return MAX3(c[0], c[1], c[2]);
  189. }
  190. #pragma mark Arithmetic operations
  191. - (UIColor *)colorByLuminanceMapping {
  192. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  193. CGFloat r, g, b, a;
  194. if (![self red:&r green:&g blue:&b alpha:&a]) {
  195. return nil;
  196. }
  197. // http://en.wikipedia.org/wiki/Luma_(video)
  198. // Y = 0.2126 R + 0.7152 G + 0.0722 B
  199. return [UIColor colorWithWhite:r * 0.2126f + g * 0.7152f + b * 0.0722f alpha:a];
  200. }
  201. - (UIColor *)colorByMultiplyingByRed:(CGFloat)red green:(CGFloat)green
  202. blue:(CGFloat)blue alpha:(CGFloat)alpha {
  203. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  204. CGFloat r, g, b, a;
  205. if (![self red:&r green:&g blue:&b alpha:&a]) {
  206. return nil;
  207. }
  208. return [UIColor colorWithRed:CLAMP(r * red, 0, 1) green:CLAMP(g * green, 0, 1)
  209. blue:CLAMP(b * blue, 0, 1) alpha:CLAMP(a * alpha, 0, 1)];
  210. }
  211. - (UIColor *)colorByAddingRed:(CGFloat)red green:(CGFloat)green
  212. blue:(CGFloat)blue alpha:(CGFloat)alpha {
  213. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  214. CGFloat r, g, b, a;
  215. if (![self red:&r green:&g blue:&b alpha:&a]) {
  216. return nil;
  217. }
  218. return [UIColor colorWithRed:CLAMP(r + red, 0, 1) green:CLAMP(g + green, 0, 1)
  219. blue:CLAMP(b + blue, 0, 1) alpha:CLAMP(a + alpha, 0, 1)];
  220. }
  221. - (UIColor *)colorByLighteningToRed:(CGFloat)red green:(CGFloat)green
  222. blue:(CGFloat)blue alpha:(CGFloat)alpha {
  223. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  224. CGFloat r, g, b, a;
  225. if (![self red:&r green:&g blue:&b alpha:&a]) {
  226. return nil;
  227. }
  228. return [UIColor colorWithRed:MAX(r, red) green:MAX(g, green) blue:MAX(b, blue)
  229. alpha:MAX(a, alpha)];
  230. }
  231. - (UIColor *)colorByDarkeningToRed:(CGFloat)red green:(CGFloat)green
  232. blue:(CGFloat)blue alpha:(CGFloat)alpha {
  233. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  234. CGFloat r, g, b, a;
  235. if (![self red:&r green:&g blue:&b alpha:&a]) {
  236. return nil;
  237. }
  238. return [UIColor colorWithRed:MIN(r, red) green:MIN(g, green) blue:MIN(b, blue)
  239. alpha:MIN(a, alpha)];
  240. }
  241. - (UIColor *)colorByMultiplyingBy:(CGFloat)f {
  242. return [self colorByMultiplyingByRed:f green:f blue:f alpha:1];
  243. }
  244. - (UIColor *)colorByAdding:(CGFloat)f {
  245. return [self colorByAddingRed:f green:f blue:f alpha:0];
  246. }
  247. - (UIColor *)colorByAdding:(CGFloat)f alpha:(CGFloat)alpha {
  248. return [self colorByAddingRed:f green:f blue:f alpha:alpha];
  249. }
  250. - (UIColor *)colorByLighteningTo:(CGFloat)f {
  251. return [self colorByLighteningToRed:f green:f blue:f alpha:0];
  252. }
  253. - (UIColor *)colorByDarkeningTo:(CGFloat)f {
  254. return [self colorByDarkeningToRed:f green:f blue:f alpha:1];
  255. }
  256. - (UIColor *)colorByMultiplyingByColor:(UIColor *)color {
  257. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  258. CGFloat r, g, b, a;
  259. if (![self red:&r green:&g blue:&b alpha:&a]) {
  260. return nil;
  261. }
  262. return [self colorByMultiplyingByRed:r green:g blue:b alpha:1];
  263. }
  264. - (UIColor *)colorByAddingColor:(UIColor *)color {
  265. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  266. CGFloat r, g, b, a;
  267. if (![self red:&r green:&g blue:&b alpha:&a]) {
  268. return nil;
  269. }
  270. return [self colorByAddingRed:r green:g blue:b alpha:0];
  271. }
  272. - (UIColor *)colorByLighteningToColor:(UIColor *)color {
  273. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  274. CGFloat r, g, b, a;
  275. if (![self red:&r green:&g blue:&b alpha:&a]) {
  276. return nil;
  277. }
  278. return [self colorByLighteningToRed:r green:g blue:b alpha:0];
  279. }
  280. - (UIColor *)colorByDarkeningToColor:(UIColor *)color {
  281. NSAssert(self.canProvideRGBComponents, @"Must be a RGB color to use arithmatic operations");
  282. CGFloat r, g, b, a;
  283. if (![self red:&r green:&g blue:&b alpha:&a]) {
  284. return nil;
  285. }
  286. return [self colorByDarkeningToRed:r green:g blue:b alpha:1];
  287. }
  288. - (UIColor *)colorByAddingHue:(CGFloat)hue saturation:(CGFloat)saturation
  289. brightness:(CGFloat)brightness alpha:(CGFloat)alpha {
  290. CGFloat h, s, b, a;
  291. if (![self hue:&h saturation:&s brightness:&b alpha:&a]) {
  292. return nil;
  293. }
  294. return [UIColor colorWithHue:CLAMP(h + hue, 0, 1)
  295. saturation:CLAMP(s + saturation, 0, 1)
  296. brightness:CLAMP(b + brightness, 0, 1) alpha:CLAMP(a + alpha, 0, 1)];
  297. }
  298. #pragma mark String utilities
  299. - (NSString *)stringFromColor {
  300. NSAssert(self.canProvideRGBComponents, @"Must be an RGB color to use -stringFromColor");
  301. NSString *result;
  302. switch (self.colorSpaceModel) {
  303. case kCGColorSpaceModelRGB:
  304. result = [NSString stringWithFormat:@"{%0.3f, %0.3f, %0.3f, %0.3f}",
  305. self.red, self.green, self.blue,
  306. self.alpha];
  307. break;
  308. case kCGColorSpaceModelMonochrome:
  309. result = [NSString stringWithFormat:@"{%0.3f, %0.3f}", self.white,
  310. self.alpha];
  311. break;
  312. default:
  313. result = nil;
  314. }
  315. return result;
  316. }
  317. - (NSString *)hexStringFromColor {
  318. return [NSString stringWithFormat:@"%0.6lx", self.rgbHex];
  319. }
  320. + (UIColor *)colorWithString:(NSString *)stringToConvert {
  321. NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
  322. if (![scanner scanString:@"{" intoString:NULL]) {
  323. return nil;
  324. }
  325. const NSUInteger kMaxComponents = 4;
  326. CGFloat c[kMaxComponents];
  327. NSUInteger i = 0;
  328. if (![scanner scanFloat:&c[i++]]) {
  329. return nil;
  330. }
  331. while (1) {
  332. if ([scanner scanString:@"}" intoString:NULL]) {
  333. break;
  334. }
  335. if (i >= kMaxComponents) {
  336. return nil;
  337. }
  338. if ([scanner scanString:@"," intoString:NULL]) {
  339. if (![scanner scanFloat:&c[i++]]) {
  340. return nil;
  341. }
  342. } else {
  343. // either we're at the end of there's an unexpected character here
  344. // both cases are error conditions
  345. return nil;
  346. }
  347. }
  348. if (![scanner isAtEnd]) {
  349. return nil;
  350. }
  351. UIColor *color;
  352. switch (i) {
  353. case 2: // monochrome
  354. color = [UIColor colorWithWhite:c[0] alpha:c[1]];
  355. break;
  356. case 4: // RGB
  357. color = [UIColor colorWithRed:c[0] green:c[1] blue:c[2] alpha:c[3]];
  358. break;
  359. default:
  360. color = nil;
  361. }
  362. return color;
  363. }
  364. #pragma mark Class methods
  365. + (UIColor *)randomColor {
  366. return [UIColor colorWithRed:(CGFloat)RAND_MAX / random()
  367. green:(CGFloat)RAND_MAX / random() blue:(CGFloat)RAND_MAX / random()
  368. alpha:1.0f];
  369. }
  370. + (UIColor *)semiRandomColor {
  371. float rand = arc4random() % 12;
  372. return [UIColor colorWithHue:(30 * rand) / 360 saturation:0.5 brightness:0.8
  373. alpha:1];
  374. }
  375. + (UIColor *)colorWithRGBHex:(UInt32)hex {
  376. int r = (hex >> 16) & 0xFF;
  377. int g = (hex >> 8) & 0xFF;
  378. int b = (hex) & 0xFF;
  379. return [UIColor colorWithRed:r / 255.0 green:g / 255.0 blue:b / 255.0 alpha:1];
  380. }
  381. // Returns a UIColor+MGExpanded by scanning the string for a hex number and passing that to +[UIColor+MGExpanded colorWithRGBHex:]
  382. // Skips any leading whitespace and ignores any trailing characters
  383. + (UIColor *)colorWithHexString:(NSString *)stringToConvert {
  384. NSScanner *scanner = [NSScanner scannerWithString:stringToConvert];
  385. unsigned hexNum;
  386. if (![scanner scanHexInt:&hexNum]) {
  387. return nil;
  388. }
  389. return [UIColor colorWithRGBHex:hexNum];
  390. }
  391. // Lookup a color using css 3/svg color name
  392. + (UIColor *)colorWithName:(NSString *)cssColorName {
  393. UIColor *color;
  394. @synchronized (colorNameCache) {
  395. // Look for the color in the cache
  396. color = colorNameCache[cssColorName];
  397. if ((id)color == NSNull.null) {
  398. // If it wasn't there previously, it's still not there now
  399. color = nil;
  400. } else if (!color) {
  401. // Color not in cache, so search for it now
  402. color = [self searchForColorByName:cssColorName];
  403. // Set the value in cache, storing NSNull on failure
  404. colorNameCache[cssColorName] = (color ? : (id)[NSNull null]);
  405. }
  406. }
  407. return color;
  408. }
  409. #pragma mark UIColor_Expanded initialization
  410. + (void)load {
  411. colorNameCache = [[NSMutableDictionary alloc] init];
  412. }
  413. @end
  414. #pragma mark -
  415. #if SUPPORTS_UNDOCUMENTED_API
  416. @implementation UIColor+MGExpanded (Undocumented_Expanded)
  417. - (NSString *)fetchStyleString {
  418. return self.styleString;
  419. }
  420. // Convert a color into RGB Color space, courtesy of Poltras
  421. // via http://ofcodeandmen.poltras.com/2009/01/22/convert-a-cgcolorref-to-another-cgcolorspaceref/
  422. //
  423. - (UIColor+MGExpanded *)rgbColor {
  424. // Call to undocumented method "styleString".
  425. NSString *style = [self styleString];
  426. NSScanner *scanner = [NSScanner scannerWithString:style];
  427. CGFloat red, green, blue;
  428. if (![scanner scanString:@"rgb(" intoString:NULL]) return nil;
  429. if (![scanner scanFloat:&red]) return nil;
  430. if (![scanner scanString:@"," intoString:NULL]) return nil;
  431. if (![scanner scanFloat:&green]) return nil;
  432. if (![scanner scanString:@"," intoString:NULL]) return nil;
  433. if (![scanner scanFloat:&blue]) return nil;
  434. if (![scanner scanString:@")" intoString:NULL]) return nil;
  435. if (![scanner isAtEnd]) return nil;
  436. return [UIColor+MGExpanded colorWithRed:red green:green blue:blue alpha:self.alpha];
  437. }
  438. @end
  439. #endif // SUPPORTS_UNDOCUMENTED_API
  440. @implementation UIColor (Expanded_Support)
  441. /*
  442. * Database of color names and hex rgb values, derived
  443. * from the css 3 color spec:
  444. * http://www.w3.org/TR/css3-color/
  445. *
  446. * We think this is a very compact way of storing
  447. * this information, and relatively cheap to lookup.
  448. *
  449. * Note that we search for color names starting with ','
  450. * and terminated by '#', so that we don't get false matches.
  451. * For this reason, the database begins with ','.
  452. */
  453. static const char *colorNameDB = ","
  454. "aliceblue#f0f8ff,antiquewhite#faebd7,aqua#00ffff,aquamarine#7fffd4,azure#f0ffff,"
  455. "beige#f5f5dc,bisque#ffe4c4,black#000000,blanchedalmond#ffebcd,blue#0000ff,"
  456. "blueviolet#8a2be2,brown#a52a2a,burlywood#deb887,cadetblue#5f9ea0,chartreuse#7fff00,"
  457. "chocolate#d2691e,coral#ff7f50,cornflowerblue#6495ed,cornsilk#fff8dc,crimson#dc143c,"
  458. "cyan#00ffff,darkblue#00008b,darkcyan#008b8b,darkgoldenrod#b8860b,darkgray#a9a9a9,"
  459. "darkgreen#006400,darkgrey#a9a9a9,darkkhaki#bdb76b,darkmagenta#8b008b,"
  460. "darkolivegreen#556b2f,darkorange#ff8c00,darkorchid#9932cc,darkred#8b0000,"
  461. "darksalmon#e9967a,darkseagreen#8fbc8f,darkslateblue#483d8b,darkslategray#2f4f4f,"
  462. "darkslategrey#2f4f4f,darkturquoise#00ced1,darkviolet#9400d3,deeppink#ff1493,"
  463. "deepskyblue#00bfff,dimgray#696969,dimgrey#696969,dodgerblue#1e90ff,"
  464. "firebrick#b22222,floralwhite#fffaf0,forestgreen#228b22,fuchsia#ff00ff,"
  465. "gainsboro#dcdcdc,ghostwhite#f8f8ff,gold#ffd700,goldenrod#daa520,gray#808080,"
  466. "green#008000,greenyellow#adff2f,grey#808080,honeydew#f0fff0,hotpink#ff69b4,"
  467. "indianred#cd5c5c,indigo#4b0082,ivory#fffff0,khaki#f0e68c,lavender#e6e6fa,"
  468. "lavenderblush#fff0f5,lawngreen#7cfc00,lemonchiffon#fffacd,lightblue#add8e6,"
  469. "lightcoral#f08080,lightcyan#e0ffff,lightgoldenrodyellow#fafad2,lightgray#d3d3d3,"
  470. "lightgreen#90ee90,lightgrey#d3d3d3,lightpink#ffb6c1,lightsalmon#ffa07a,"
  471. "lightseagreen#20b2aa,lightskyblue#87cefa,lightslategray#778899,"
  472. "lightslategrey#778899,lightsteelblue#b0c4de,lightyellow#ffffe0,lime#00ff00,"
  473. "limegreen#32cd32,linen#faf0e6,magenta#ff00ff,maroon#800000,mediumaquamarine#66cdaa,"
  474. "mediumblue#0000cd,mediumorchid#ba55d3,mediumpurple#9370db,mediumseagreen#3cb371,"
  475. "mediumslateblue#7b68ee,mediumspringgreen#00fa9a,mediumturquoise#48d1cc,"
  476. "mediumvioletred#c71585,midnightblue#191970,mintcream#f5fffa,mistyrose#ffe4e1,"
  477. "moccasin#ffe4b5,navajowhite#ffdead,navy#000080,oldlace#fdf5e6,olive#808000,"
  478. "olivedrab#6b8e23,orange#ffa500,orangered#ff4500,orchid#da70d6,palegoldenrod#eee8aa,"
  479. "palegreen#98fb98,paleturquoise#afeeee,palevioletred#db7093,papayawhip#ffefd5,"
  480. "peachpuff#ffdab9,peru#cd853f,pink#ffc0cb,plum#dda0dd,powderblue#b0e0e6,"
  481. "purple#800080,red#ff0000,rosybrown#bc8f8f,royalblue#4169e1,saddlebrown#8b4513,"
  482. "salmon#fa8072,sandybrown#f4a460,seagreen#2e8b57,seashell#fff5ee,sienna#a0522d,"
  483. "silver#c0c0c0,skyblue#87ceeb,slateblue#6a5acd,slategray#708090,slategrey#708090,"
  484. "snow#fffafa,springgreen#00ff7f,steelblue#4682b4,tan#d2b48c,teal#008080,"
  485. "thistle#d8bfd8,tomato#ff6347,turquoise#40e0d0,violet#ee82ee,wheat#f5deb3,"
  486. "white#ffffff,whitesmoke#f5f5f5,yellow#ffff00,yellowgreen#9acd32";
  487. + (UIColor *)searchForColorByName:(NSString *)cssColorName {
  488. UIColor *result = nil;
  489. // Compile the string we'll use to search against the database
  490. // We search for ",<colorname>#" to avoid false matches
  491. const char *searchString = [[NSString stringWithFormat:@",%@#", cssColorName]
  492. UTF8String];
  493. // Search for the color name
  494. const char *found = strstr(colorNameDB, searchString);
  495. // If found, step past the search string and grab the hex representation
  496. if (found) {
  497. const char *after = found + strlen(searchString);
  498. int hex;
  499. if (sscanf(after, "%x", &hex) == 1) {
  500. result = [self colorWithRGBHex:hex];
  501. }
  502. }
  503. return result;
  504. }
  505. @end