-
Notifications
You must be signed in to change notification settings - Fork 186
/
UIColor+ImageGetColor.m
191 lines (130 loc) · 4.75 KB
/
UIColor+ImageGetColor.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
//
// UIColor+ImageGetColor.m
// HyPopMenuView
//
// Created by H y on 15/9/8.
// Copyright (c) 2015年 ouy.Aberi. All rights reserved.
//
#import "UIColor+ImageGetColor.h"
@implementation UIView (GetImgae)
- (UIImage*)imageRepresentation
{
int width = CGRectGetWidth([self bounds]);
int height = CGRectGetHeight([self bounds]);
NSInteger myDataLength = width * height * 4;
// allocate array and read pixels into it.
GLubyte* buffer = (GLubyte*)malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte* buffer2 = (GLubyte*)malloc(myDataLength);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width * 4; x++) {
buffer2[(height - 1 - y) * width * 4 + x] = buffer[y * 4 * width + x];
}
}
// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
// make the cgimage
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// then make the uiimage from that
UIImage* myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}
@end
@implementation UIColor (ImageGetColor)
+ (UIColor*)getPixelColorAtLocation:(CGPoint)point inImage:(UIImage*)image
{
UIColor* color = nil;
CGImageRef inImage = image.CGImage;
CGContextRef cgctx = [self createARGBBitmapContextFromImage:
inImage];
if (cgctx == NULL) {
return nil; /* error */
}
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = { { 0, 0 }, { w, h } };
CGContextDrawImage(cgctx, rect, inImage);
unsigned char* data = CGBitmapContextGetData(cgctx);
if (data != NULL) {
int offset = 4 * ((w * round(point.y)) + round(point.x));
int alpha = data[offset];
int red = data[offset + 1];
int green = data[offset + 2];
int blue = data[offset + 3];
NSLog(@"offset: %i colors: RGB A %i %i %i %i", offset, red, green,
blue, alpha);
NSLog(@"x:%f y:%f", point.x, point.y);
color = [UIColor colorWithRed:(red / 255.0f) green:(green / 255.0f) blue:
(blue / 255.0f)
alpha:(alpha / 255.0f)];
}
CGContextRelease(cgctx);
if (data) {
free(data);
}
return color;
}
+ (CGContextRef)createARGBBitmapContextFromImage:(CGImageRef)inImage
{
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
void* bitmapData;
unsigned long bitmapByteCount;
unsigned long bitmapBytesPerRow;
size_t pixelsWide = CGImageGetWidth(inImage);
size_t pixelsHigh = CGImageGetHeight(inImage);
bitmapBytesPerRow = (pixelsWide * 4);
bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
colorSpace = CGColorSpaceCreateDeviceRGB();
if (colorSpace == NULL)
{
fprintf(stderr, "Error allocating color space\n");
return NULL;
}
bitmapData = malloc(bitmapByteCount);
if (bitmapData == NULL)
{
fprintf(stderr, "Memory not allocated!");
CGColorSpaceRelease(colorSpace);
return NULL;
}
context = CGBitmapContextCreate(bitmapData,
pixelsWide,
pixelsHigh,
8,
bitmapBytesPerRow,
colorSpace,
(CGBitmapInfo)kCGImageAlphaPremultipliedFirst);
if (context == NULL)
{
free(bitmapData);
fprintf(stderr, "Context not created!");
}
CGColorSpaceRelease(colorSpace);
return context;
}
@end
@implementation UIImage (Tint)
- (UIImage*)imageWithTintColor:(UIColor*)tintColor
{
//We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
//Draw the tinted image in context
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
UIImage* tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
@end