UIColor+Additions.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #import "UIColor+Additions.h"
  2. @implementation UIColor (Additions)
  3. - (UIColor*)changeBrightnessByAmount:(CGFloat)amount
  4. {
  5. CGFloat hue, saturation, brightness, alpha;
  6. if ([self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
  7. brightness += (amount-1.0);
  8. brightness = MAX(MIN(brightness, 1.0), 0.0);
  9. return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
  10. }
  11. CGFloat white;
  12. if ([self getWhite:&white alpha:&alpha]) {
  13. white += (amount-1.0);
  14. white = MAX(MIN(white, 1.0), 0.0);
  15. return [UIColor colorWithWhite:white alpha:alpha];
  16. }
  17. return nil;
  18. }
  19. + (UIColor*)changeBrightness:(UIColor*)color amount:(CGFloat)amount
  20. {
  21. CGFloat hue, saturation, brightness, alpha;
  22. if ([color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
  23. brightness += (amount-1.0);
  24. brightness = MAX(MIN(brightness, 1.0), 0.0);
  25. return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
  26. }
  27. CGFloat white;
  28. if ([color getWhite:&white alpha:&alpha]) {
  29. white += (amount-1.0);
  30. white = MAX(MIN(white, 1.0), 0.0);
  31. return [UIColor colorWithWhite:white alpha:alpha];
  32. }
  33. return nil;
  34. }
  35. + (UIColor *)colorFromHex:(NSString *)s
  36. {
  37. NSScanner *scan = [NSScanner scannerWithString:[s substringToIndex:2]];
  38. unsigned int r = 0, g = 0, b = 0;
  39. [scan scanHexInt:&r];
  40. scan = [NSScanner scannerWithString:[[s substringFromIndex:2] substringToIndex:2]];
  41. [scan scanHexInt:&g];
  42. scan = [NSScanner scannerWithString:[s substringFromIndex:4]];
  43. [scan scanHexInt:&b];
  44. return [UIColor colorWithRed:(float)r/255 green:(float)g/255 blue:(float)b/255 alpha:1.0];
  45. }
  46. - (NSString *)hexValue
  47. {
  48. const CGFloat *components = CGColorGetComponents(self.CGColor);
  49. CGFloat r = components[0];
  50. CGFloat g = components[1];
  51. CGFloat b = components[2];
  52. return [NSString stringWithFormat:@"%02lX%02lX%02lX",
  53. lroundf(r * 255),
  54. lroundf(g * 255),
  55. lroundf(b * 255)];
  56. }
  57. - (UIColor *)sqf_contrastingColorWithMethod:(SQFContrastingColorMethod)method
  58. {
  59. switch (method) {
  60. case SQFContrastingColorFiftyPercentMethod:
  61. return [self sqf_contrastingColorFiftyPercentMethod];
  62. break;
  63. case SQFContrastingColorYIQMethod:
  64. return [self sqf_contrastingColorYIQMethod];
  65. }
  66. }
  67. - (UIColor *)sqf_contrastingColorFiftyPercentMethod
  68. {
  69. CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
  70. [self getRed:&red green:&green blue:&blue alpha:&alpha];
  71. NSInteger redDecimal = (NSInteger)(red * 255);
  72. NSInteger greenDecimal = (NSInteger)(green * 255);
  73. NSInteger blueDecimal = (NSInteger)(blue * 255);
  74. NSString *hex = [NSString stringWithFormat:@"%02x%02x%02x", (unsigned int)redDecimal, (unsigned int)greenDecimal, (unsigned int)blueDecimal];
  75. unsigned int result = 0;
  76. NSScanner *scanner = [NSScanner scannerWithString:hex];
  77. [scanner scanHexInt:&result];
  78. return (result > 0xffffff / 2) ? [UIColor blackColor] : [UIColor whiteColor];
  79. }
  80. - (UIColor *)sqf_contrastingColorYIQMethod
  81. {
  82. CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
  83. [self getRed:&red green:&green blue:&blue alpha:&alpha];
  84. CGFloat yiq = ((( red * 255 ) * 299 ) + (( green * 255 ) * 587 ) + (( blue * 255 ) * 114 )) / 1000;
  85. return (yiq >= 128.0) ? [UIColor blackColor] : [UIColor whiteColor];
  86. }
  87. @end