Skip to content

Commit

Permalink
Merge pull request #6 from aleffert/master
Browse files Browse the repository at this point in the history
Support iOS 5 and iOS 6
  • Loading branch information
fleitz committed May 2, 2013
2 parents a0792fb + 59c3067 commit 5fdc0f6
Showing 1 changed file with 16 additions and 42 deletions.
58 changes: 16 additions & 42 deletions ColorArt/Classes/SLColorArt.m
Expand Up @@ -149,7 +149,7 @@ - (UIImage*)_scaleImage:(UIImage*)image size:(CGSize)scaledSize

- (NSDictionary*)_analyzeImage:(UIImage*)anImage
{
NSMutableDictionary *imageColors = nil;
NSArray *imageColors = nil;
UIColor *backgroundColor = [self _findEdgeColor:anImage imageColors:&imageColors];
UIColor *primaryColor = nil;
UIColor *secondaryColor = nil;
Expand Down Expand Up @@ -212,7 +212,7 @@ - (NSDictionary*)_analyzeImage:(UIImage*)anImage

} RGBAPixel;

- (UIColor*)_findEdgeColor:(UIImage*)image imageColors:(NSDictionary**)colors
- (UIColor*)_findEdgeColor:(UIImage*)image imageColors:(NSArray**)colors
{
CGImageRef imageRep = image.CGImage;

Expand Down Expand Up @@ -258,49 +258,34 @@ - (UIColor*)_findEdgeColor:(UIImage*)image imageColors:(NSDictionary**)colors
}
CGContextRelease(bmContext);

NSMutableDictionary* imageColors = [NSMutableDictionary dictionary];
NSMutableDictionary* edgeColors = [NSMutableDictionary dictionary];
NSMutableArray* imageColors = [NSMutableArray array];
NSMutableArray* edgeColors = [NSMutableArray array];

for(NSUInteger b = 0; b < pixelRange; b++) {
for(NSUInteger g = 0; g < pixelRange; g++) {
for(NSUInteger r = 0; r < pixelRange; r++) {
NSUInteger count = rawImageColors[r][g][b];
if(count > 3) {
if(count > self.randomColorThreshold) {
UIColor* color = [UIColor colorWithRed:r / (CGFloat)pixelRange green:g / (CGFloat)pixelRange blue:b / (CGFloat)pixelRange alpha:1];
[imageColors setObject:@(count) forKey:color];
PCCountedColor* countedColor = [[PCCountedColor alloc] initWithColor:color count:count];
[imageColors addObject:countedColor];
}

count = rawEdgeColors[r][g][b];
if(count > 3) {
if(count > self.randomColorThreshold) {
UIColor* color = [UIColor colorWithRed:r / (CGFloat)pixelRange green:g / (CGFloat)pixelRange blue:b / (CGFloat)pixelRange alpha:1];
[edgeColors setObject:@(count) forKey:color];
PCCountedColor* countedColor = [[PCCountedColor alloc] initWithColor:color count:count];
[edgeColors addObject:countedColor];
}
}
}
}

*colors = imageColors;


NSEnumerator *enumerator = [edgeColors.allKeys objectEnumerator];
UIColor *curColor = nil;
NSMutableArray *sortedColors = [NSMutableArray arrayWithCapacity:[edgeColors count]];

while ( (curColor = [enumerator nextObject]) != nil )
{
NSUInteger colorCount = [[edgeColors objectForKey:curColor] unsignedIntegerValue];

if ( colorCount <= self.randomColorThreshold ) // prevent using random colors, threshold should be based on input image size
continue;

PCCountedColor *container = [[PCCountedColor alloc] initWithColor:curColor count:colorCount];

[sortedColors addObject:container];
}


NSMutableArray* sortedColors = edgeColors;
[sortedColors sortUsingSelector:@selector(compare:)];


PCCountedColor *proposedEdgeColor = nil;

if ( [sortedColors count] > 0 )
Expand Down Expand Up @@ -334,20 +319,18 @@ - (UIColor*)_findEdgeColor:(UIImage*)image imageColors:(NSDictionary**)colors
}


- (void)_findTextColors:(NSMutableDictionary*)colors primaryColor:(UIColor**)primaryColor secondaryColor:(UIColor**)secondaryColor detailColor:(UIColor**)detailColor backgroundColor:(UIColor*)backgroundColor
- (void)_findTextColors:(NSArray*)colors primaryColor:(UIColor**)primaryColor secondaryColor:(UIColor**)secondaryColor detailColor:(UIColor**)detailColor backgroundColor:(UIColor*)backgroundColor
{
NSEnumerator *enumerator = [colors.allKeys objectEnumerator];
UIColor *curColor = nil;
NSMutableArray *sortedColors = [NSMutableArray arrayWithCapacity:[colors count]];
BOOL findDarkTextColor = ![backgroundColor pc_isDarkColor];

while ( (curColor = [enumerator nextObject]) != nil )
{
curColor = [curColor pc_colorWithMinimumSaturation:.15];
for(PCCountedColor* countedColor in colors) {
UIColor* curColor = [countedColor.color pc_colorWithMinimumSaturation:.15];

if ( [curColor pc_isDarkColor] == findDarkTextColor )
{
NSUInteger colorCount = [[colors objectForKey:curColor] unsignedIntegerValue];
NSUInteger colorCount = countedColor.count;

//if ( colorCount <= 2 ) // prevent using random colors, threshold should be based on input image size
// continue;
Expand Down Expand Up @@ -388,16 +371,7 @@ - (void)_findTextColors:(NSMutableDictionary*)colors primaryColor:(UIColor**)pri
}

@end
@implementation UIColor (Copying)

- (id)copyWithZone:(NSZone *)zone{
CGColorRef cgcopy = CGColorCreateCopy([self CGColor]);
UIColor *copy = [[[self class] allocWithZone:zone] initWithCGColor:cgcopy];
CGColorRelease(cgcopy);
return copy;
}

@end
@implementation UIColor (DarkAddition)

- (BOOL)pc_isDarkColor
Expand Down

0 comments on commit 5fdc0f6

Please sign in to comment.