This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSLColorArt.m
executable file
·526 lines (396 loc) · 13.1 KB
/
SLColorArt.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#import "SLColorArt.h"
#define kColorThresholdMinimumPercentage 0.01
UIColor* CGImageColorAtXY(CGImageRef image, NSInteger x, NSInteger y) {
return [UIColor blackColor];
}
typedef struct gs_pixel {
uint8_t a;
uint8_t r;
uint8_t g;
uint8_t b;
} gs_pixel;
uint8_t gs_pixel_avg (gs_pixel * p) {
int16_t a = (p->r + p->g + p->b)/3;
//clip result to 0xFF to avoid wraparound
return (uint8_t)(a > 0xFF)?0xFF:a;
}
UIColor *dataColorAtXY(const gs_pixel *imgData, int pixelsPerRow, int x, int y)
{
gs_pixel pixel = imgData[y*pixelsPerRow + x];
UIColor *color = [UIColor colorWithRed:pixel.r/255.f green:pixel.g/255.f blue:pixel.b/255.f alpha:pixel.a/255.f];
return color;
}
@interface UIColor (DarkAddition)
- (BOOL)pc_isDarkColor;
- (BOOL)pc_isDistinct:(UIColor *)compareColor;
- (UIColor *)pc_colorWithMinimumSaturation:(CGFloat)saturation;
- (BOOL)pc_isBlackOrWhite;
- (BOOL)pc_isContrastingColor:(UIColor *)color;
@end
@interface PCCountedColor : NSObject
@property (assign) NSUInteger count;
@property (strong) UIColor *color;
- (id)initWithColor:(UIColor *)color count:(NSUInteger)count;
@end
@interface SLColorArt ()
@property CGSize scaledSize;
@property(retain,readwrite) UIColor *backgroundColor;
@property(retain,readwrite) UIColor *primaryColor;
@property(retain,readwrite) UIColor *secondaryColor;
@property(retain,readwrite) UIColor *detailColor;
@property(strong) NSData *imgData;
@end
@implementation SLColorArt
- (id)initWithImage:(UIImage *)image
{
return [self initWithImage:image scaledSize:CGSizeZero];
}
- (id)initWithImage:(UIImage *)image scaledSize:(CGSize)size
{
if (!image) return nil;
self = [super init];
if (self)
{
if (CGSizeEqualToSize(size, CGSizeZero) || CGSizeEqualToSize(size, image.size)) {
self.scaledSize = image.size;
self.scaledImage = image;
} else {
self.scaledSize = size;
UIImage *finalImage = [self scaleImage:image size:size];
self.scaledImage = finalImage;
}
[self extractDataFromImage:self.scaledImage];
[self analyzeImage:image];
}
return self;
}
- (void)extractDataFromImage:(UIImage *)image {
CGColorSpaceRef d_colorSpace = CGColorSpaceCreateDeviceRGB();
size_t bytesPerRow = image.size.width * 4;
NSUInteger len = image.size.height*bytesPerRow;
gs_pixel *data = malloc(len);
self.imgData = [NSData dataWithBytesNoCopy:data length:len freeWhenDone:YES];
CGContextRef context = CGBitmapContextCreate(data, image.size.width,
image.size.height,
8, bytesPerRow,
d_colorSpace,
kCGImageAlphaNoneSkipFirst);
UIGraphicsPushContext(context);
CGContextTranslateCTM(context, 0.0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
[image drawInRect:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
UIGraphicsPopContext();
CGContextRelease(context);
CGColorSpaceRelease(d_colorSpace);
}
- (UIImage *)scaleImage:(UIImage *)image size:(CGSize)targetSize
{
UIImage *sourceImage = image;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
{
scaleFactor = widthFactor; // scale to fit height
}
else
{
scaleFactor = heightFactor; // scale to fit width
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
{
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
{
//NSLog(@"could not scale image");
}
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
- (void)analyzeImage:(UIImage *)anImage
{
NSCountedSet *imageColors = nil;
UIColor *backgroundColor = [self imageColors:&imageColors];
UIColor *primaryColor = nil;
UIColor *secondaryColor = nil;
UIColor *detailColor = nil;
BOOL darkBackground = [backgroundColor pc_isDarkColor];
CGFloat red, green, blue, alpha;
[backgroundColor getRed:&red green:&green blue:&blue alpha:&alpha];
[self findTextColors:imageColors primaryColor:&primaryColor secondaryColor:&secondaryColor detailColor:&detailColor backgroundColor:backgroundColor];
if ( primaryColor == nil )
{
//NSLog(@"missed primary");
if ( darkBackground )
primaryColor = [UIColor whiteColor];
else
primaryColor = [UIColor blackColor];
}
if ( secondaryColor == nil )
{
//NSLog(@"missed secondary");
if ( darkBackground )
secondaryColor = [UIColor whiteColor];
else
secondaryColor = [UIColor blackColor];
}
if ( detailColor == nil )
{
//NSLog(@"missed detail");
if ( darkBackground )
detailColor = [UIColor whiteColor];
else
detailColor = [UIColor blackColor];
}
self.backgroundColor = backgroundColor;
self.primaryColor = primaryColor;
self.secondaryColor = secondaryColor;
self.detailColor = detailColor;
}
- (UIColor*)imageColors:(NSCountedSet**)colors
{
NSInteger pixelsWide = self.scaledImage.size.width;
NSInteger pixelsHigh = self.scaledImage.size.height;
NSCountedSet *imageColors = [[NSCountedSet alloc] initWithCapacity:pixelsWide * pixelsHigh];
NSCountedSet *leftEdgeColors = [[NSCountedSet alloc] initWithCapacity:pixelsHigh];
NSUInteger x, y;
x = y = 0;
while (x < pixelsWide)
{
while (y < pixelsHigh)
{
UIColor *color = dataColorAtXY(self.imgData.bytes, self.scaledSize.width, x, y);
if ( x == 0 )
{
[leftEdgeColors addObject:color];
}
[imageColors addObject:color];
y += 2;
}
x += 2;
}
*colors = imageColors;
NSEnumerator *enumerator = [leftEdgeColors objectEnumerator];
UIColor *curColor = nil;
NSMutableArray *sortedColors = [NSMutableArray arrayWithCapacity:[leftEdgeColors count]];
while ( (curColor = [enumerator nextObject]) != nil )
{
NSUInteger colorCount = [leftEdgeColors countForObject:curColor];
NSInteger randomColorsThreshold = (NSInteger)(pixelsHigh * kColorThresholdMinimumPercentage);
if ( colorCount <= randomColorsThreshold ) // prevent using random colors, threshold based on input image height
continue;
PCCountedColor *container = [[PCCountedColor alloc] initWithColor:curColor count:colorCount];
[sortedColors addObject:container];
}
[sortedColors sortUsingSelector:@selector(compare:)];
PCCountedColor *proposedEdgeColor = nil;
if ( [sortedColors count] > 0 )
{
proposedEdgeColor = [sortedColors objectAtIndex:0];
if ( [proposedEdgeColor.color pc_isBlackOrWhite] ) // want to choose color over black/white so we keep looking
{
for ( NSInteger i = 1; i < [sortedColors count]; i++ )
{
PCCountedColor *nextProposedColor = [sortedColors objectAtIndex:i];
if (((double)nextProposedColor.count / (double)proposedEdgeColor.count) > .3 ) // make sure the second choice color is 30% as common as the first choice
{
if ( ![nextProposedColor.color pc_isBlackOrWhite] )
{
proposedEdgeColor = nextProposedColor;
break;
}
}
else
{
// reached color threshold less than 40% of the original proposed edge color so bail
break;
}
}
}
}
return proposedEdgeColor.color;
}
- (void)findTextColors:(NSCountedSet*)colors primaryColor:(UIColor**)primaryColor secondaryColor:(UIColor**)secondaryColor detailColor:(UIColor**)detailColor backgroundColor:(UIColor*)backgroundColor
{
NSEnumerator *enumerator = [colors 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];
if ( [curColor pc_isDarkColor] == findDarkTextColor )
{
NSUInteger colorCount = [colors countForObject:curColor];
PCCountedColor *container = [[PCCountedColor alloc] initWithColor:curColor count:colorCount];
[sortedColors addObject:container];
}
}
[sortedColors sortUsingSelector:@selector(compare:)];
for ( PCCountedColor *curContainer in sortedColors )
{
curColor = curContainer.color;
if ( *primaryColor == nil )
{
if ( [curColor pc_isContrastingColor:backgroundColor] )
*primaryColor = curColor;
}
else if ( *secondaryColor == nil )
{
if ( ![*primaryColor pc_isDistinct:curColor] || ![curColor pc_isContrastingColor:backgroundColor] )
continue;
*secondaryColor = curColor;
}
else if ( *detailColor == nil )
{
if ( ![*secondaryColor pc_isDistinct:curColor] || ![*primaryColor pc_isDistinct:curColor] || ![curColor pc_isContrastingColor:backgroundColor] )
continue;
*detailColor = curColor;
break;
}
}
}
@end
@implementation UIColor (DarkAddition)
- (BOOL)pc_isDarkColor
{
CGFloat r, g, b, a;
[self getRed:&r green:&g blue:&b alpha:&a];
CGFloat lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
if ( lum < .5 )
{
return YES;
}
return NO;
}
- (BOOL)pc_isDistinct:(UIColor*)compareColor
{
CGFloat r, g, b, a;
CGFloat r1, g1, b1, a1;
[self getRed:&r green:&g blue:&b alpha:&a];
[compareColor getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
CGFloat threshold = .25; //.15
if ( fabs(r - r1) > threshold ||
fabs(g - g1) > threshold ||
fabs(b - b1) > threshold ||
fabs(a - a1) > threshold )
{
// check for grays, prevent multiple gray colors
if ( fabs(r - g) < .03 && fabs(r - b) < .03 )
{
if ( fabs(r1 - g1) < .03 && fabs(r1 - b1) < .03 )
return NO;
}
return YES;
}
return NO;
}
- (UIColor*)pc_colorWithMinimumSaturation:(CGFloat)minSaturation
{
if ( self != nil )
{
CGFloat hue = 0.0;
CGFloat saturation = 0.0;
CGFloat brightness = 0.0;
CGFloat alpha = 0.0;
[self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
if ( saturation < minSaturation )
{
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
}
}
return self;
}
- (BOOL)pc_isBlackOrWhite
{
// UIColor *tempColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
if ( self != nil )
{
CGFloat r, g, b, a;
[self getRed:&r green:&g blue:&b alpha:&a];
if ( r > .91 && g > .91 && b > .91 )
return YES; // white
if ( r < .09 && g < .09 && b < .09 )
return YES; // black
}
return NO;
}
- (BOOL)pc_isContrastingColor:(UIColor*)color
{
if ( self != nil && color != nil )
{
CGFloat br, bg, bb, ba;
CGFloat fr, fg, fb, fa;
[self getRed:&br green:&bg blue:&bb alpha:&ba];
[color getRed:&fr green:&fg blue:&fb alpha:&fa];
CGFloat bLum = 0.2126 * br + 0.7152 * bg + 0.0722 * bb;
CGFloat fLum = 0.2126 * fr + 0.7152 * fg + 0.0722 * fb;
CGFloat contrast = 0.;
if ( bLum > fLum )
contrast = (bLum + 0.05) / (fLum + 0.05);
else
contrast = (fLum + 0.05) / (bLum + 0.05);
//return contrast > 3.0; //3-4.5 W3C recommends 3:1 ratio, but that filters too many colors
return contrast > 1.6;
}
return YES;
}
@end
@implementation PCCountedColor
- (id)initWithColor:(UIColor*)color count:(NSUInteger)count
{
self = [super init];
if ( self )
{
self.color = color;
self.count = count;
}
return self;
}
- (NSComparisonResult)compare:(PCCountedColor*)object
{
if ( [object isKindOfClass:[PCCountedColor class]] )
{
if ( self.count < object.count )
{
return NSOrderedDescending;
}
else if ( self.count == object.count )
{
return NSOrderedSame;
}
}
return NSOrderedAscending;
}
@end