-
Notifications
You must be signed in to change notification settings - Fork 2
/
LEColorPicker.m
executable file
·1010 lines (822 loc) · 35.1 KB
/
LEColorPicker.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// LEColorPicker.m
// LEColorPicker
//
// Created by Luis Enrique Espinoza Severino on 10-12-12.
// Copyright (c) 2012 Luis Espinoza. All rights reserved.
//
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
#import "LEColorPicker.h"
#import <OpenGLES/ES2/gl.h>
#import <OpenGLES/ES2/glext.h>
#import <UIKit/UIKit.h>
#import <float.h>
@interface UIColor (YUVSpace)
+ (float) yComponentFromColor:(UIColor*)color;
+ (float) uComponentFromColor:(UIColor*)color;
+ (float) vComponentFromColor:(UIColor*)color;
+ (float) YUVSpaceDistanceToColor:(UIColor*)toColor fromColor:(UIColor*)fromColor;
+ (float) YUVSpaceSquareDistanceToColor:(UIColor *)toColor fromColor:(UIColor *)fromColor;
@end
@implementation UIColor (YUVSpace)
+ (float)yComponentFromColor:(UIColor *)color
{
float red = 0.0;
float green = 0.0;
float blue = 0.0;
float alpha = 0.0;
float y = 0.0;
if (color) {
size_t numComponents = CGColorGetNumberOfComponents(color.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
y = 0.299*red + 0.587*green+ 0.114*blue;
}
return y;
}
+ (float)uComponentFromColor:(UIColor *)color
{
float red = 0.0;
float green = 0.0;
float blue = 0.0;
float alpha = 0.0;
float u = 0.0;
if (color) {
size_t numComponents = CGColorGetNumberOfComponents(color.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
u = (-0.14713)*red + (-0.28886)*green + (0.436)*blue;
}
return u;
}
+ (float)vComponentFromColor:(UIColor *)color
{
float red = 0.0;
float green = 0.0;
float blue = 0.0;
float alpha = 0.0;
float v = 0.0;
if (color) {
size_t numComponents = CGColorGetNumberOfComponents(color.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
v = 0.615*red + (-0.51499)*green + (-0.10001)*blue;
}
return v;
}
+ (float)YUVSpaceDistanceToColor:(UIColor *)toColor fromColor:(UIColor *)fromColor
{
float YToColor = [UIColor yComponentFromColor:toColor];
float UToColor = [UIColor uComponentFromColor:toColor];
float VToColor = [UIColor vComponentFromColor:toColor];
float YFromColor = [UIColor yComponentFromColor:fromColor];
float UFromColor = [UIColor uComponentFromColor:fromColor];
float VFromColor = [UIColor vComponentFromColor:fromColor];
float deltaY = YToColor - YFromColor;
float deltaU = UToColor - UFromColor;
float deltaV = VToColor - VFromColor;
float distance = sqrtf(deltaY*deltaY + deltaU*deltaU + deltaV*deltaV);
return distance;
}
+ (float)YUVSpaceSquareDistanceToColor:(UIColor *)toColor fromColor:(UIColor *)fromColor
{
float YToColor = [UIColor yComponentFromColor:toColor];
float UToColor = [UIColor uComponentFromColor:toColor];
float VToColor = [UIColor vComponentFromColor:toColor];
float YFromColor = [UIColor yComponentFromColor:fromColor];
float UFromColor = [UIColor uComponentFromColor:fromColor];
float VFromColor = [UIColor vComponentFromColor:fromColor];
float deltaY = YToColor - YFromColor;
float deltaU = UToColor - UFromColor;
float deltaV = VToColor - VFromColor;
float distance = deltaY*deltaY + deltaU*deltaU + deltaV*deltaV;
return distance;
}
@end
@implementation LEColorScheme
@end
@implementation LEColorPicker
#pragma mark - Preprocessor definitions
#define LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE 32
#define LECOLORPICKER_BACKGROUND_FILTER_TOLERANCE 0.5
#define LECOLORPICKER_PRIMARY_TEXT_FILTER_TOLERANCE 0.3
#define LECOLORPICKER_DEFAULT_COLOR_DIFFERENCE 500
#define LECOLORPICKER_DEFAULT_BRIGHTNESS_DIFFERENCE 125
#define STRINGIZE(x) #x
#define STRINGIZE2(x) STRINGIZE(x)
#define SHADER_STRING(text) @ STRINGIZE2(text)
#pragma mark - C structures and constants
// Vertex structure
typedef struct {
float Position[3];
float Color[4];
float TexCoord[2];
} Vertex;
// LEColor structure
typedef struct {
NSUInteger red;
NSUInteger green;
NSUInteger blue;
} LEColor;
// Add texture coordinates to Vertices as follows
const Vertex Vertices[] = {
// Front
{{1, -1, 0}, {1, 0, 0, 1}, {1, 0}},
{{1, 1, 0}, {0, 1, 0, 1}, {1, 1}},
{{-1, 1, 0}, {0, 0, 1, 1}, {0, 1}},
{{-1, -1, 0}, {0, 0, 0, 1}, {0, 0}},
};
// Triangles coordinates
const GLubyte Indices[] = {
// Front
0, 1, 2,
2, 3, 0,
};
//Vertex Shader string definition
NSString *const kDominantVertexShaderString = SHADER_STRING
(
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;
void main(void) {
DestinationColor = SourceColor;
gl_Position = Position;
TexCoordOut = TexCoordIn;
}
);
//Fragment Shader string definition
NSString *const kDominantFragmentShaderString = SHADER_STRING
(
varying lowp vec4 DestinationColor;
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
uniform int ProccesedWidth;
uniform int TotalWidth;
void main(void) {
lowp vec4 dummyColor = DestinationColor; //Dummy line for avoid WARNING from shader compiler
lowp float accumulator = 0.0;
lowp vec4 currentPixel = texture2D(Texture, TexCoordOut);
highp float currentY = 0.299*currentPixel.r + 0.587*currentPixel.g+ 0.114*currentPixel.b;
highp float currentU = (-0.14713)*currentPixel.r + (-0.28886)*currentPixel.g + (0.436)*currentPixel.b;
highp float currentV = 0.615*currentPixel.r + (-0.51499)*currentPixel.g + (-0.10001)*currentPixel.b;
highp vec3 currentYUV = vec3(currentY,currentU,currentV);
lowp float d;
if ((TexCoordOut.x > (float(ProccesedWidth)/float(TotalWidth))) || (TexCoordOut.y > (float(ProccesedWidth)/float(TotalWidth)))) {
gl_FragColor = vec4(0.0,0.0,0.0,1.0);
} else {
accumulator = 0.0;
for (int i=0; i<ProccesedWidth; i=i+1) {
for (int j=0; j<ProccesedWidth; j=j+1) {
lowp vec2 coord = vec2(float(i)/float(TotalWidth),float(j)/float(TotalWidth));
lowp vec4 samplePixel = texture2D(Texture, coord);
highp float sampleY = 0.299*samplePixel.r + 0.587*samplePixel.g+ 0.114*samplePixel.b;
highp float sampleU = (-0.14713)*samplePixel.r + (-0.28886)*samplePixel.g + (0.436)*samplePixel.b;
highp float sampleV = 0.615*samplePixel.r + (-0.51499)*samplePixel.g + (-0.10001)*samplePixel.b;
highp vec3 sampleYUV = vec3(sampleY,sampleU,sampleV);
d = distance(sampleYUV,currentYUV);
if (d < 0.1) {
accumulator = accumulator + 0.0039;
}
}
}
gl_FragColor = vec4(currentPixel.r,currentPixel.g,currentPixel.b,accumulator);
}
}
);
#pragma mark - C internal functions declaration (to avoid possible warnings)
/**
Function for free output buffer data.
**/
void freeImageData(void *info, const void *data, size_t size);
/**
Function for calculating the square euclidian distance between 2 RGB colors in RGB space.
@param colorA A RGB color.
@param colorB Another RGB color.
@return The square of euclidian distance in RGB space.
*/
NSUInteger squareDistanceInRGBSpaceBetweenColor(LEColor colorA, LEColor colorB);
#pragma mark - C internal functions implementation
void freeImageData(void *info, const void *data, size_t size)
{
//printf("freeImageData called");
free((void*)data);
}
NSUInteger squareDistanceInRGBSpaceBetweenColor(LEColor colorA, LEColor colorB)
{
NSUInteger squareDistance = ((colorA.red - colorB.red)*(colorA.red - colorB.red))+
((colorA.green - colorB.green) * (colorA.green - colorB.green))+
((colorA.blue - colorB.blue) * (colorA.blue - colorB.blue));
return squareDistance;
}
#pragma mark - Obj-C interface methods
- (id)init
{
self = [super init];
if (self) {
// Create queue and set working flag initial state
taskQueue = dispatch_queue_create("LEColorPickerQueue", DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(taskQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0));
// Add notifications for multitasking and background aware
//[self addNotificationObservers];
}
return self;
}
- (void)pickColorsFromImage:(UIImage *)image
onComplete:(void (^)(LEColorScheme *colorsPickedDictionary))completeBlock
{
if ([self isAppActive]) {
dispatch_async(taskQueue, ^{
// Color calculation process
LEColorScheme *colorScheme = [self colorSchemeFromImage:image];
// Call complete block and pass colors result
dispatch_async(dispatch_get_main_queue(), ^{
completeBlock(colorScheme);
});
});
}
}
- (LEColorScheme*)colorSchemeFromImage:(UIImage*)inputImage
{
if ([self isAppActive]) {
// First, we scale the input image, to get a constant image size and square texture.
UIImage *scaledImage = [self scaleImage:inputImage
width:LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE
height:LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE];
// Now, We set the initial OpenGL ES 2.0 state.
[self setupOpenGL];
// Then we set the scaled image as the texture to render.
_aTexture = [self setupTextureFromImage:scaledImage];
// Now that all is ready, proceed we the render, to find the dominant color
[self renderDominant];
// Now that we have the rendered result, we start the color calculations.
LEColorScheme *colorScheme = [[LEColorScheme alloc] init];
colorScheme.backgroundColor = [self colorWithBiggerCountFromImageWidth:LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE height:LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE];
// Now, find text colors
[self findTextColorsTaskForColorScheme:colorScheme];
return colorScheme;
}
return nil;
}
#pragma mark - Old interface implementation
+ (void)pickColorFromImage:(UIImage *)image onComplete:(void (^)(NSDictionary *))completeBlock
{
static LEColorPicker *colorPicker;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
colorPicker = [[LEColorPicker alloc] init];
});
[colorPicker pickColorsFromImage:image onComplete:^(LEColorScheme *colorScheme) {
NSDictionary *colorsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
colorScheme.backgroundColor,@"BackgroundColor",
colorScheme.primaryTextColor,@"PrimaryTextColor",
colorScheme.secondaryTextColor,@"SecondaryTextColor", nil];
if ([NSThread isMainThread]) {
completeBlock(colorsDictionary);
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
completeBlock(colorsDictionary);
});
}
}];
}
#pragma mark - OpenGL ES 2 custom methods
- (void)setupOpenGL
{
// Start openGLES
if([self setupContext]){
[self setupFrameBuffer];
[self setupRenderBuffer];
[self setupDepthBuffer];
[self setupOpenGLForDominantColor];
[self setupVBOs];
}
}
- (void)renderDominant
{
//start up
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ZERO);
glEnable(GL_TEXTURE_2D);
//Setup inputs
glViewport(0, 0, LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE, LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glVertexAttribPointer(_positionSlot, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(_colorSlot, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 3));
glVertexAttribPointer(_texCoordSlot, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float) * 7));
glUniform1i(_proccesedWidthSlot, LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE/2);
glUniform1i(_totalWidthSlot, LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _aTexture);
glUniform1i(_textureUniform, 0);
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}
- (GLuint)setupTextureFromImage:(UIImage*)image
{
// Get core graphics image reference
CGImageRef inputTextureImage = image.CGImage;
if (!inputTextureImage) {
LELog(@"Failed to load image for texture");
}
NSUInteger width = CGImageGetWidth(inputTextureImage);
NSUInteger height = CGImageGetHeight(inputTextureImage);
GLubyte *inputTextureData = (GLubyte*)calloc(width*height*4, sizeof(GLubyte));
CGColorSpaceRef inputTextureColorSpace = CGImageGetColorSpace(inputTextureImage);
CGContextRef inputTextureContext = CGBitmapContextCreate(inputTextureData,
width, height,
8, width*4,
inputTextureColorSpace,
(CGBitmapInfo)kCGImageAlphaPremultipliedLast);
//3 Draw image into the context
CGContextDrawImage(inputTextureContext, CGRectMake(0, 0, width, height),inputTextureImage);
CGContextRelease(inputTextureContext);
//4 Send the pixel data to OpenGL
GLuint inputTexName;
glGenTextures(1, &inputTexName);
glBindTexture(GL_TEXTURE_2D, inputTexName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA , (int)width, (int)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, inputTextureData);
free(inputTextureData);
return inputTexName;
}
- (BOOL)setupContext {
EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES2;
if (_context == NULL) {
_context = [[EAGLContext alloc] initWithAPI:api];
if (!_context) {
NSLog(@"Failed to initialize OpenGLES 2.0 context");
return NO;
}
}
if (![EAGLContext setCurrentContext:_context]) {
NSLog(@"Failed to set current OpenGL context");
return NO;
}
return YES;
}
- (void)setupFrameBuffer {
GLuint framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
- (void)setupRenderBuffer {
glGenRenderbuffers(1, &_colorRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8_OES, LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE, LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderBuffer);
}
- (void)setupDepthBuffer {
glGenRenderbuffers(1, &_depthRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE , LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, _depthRenderBuffer);
}
- (void)setupVBOs {
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}
- (BOOL)setupOpenGLForDominantColor
{
GLuint vertShader, fragShader;
// Create and compile vertex shader.
if (![self compileShader:&vertShader type:GL_VERTEX_SHADER string:kDominantVertexShaderString]) {
LELog(@"Failed to compile vertex shader");
return NO;
}
// Create and compile fragment shader.
if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER string:kDominantFragmentShaderString]) {
LELog(@"Failed to compile fragment shader");
return NO;
}
// Create shader program.
_program = glCreateProgram();
// Attach vertex shader to program.
glAttachShader(_program, vertShader);
// Attach fragment shader to program.
glAttachShader(_program, fragShader);
// Link program.
if (![self linkProgram:_program]) {
LELog(@"Failed to link program: %d", _program);
if (vertShader) {
glDeleteShader(vertShader);
vertShader = 0;
}
if (fragShader) {
glDeleteShader(fragShader);
fragShader = 0;
}
if (_program) {
glDeleteProgram(_program);
_program = 0;
}
return NO;
}
glUseProgram(_program);
//Get attributes locations
_positionSlot = glGetAttribLocation(_program, "Position");
_colorSlot = glGetAttribLocation(_program, "SourceColor");
_texCoordSlot = glGetAttribLocation(_program, "TexCoordIn");
glEnableVertexAttribArray(_positionSlot);
glEnableVertexAttribArray(_colorSlot);
glEnableVertexAttribArray(_texCoordSlot);
_textureUniform = glGetUniformLocation(_program, "Texture");
_proccesedWidthSlot = glGetUniformLocation(_program, "ProccesedWidth");
_totalWidthSlot = glGetUniformLocation(_program, "TotalWidth");
return YES;
}
#pragma mark - OpenGL ES 2 shader compilation
- (BOOL)compileShader:(GLuint *)shader type:(GLenum)type string:(NSString *)string
{
GLint status;
const GLchar *source;
source = (GLchar*)[string UTF8String];
if (!source) {
LELog(@"Failed to load vertex shader");
return NO;
}
*shader = glCreateShader(type);
glShaderSource(*shader, 1, &source, NULL);
glCompileShader(*shader);
#ifdef LE_DEBUG
GLint logLength;
glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength);
glGetShaderInfoLog(*shader, logLength, &logLength, log);
NSLog(@"Shader compile log:\n%s", log);
free(log);
}
#endif
glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
if (status == 0) {
glDeleteShader(*shader);
return NO;
}
return YES;
}
- (BOOL)linkProgram:(GLuint)prog
{
GLint status;
glLinkProgram(prog);
#ifdef LE_DEBUG
GLint logLength;
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(prog, logLength, &logLength, log);
NSLog(@"Program link log:\n%s", log);
free(log);
}
#endif
glGetProgramiv(prog, GL_LINK_STATUS, &status);
if (status == 0) {
return NO;
}
return YES;
}
- (BOOL)validateProgram:(GLuint)prog
{
GLint logLength, status;
glValidateProgram(prog);
glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(prog, logLength, &logLength, log);
LELog(@"Program validate log:\n%s", log);
free(log);
}
glGetProgramiv(prog, GL_VALIDATE_STATUS, &status);
if (status == 0) {
return NO;
}
return YES;
}
#pragma mark - Convert GL image to UIImage
-(UIImage *)dumpImageWithWidth:(NSUInteger)width height:(NSUInteger)height biggestAlphaColorReturn:(UIColor**)returnColor
{
GLubyte *buffer = (GLubyte *) malloc(width * height * 4);
glReadPixels(0, 0, (int)width, (int)height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)buffer);
/* Find bigger Alpha color*/
NSUInteger biggerR = 0;
NSUInteger biggerG = 0;
NSUInteger biggerB = 0;
NSUInteger biggerAlpha = 0;
for (NSUInteger y=0; y<(height/2); y++) {
for (NSUInteger x=0; x<(width/2)*4; x++) {
if ((!((x+1)%4)) && (x>0)) {
if (buffer[y * 4 * width + x] > biggerAlpha ) {
biggerAlpha = buffer[y * 4 * width + x];
biggerR = buffer[y * 4 * width + (x-3)];
biggerG = buffer[y * 4 * width + (x-2)];
biggerB = buffer[y * 4 * width + (x-1)];
}
}
}
}
*returnColor = [UIColor colorWithRed:biggerR/255.0
green:biggerG/255.0
blue:biggerB/255.0
alpha:1.0];
// make data provider from buffer
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, width * height * 4, freeImageData);
// set up for CGImage creation
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * (int)width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
// Use this to retain alpha
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// make UIImage from CGImage
UIImage *newUIImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
return newUIImage;
}
-(UIColor *)colorWithBiggerCountFromImageWidth:(NSUInteger)width height:(NSUInteger)height
{
GLubyte *buffer = (GLubyte *) malloc(width * height * 4);
glReadPixels(0, 0, (int)width, (int)height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)buffer);
/* Find bigger Alpha color*/
NSUInteger biggerR = 0;
NSUInteger biggerG = 0;
NSUInteger biggerB = 0;
NSUInteger biggerAlpha = 0;
for (NSUInteger y=0; y<(height/2); y++) {
for (NSUInteger x=0; x<(width/2)*4; x++) {
if ((!((x+1)%4)) && (x>0)) {
if (buffer[y * 4 * width + x] > biggerAlpha ) {
biggerAlpha = buffer[y * 4 * width + x];
biggerR = buffer[y * 4 * width + (x-3)];
biggerG = buffer[y * 4 * width + (x-2)];
biggerB = buffer[y * 4 * width + (x-1)];
}
}
}
}
free(buffer);
return [UIColor colorWithRed:biggerR/255.0
green:biggerG/255.0
blue:biggerB/255.0
alpha:1.0];
}
-(UIColor *)colorFromImageWithWidth:(NSUInteger)width
height:(NSUInteger)height
filteringColor:(UIColor*)colorToFilter
tolerance:(GLfloat)tolerance
{
GLubyte *buffer = (GLubyte *) malloc(width * height * 4);
glReadPixels(0, 0, (int)width, (int)height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)buffer);
/* Find bigger Alpha color*/
NSUInteger biggerR = 0;
NSUInteger biggerG = 0;
NSUInteger biggerB = 0;
NSUInteger biggerAlpha = 0;
CGFloat filteringRedFloat = 0;
CGFloat filteringGreenFloat = 0;
CGFloat filteringBlueFloat = 0;
[colorToFilter getRed:&filteringRedFloat
green:&filteringGreenFloat
blue:&filteringBlueFloat
alpha:nil];
NSUInteger filteringRed = (NSUInteger)(filteringRedFloat*255);
NSUInteger filteringGreen = (NSUInteger)(filteringGreenFloat*255);
NSUInteger filteringBlue = (NSUInteger)(filteringBlueFloat*255);
for (NSUInteger y=0; y<(height/2); y++) {
for (NSUInteger x=0; x<(width/2)*4; x++) {
if ((!((x+1)%4)) && (x>0)) {
NSUInteger currentRed = buffer[y * 4 * width + (x-3)];
NSUInteger currentGreen = buffer[y * 4 * width + (x-2)];
NSUInteger currentBlue = buffer[y * 4 * width + (x-1)];
NSUInteger squareDistance = (currentRed-filteringRed)*(currentRed-filteringRed)+
(currentGreen-filteringGreen)*(currentGreen-filteringGreen)+
(currentBlue-filteringBlue)*(currentBlue-filteringBlue);
NSUInteger thresholdSquareDistance = (255*tolerance)*(255*tolerance);
if (squareDistance > thresholdSquareDistance) {
if (buffer[y * 4 * width + x] > biggerAlpha ) {
biggerAlpha = buffer[y * 4 * width + x];
biggerR = buffer[y * 4 * width + (x-3)];
biggerG = buffer[y * 4 * width + (x-2)];
biggerB = buffer[y * 4 * width + (x-1)];
}
}
}
}
}
free(buffer);
return [UIColor colorWithRed:biggerR/255.0
green:biggerG/255.0
blue:biggerB/255.0
alpha:1.0];
}
-(void)findTextColorsTaskForColorScheme:(LEColorScheme*)colorScheme
{
//Set sizes for buffer index calculations
int width = LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE;
int height = LECOLORPICKER_GPU_DEFAULT_SCALED_SIZE;
//Read Render buffer
GLubyte *buffer = (GLubyte *) malloc(width * height * 4);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid *)buffer);
//Set initials values for local variables
NSUInteger primaryColorR = 0;
NSUInteger primaryColorG = 0;
NSUInteger primaryColorB = 0;
NSUInteger primaryColorAlpha = 0;
CGFloat backgroundRedFloat = 0;
CGFloat backgroundGreenFloat = 0;
CGFloat backgroundBlueFloat = 0;
[colorScheme.backgroundColor getRed:&backgroundRedFloat
green:&backgroundGreenFloat
blue:&backgroundBlueFloat
alpha:nil];
LEColor backgroundColor = {(unsigned int)(backgroundRedFloat*255),
(unsigned int)(backgroundGreenFloat*255),
(unsigned int)(backgroundBlueFloat*255)};
for (NSUInteger y=0; y<(height/2); y++) {
for (NSUInteger x=0; x<(width/2)*4; x++) {
if ((!((x+1)%4)) && (x>0)) {
NSUInteger currentRed = buffer[y * 4 * width + (x-3)];
NSUInteger currentGreen = buffer[y * 4 * width + (x-2)];
NSUInteger currentBlue = buffer[y * 4 * width + (x-1)];
LEColor currentColor = {currentRed,currentGreen,currentBlue};
NSUInteger squareDistance = squareDistanceInRGBSpaceBetweenColor(currentColor, backgroundColor);
NSUInteger thresholdSquareDistance = (255*LECOLORPICKER_BACKGROUND_FILTER_TOLERANCE)*(255*LECOLORPICKER_BACKGROUND_FILTER_TOLERANCE);
if (squareDistance > thresholdSquareDistance) {
if (buffer[y * 4 * width + x] > primaryColorAlpha ) {
primaryColorAlpha = buffer[y * 4 * width + x];
primaryColorR = buffer[y * 4 * width + (x-3)];
primaryColorG = buffer[y * 4 * width + (x-2)];
primaryColorB = buffer[y * 4 * width + (x-1)];
}
}
}
}
}
UIColor *tmpColor = [UIColor colorWithRed:primaryColorR/255.0
green:primaryColorG/255.0
blue:primaryColorB/255.0
alpha:1.0];
colorScheme.primaryTextColor = tmpColor;
NSUInteger secondaryColorR = 0;
NSUInteger secondaryColorG = 0;
NSUInteger secondaryColorB = 0;
NSUInteger secondaryColorAlpha = 0;
LEColor primaryTextColor = {primaryColorR,primaryColorG,primaryColorB};
for (NSUInteger y=0; y<(height/2); y++) {
for (NSUInteger x=0; x<(width/2)*4; x++) {
if ((!((x+1)%4)) && (x>0)) {
NSUInteger currentRed = buffer[y * 4 * width + (x-3)];
NSUInteger currentGreen = buffer[y * 4 * width + (x-2)];
NSUInteger currentBlue = buffer[y * 4 * width + (x-1)];
LEColor currentColor = {currentRed,currentGreen,currentBlue};
NSUInteger squareDistanceToBackground = squareDistanceInRGBSpaceBetweenColor(currentColor, backgroundColor);
NSUInteger squareDistanceToPrimary = squareDistanceInRGBSpaceBetweenColor(currentColor, primaryTextColor);
NSUInteger thresholdSquareDistanceToBackground = (255*LECOLORPICKER_BACKGROUND_FILTER_TOLERANCE)*(255*LECOLORPICKER_BACKGROUND_FILTER_TOLERANCE);
NSUInteger thresholdSquareDistanceToPrimary = (255*LECOLORPICKER_PRIMARY_TEXT_FILTER_TOLERANCE)*(255*LECOLORPICKER_PRIMARY_TEXT_FILTER_TOLERANCE);
if ((squareDistanceToBackground > thresholdSquareDistanceToBackground) && (squareDistanceToPrimary > thresholdSquareDistanceToPrimary)) {
if (buffer[y * 4 * width + x] > secondaryColorAlpha ) {
secondaryColorAlpha = buffer[y * 4 * width + x];
secondaryColorR = buffer[y * 4 * width + (x-3)];
secondaryColorG = buffer[y * 4 * width + (x-2)];
secondaryColorB = buffer[y * 4 * width + (x-1)];
}
}
}
}
}
tmpColor = [UIColor colorWithRed:secondaryColorR/255.0
green:secondaryColorG/255.0
blue:secondaryColorB/255.0
alpha:1.0];
if ([self isSufficienteContrastBetweenBackground:colorScheme.backgroundColor
andForground:tmpColor]) {
colorScheme.secondaryTextColor = tmpColor;
} else {
NSLog(@"colorSchemeBG: %@", colorScheme.backgroundColor);
if ([UIColor yComponentFromColor:colorScheme.backgroundColor] < 0.5) {
colorScheme.secondaryTextColor = [UIColor whiteColor];
} else {
colorScheme.secondaryTextColor = [UIColor blackColor];
}
}
free(buffer);
}
- (UIImage*)scaleImage:(UIImage*)image width:(CGFloat)width height:(CGFloat)height
{
UIImage *scaledImage = [self imageWithImage:image scaledToSize:CGSizeMake(width,height)];
return scaledImage;
}
- (BOOL)isSufficienteContrastBetweenBackground:(UIColor*)backgroundColor andForground:(UIColor*)foregroundColor
{
NSLog(@"isSuff bg: %@, fg: %@", backgroundColor, foregroundColor);
float backgroundColorBrightness = [UIColor yComponentFromColor:backgroundColor];
float foregroundColorBrightness = [UIColor yComponentFromColor:foregroundColor];
float brightnessDifference = fabsf(backgroundColorBrightness-foregroundColorBrightness)*255;
LELog(@"BrightnessDifference %f ",brightnessDifference);
if (brightnessDifference>=LECOLORPICKER_DEFAULT_BRIGHTNESS_DIFFERENCE) {
float backgroundRed = 0.0;
float backgroundGreen = 0.0;
float backgroundBlue = 0.0;
float foregroundRed = 0.0;
float foregroundGreen = 0.0;
float foregroundBlue = 0.0;
size_t numComponents = CGColorGetNumberOfComponents(backgroundColor.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(backgroundColor.CGColor);
backgroundRed = components[0];
backgroundGreen = components[1];
backgroundBlue = components[2];
}
numComponents = CGColorGetNumberOfComponents(foregroundColor.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(foregroundColor.CGColor);
foregroundRed = components[0];
foregroundGreen = components[1];
foregroundBlue = components[2];
}
//Compute "Color Diference"
float colorDifference = (MAX(backgroundRed,foregroundRed)-MIN(backgroundRed, foregroundRed)) +
(MAX(backgroundGreen,foregroundGreen)-MIN(backgroundGreen, foregroundGreen)) +
(MAX(backgroundBlue,foregroundBlue)-MIN(backgroundBlue, foregroundBlue));
LELog(@"ColorDifference = %f",colorDifference*255);
if ((colorDifference*255)>LECOLORPICKER_DEFAULT_COLOR_DIFFERENCE) {
return YES;
}
}
return NO;
}
#pragma mark - UIImage utilities
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark - Multitasking and Background aware
- (void)addNotificationObservers
{
NSLog(@"LEColorPicker adding");
// Add observers for notification to respond at app state changes.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(appDidEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
- (void)dealloc {
//Remove all observers
NSLog(@"LEColorPicker dealloc");
//[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
//[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)appDidEnterBackground
{
NSLog(@"LEColorPicker appDidEnterBackground");
dispatch_suspend(taskQueue);
glFinish();
}
- (void)appDidEnterForeground
{
NSLog(@"LEColorPicker appDidEnterForeground");
dispatch_resume(taskQueue);
}
- (BOOL)isAppActive