diff --git a/Demo/TDImageColors.xcodeproj/project.pbxproj b/Demo/TDImageColors.xcodeproj/project.pbxproj index ae086e2..6b335b0 100644 --- a/Demo/TDImageColors.xcodeproj/project.pbxproj +++ b/Demo/TDImageColors.xcodeproj/project.pbxproj @@ -19,9 +19,14 @@ 904BA562183F0A6400F72300 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 904BA54A183F0A6400F72300 /* UIKit.framework */; }; 904BA56A183F0A6400F72300 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 904BA568183F0A6400F72300 /* InfoPlist.strings */; }; 904BA56C183F0A6400F72300 /* TDImageColorsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 904BA56B183F0A6400F72300 /* TDImageColorsTests.m */; }; - 904BA589183F1F7300F72300 /* test_image.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 904BA588183F1F7300F72300 /* test_image.jpg */; }; 904BA58D183F1F8B00F72300 /* TDDemoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 904BA58B183F1F8B00F72300 /* TDDemoViewController.m */; }; 904BA58E183F1F8B00F72300 /* TDDemoViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 904BA58C183F1F8B00F72300 /* TDDemoViewController.xib */; }; + 9066FEF61863F034008FF19E /* test_image0.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 9066FEF01863F034008FF19E /* test_image0.jpg */; }; + 9066FEF71863F034008FF19E /* test_image1.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 9066FEF11863F034008FF19E /* test_image1.jpg */; }; + 9066FEF81863F034008FF19E /* test_image2.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 9066FEF21863F034008FF19E /* test_image2.jpg */; }; + 9066FEF91863F034008FF19E /* test_image3.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 9066FEF31863F034008FF19E /* test_image3.jpg */; }; + 9066FEFA1863F034008FF19E /* test_image4.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 9066FEF41863F034008FF19E /* test_image4.jpg */; }; + 9066FEFB1863F034008FF19E /* test_image5.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 9066FEF51863F034008FF19E /* test_image5.jpg */; }; 90B48ABE184351A200F7C76F /* TDImageColors.m in Sources */ = {isa = PBXBuildFile; fileRef = 90B48AB9184351A200F7C76F /* TDImageColors.m */; }; 90B48ABF184351A200F7C76F /* UIColor+TDAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 90B48ABB184351A200F7C76F /* UIColor+TDAdditions.m */; }; 90B48AC0184351A200F7C76F /* UIImage+TDAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 90B48ABD184351A200F7C76F /* UIImage+TDAdditions.m */; }; @@ -54,10 +59,15 @@ 904BA567183F0A6400F72300 /* TDImageColorsTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "TDImageColorsTests-Info.plist"; sourceTree = ""; }; 904BA569183F0A6400F72300 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = ""; }; 904BA56B183F0A6400F72300 /* TDImageColorsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TDImageColorsTests.m; sourceTree = ""; }; - 904BA588183F1F7300F72300 /* test_image.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test_image.jpg; sourceTree = ""; }; 904BA58A183F1F8B00F72300 /* TDDemoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TDDemoViewController.h; path = TDImageColors/TDDemoViewController.h; sourceTree = SOURCE_ROOT; }; 904BA58B183F1F8B00F72300 /* TDDemoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TDDemoViewController.m; path = TDImageColors/TDDemoViewController.m; sourceTree = SOURCE_ROOT; }; 904BA58C183F1F8B00F72300 /* TDDemoViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = TDDemoViewController.xib; path = TDImageColors/TDDemoViewController.xib; sourceTree = SOURCE_ROOT; }; + 9066FEF01863F034008FF19E /* test_image0.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test_image0.jpg; sourceTree = ""; }; + 9066FEF11863F034008FF19E /* test_image1.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test_image1.jpg; sourceTree = ""; }; + 9066FEF21863F034008FF19E /* test_image2.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test_image2.jpg; sourceTree = ""; }; + 9066FEF31863F034008FF19E /* test_image3.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test_image3.jpg; sourceTree = ""; }; + 9066FEF41863F034008FF19E /* test_image4.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test_image4.jpg; sourceTree = ""; }; + 9066FEF51863F034008FF19E /* test_image5.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = test_image5.jpg; sourceTree = ""; }; 90B48AB8184351A200F7C76F /* TDImageColors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TDImageColors.h; sourceTree = ""; }; 90B48AB9184351A200F7C76F /* TDImageColors.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TDImageColors.m; sourceTree = ""; }; 90B48ABA184351A200F7C76F /* UIColor+TDAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIColor+TDAdditions.h"; sourceTree = ""; }; @@ -126,10 +136,10 @@ 90B48AB7184351A200F7C76F /* TDImageColors */, 904BA555183F0A6400F72300 /* TDAppDelegate.h */, 904BA556183F0A6400F72300 /* TDAppDelegate.m */, - 904BA588183F1F7300F72300 /* test_image.jpg */, 904BA58A183F1F8B00F72300 /* TDDemoViewController.h */, 904BA58B183F1F8B00F72300 /* TDDemoViewController.m */, 904BA58C183F1F8B00F72300 /* TDDemoViewController.xib */, + 9066FEEF1863F034008FF19E /* images */, 904BA558183F0A6400F72300 /* Images.xcassets */, 904BA54D183F0A6400F72300 /* Supporting Files */, ); @@ -165,6 +175,19 @@ name = "Supporting Files"; sourceTree = ""; }; + 9066FEEF1863F034008FF19E /* images */ = { + isa = PBXGroup; + children = ( + 9066FEF01863F034008FF19E /* test_image0.jpg */, + 9066FEF11863F034008FF19E /* test_image1.jpg */, + 9066FEF21863F034008FF19E /* test_image2.jpg */, + 9066FEF31863F034008FF19E /* test_image3.jpg */, + 9066FEF41863F034008FF19E /* test_image4.jpg */, + 9066FEF51863F034008FF19E /* test_image5.jpg */, + ); + path = images; + sourceTree = ""; + }; 90B48AB7184351A200F7C76F /* TDImageColors */ = { isa = PBXGroup; children = ( @@ -255,10 +278,15 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 9066FEF61863F034008FF19E /* test_image0.jpg in Resources */, + 9066FEF71863F034008FF19E /* test_image1.jpg in Resources */, + 9066FEF81863F034008FF19E /* test_image2.jpg in Resources */, 904BA551183F0A6400F72300 /* InfoPlist.strings in Resources */, + 9066FEFA1863F034008FF19E /* test_image4.jpg in Resources */, + 9066FEF91863F034008FF19E /* test_image3.jpg in Resources */, 904BA58E183F1F8B00F72300 /* TDDemoViewController.xib in Resources */, 904BA559183F0A6400F72300 /* Images.xcassets in Resources */, - 904BA589183F1F7300F72300 /* test_image.jpg in Resources */, + 9066FEFB1863F034008FF19E /* test_image5.jpg in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Demo/TDImageColors/TDDemoViewController.h b/Demo/TDImageColors/TDDemoViewController.h index f9b6af7..3e8c50d 100644 --- a/Demo/TDImageColors/TDDemoViewController.h +++ b/Demo/TDImageColors/TDDemoViewController.h @@ -11,5 +11,6 @@ @interface TDDemoViewController : UIViewController @property (strong, nonatomic) IBOutletCollection(UIView) NSArray *colorViews; +@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end diff --git a/Demo/TDImageColors/TDDemoViewController.m b/Demo/TDImageColors/TDDemoViewController.m index 8769d11..f58728d 100644 --- a/Demo/TDImageColors/TDDemoViewController.m +++ b/Demo/TDImageColors/TDDemoViewController.m @@ -29,10 +29,12 @@ - (void)viewDidLoad { dispatch_group_t group = dispatch_group_create(); dispatch_group_enter(group); - UIImage *image = [UIImage imageNamed:@"test_image.jpg"]; + NSString *imageName = [NSString stringWithFormat:@"test_image%d.jpg", arc4random_uniform(6)]; + UIImage *image = [UIImage imageNamed:imageName]; + self.imageView.image = image; TDImageColors *imageColors = [[TDImageColors alloc] initWithImage:image count:5]; dispatch_group_leave(group); - + dispatch_group_notify(group, dispatch_get_main_queue(), ^{ for (UIColor *color in imageColors.colors) { NSUInteger idx = [imageColors.colors indexOfObject:color]; diff --git a/Demo/TDImageColors/TDDemoViewController.xib b/Demo/TDImageColors/TDDemoViewController.xib index c423479..ad5a41b 100644 --- a/Demo/TDImageColors/TDDemoViewController.xib +++ b/Demo/TDImageColors/TDDemoViewController.xib @@ -1,11 +1,12 @@ - + + @@ -19,7 +20,7 @@ - + @@ -55,7 +56,4 @@ - - - \ No newline at end of file diff --git a/Demo/TDImageColors/images/test_image0.jpg b/Demo/TDImageColors/images/test_image0.jpg new file mode 100644 index 0000000..91db957 Binary files /dev/null and b/Demo/TDImageColors/images/test_image0.jpg differ diff --git a/Demo/TDImageColors/test_image.jpg b/Demo/TDImageColors/images/test_image1.jpg similarity index 100% rename from Demo/TDImageColors/test_image.jpg rename to Demo/TDImageColors/images/test_image1.jpg diff --git a/Demo/TDImageColors/images/test_image2.jpg b/Demo/TDImageColors/images/test_image2.jpg new file mode 100644 index 0000000..380e7f1 Binary files /dev/null and b/Demo/TDImageColors/images/test_image2.jpg differ diff --git a/Demo/TDImageColors/test_image3.jpg b/Demo/TDImageColors/images/test_image3.jpg similarity index 100% rename from Demo/TDImageColors/test_image3.jpg rename to Demo/TDImageColors/images/test_image3.jpg diff --git a/Demo/TDImageColors/test_image4.jpg b/Demo/TDImageColors/images/test_image4.jpg similarity index 100% rename from Demo/TDImageColors/test_image4.jpg rename to Demo/TDImageColors/images/test_image4.jpg diff --git a/Demo/TDImageColors/images/test_image5.jpg b/Demo/TDImageColors/images/test_image5.jpg new file mode 100644 index 0000000..4b8ab76 Binary files /dev/null and b/Demo/TDImageColors/images/test_image5.jpg differ diff --git a/TDImageColors.podspec b/TDImageColors.podspec index 883dad6..0e340de 100644 --- a/TDImageColors.podspec +++ b/TDImageColors.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'TDImageColors' - s.version = '0.1.1' + s.version = '0.2' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'iOS Library used to detect a number of most used colors in a UIImage.' s.homepage = 'http://github.com/timominous/TDImageColors' diff --git a/TDImageColors/TDImageColors.m b/TDImageColors/TDImageColors.m index 1d53568..a33d31a 100644 --- a/TDImageColors/TDImageColors.m +++ b/TDImageColors/TDImageColors.m @@ -8,7 +8,7 @@ #import "TDImageColors.h" -#define TDIMAGECOLORS_SCALED_SIZE 32 +#define TDIMAGECOLORS_SCALED_SIZE 100 @interface TDCountedColor : NSObject @@ -42,108 +42,61 @@ - (void)detectColorsFromImage:(UIImage *)image { if (!image) return; NSCountedSet *imageColors; - UIColor *backgroundColor = [self findBackroundColorOfImage:image imageColors:&imageColors]; - BOOL darkBackground = [backgroundColor isDarkColor]; - NSMutableArray *finalColors = [NSMutableArray arrayWithObject:backgroundColor]; - [finalColors addObjectsFromArray:[self findImageColors:imageColors backgroundColor:backgroundColor]]; - while (finalColors.count < self.count) { - if (darkBackground) - [finalColors addObject:[UIColor whiteColor]]; - else - [finalColors addObject:[UIColor blackColor]]; - } - + NSMutableArray *finalColors = [NSMutableArray array]; + [finalColors addObjectsFromArray:[self findColorsOfImage:image imageColors:&imageColors]]; + while (finalColors.count < self.count) + [finalColors addObject:[UIColor whiteColor]]; self.colors = [NSArray arrayWithArray:finalColors]; } -- (NSArray *)findImageColors:(NSCountedSet *)imageColors backgroundColor:(UIColor *)backgroundColor { - NSEnumerator *enumerator = [imageColors objectEnumerator]; - UIColor *curColor = nil; - NSMutableArray *sortedColors = [NSMutableArray arrayWithCapacity:imageColors.count]; - NSMutableArray *resultColors = [NSMutableArray array]; - BOOL findDarkTextColor = ![backgroundColor isDarkColor]; - - while ((curColor = [enumerator nextObject]) != nil) { - curColor = [curColor colorWithMinimumSaturation:0.15f]; - if ([curColor isDarkColor] == findDarkTextColor) { - NSUInteger colorCount = [imageColors countForObject:curColor]; - [sortedColors addObject:[[TDCountedColor alloc] initWithColor:curColor count:colorCount]]; - } - } - - [sortedColors sortUsingSelector:@selector(compare:)]; - - for (TDCountedColor *countedColor in sortedColors) { - curColor = countedColor.color; - BOOL continueFlag = NO; - for (UIColor *c in resultColors) { - if (![curColor isDistinct:c]) { - continueFlag = YES; - break; - } - } - if (continueFlag) - continue; - if (resultColors.count < self.count - 1) - [resultColors addObject:curColor]; - else - break; - } - return [NSArray arrayWithArray:resultColors]; -} - -- (UIColor *)findBackroundColorOfImage:(UIImage *)image imageColors:(NSCountedSet **)colors { +- (NSArray *)findColorsOfImage:(UIImage *)image imageColors:(NSCountedSet **)colors { size_t width = CGImageGetWidth(image.CGImage); size_t height = CGImageGetHeight(image.CGImage); NSCountedSet *imageColors = [[NSCountedSet alloc] initWithCapacity:width * height]; - NSCountedSet *leftEdgeColors = [[NSCountedSet alloc] initWithCapacity:height]; + NSDate *start = [NSDate date]; for (NSUInteger x = 0; x < width; x++) { for (NSUInteger y = 0; y < height; y++) { UIColor *color = [UIImage colorFromImage:image atX:x andY:y]; - if (x == 0) - [leftEdgeColors addObject:color]; [imageColors addObject:color]; } } + NSDate *finish = [NSDate date]; + NSTimeInterval execution = [finish timeIntervalSinceDate:start]; *colors = imageColors; - NSEnumerator *enumerator = [leftEdgeColors objectEnumerator]; + NSEnumerator *enumerator = [imageColors objectEnumerator]; UIColor *curColor = nil; - NSMutableArray *sortedColors = [NSMutableArray arrayWithCapacity:leftEdgeColors.count]; + NSMutableArray *sortedColors = [NSMutableArray arrayWithCapacity:imageColors.count]; + NSMutableArray *resultColors = [NSMutableArray array]; while ((curColor = [enumerator nextObject]) != nil) { - NSUInteger colorCount = [leftEdgeColors countForObject:curColor]; - NSInteger randomColorsThreshold = (NSInteger)(height * kColorThresholdMinimumPercentage); - if (colorCount < randomColorsThreshold) - continue; + curColor = [curColor colorWithMinimumSaturation:0.15f]; + NSUInteger colorCount = [imageColors countForObject:curColor]; [sortedColors addObject:[[TDCountedColor alloc] initWithColor:curColor count:colorCount]]; } [sortedColors sortUsingSelector:@selector(compare:)]; - TDCountedColor *proposedBackgroundColor = nil; - - if (sortedColors.count > 0) { - proposedBackgroundColor = sortedColors[0]; - if ([proposedBackgroundColor.color isBlackOrWhite]) { - for (NSInteger i = 1; i < sortedColors.count; i++) { - TDCountedColor *nextProposed = sortedColors[i]; - if (((double)nextProposed.count / (double)proposedBackgroundColor.count) > 0.3f) { - if (![nextProposed.color isBlackOrWhite]) { - proposedBackgroundColor = nextProposed; - break; - } - } else { - break; - } + for (TDCountedColor *countedColor in sortedColors) { + curColor = countedColor.color; + BOOL continueFlag = NO; + for (UIColor *c in resultColors) { + if (![curColor isDistinct:c]) { + continueFlag = YES; + break; } } + if (continueFlag) + continue; + if (resultColors.count < self.count) + [resultColors addObject:curColor]; + else + break; } - - return proposedBackgroundColor.color; + return [NSArray arrayWithArray:resultColors]; } @end