Repository navigation
Expand file tree
/
Copy pathWDColorSlider.m
More file actions
375 lines (304 loc) · 10.1 KB
/
Copy pathWDColorSlider.m
File metadata and controls
375 lines (304 loc) · 10.1 KB
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
//
// WDColorSlider.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2008-2013 Steve Sprang
//
#import "WDColorSlider.h"
#import "WDUtilities.h"
#import "WDColorIndicator.h"
#import "WDColor.h"
#import "UIView+Additions.h"
#define kCornerRadius 10
#define kIndicatorInset 10
@interface WDColorSlider (Private)
- (CGImageRef) p_hueImage;
- (void) p_buildHueImage;
- (void) positionIndicator_;
@end
static void evaluateShading(void *info, const CGFloat *in, CGFloat *out)
{
WDColorSlider *slider = (__bridge WDColorSlider *) info;
WDColor *color = slider.color;
CGFloat blend = in[0];
float hue, saturation, brightness;
float r1 = 0, g1 = 0, b1 = 0;
float r2 = 0, g2 = 0, b2 = 0;
float r = 0, g = 0, b = 0;
BOOL blendRGB = YES;
hue = color.hue;
saturation = color.saturation;
brightness = color.brightness;
if (slider.mode == WDColorSliderModeAlpha) {
r = color.red; g = color.green; b = color.blue;
blendRGB = NO;
} else if (slider.mode == WDColorSliderModeBrightness) {
HSVtoRGB(hue, saturation, 0.0, &r1, &g1, &b1);
HSVtoRGB(hue, saturation, 1.0, &r2, &g2, &b2);
} else if (slider.mode == WDColorSliderModeSaturation) {
HSVtoRGB(hue, 0.0, brightness, &r1, &g1, &b1);
HSVtoRGB(hue, 1.0, brightness, &r2, &g2, &b2);
} else if (slider.mode == WDColorSliderModeRed) {
r1 = 0; r2 = 1;
g1 = g2 = color.green;
b1 = b2 = color.blue;
} else if (slider.mode == WDColorSliderModeGreen) {
r1 = r2 = color.red;
g1 = 0; g2 = 1;
b1 = b2 = color.blue;
} else if (slider.mode == WDColorSliderModeBlue) {
r1 = r2 = color.red;
g1 = g2 = color.green;
b1 = 0; b2 = 1;
} else if (slider.mode == WDColorSliderModeRedBalance) {
r1 = 0; r2 = 1;
g1 = 1; g2 = 0;
b1 = 1; b2 = 0;
} else if (slider.mode == WDColorSliderModeGreenBalance) {
r1 = 1; r2 = 0;
g1 = 0; g2 = 1;
b1 = 1; b2 = 0;
} else if (slider.mode == WDColorSliderModeBlueBalance) {
r1 = 1; r2 = 0;
g1 = 1; g2 = 0;
b1 = 0; b2 = 1;
}
if (blendRGB) {
r = (blend * r2) + (1.0f - blend) * r1;
g = (blend * g2) + (1.0f - blend) * g1;
b = (blend * b2) + (1.0f - blend) * b1;
}
out[0] = r;
out[1] = g;
out[2] = b;
out[3] = (slider.mode == WDColorSliderModeAlpha ? in[0] : 1.0f);
}
static void release(void *info) {
}
@implementation WDColorSlider
@synthesize mode = mode_;
@synthesize floatValue = value_;
@synthesize color = color_;
@synthesize reversed = reversed_;
@synthesize indicator = indicator_;
- (void) awakeFromNib
{
indicator_ = [WDColorIndicator colorIndicator];
indicator_.sharpCenter = WDCenterOfRect([self bounds]);
[self addSubview:indicator_];
self.opaque = NO;
self.backgroundColor = nil;
self.clearsContextBeforeDrawing = YES;
}
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect bounds = CGRectInset(self.bounds, -10, -10);
return CGRectContainsPoint(bounds, point);
}
- (void) dealloc
{
if (hueImage_) {
CGImageRelease(hueImage_);
}
if (shadingRef_) {
CGShadingRelease(shadingRef_);
}
}
- (CGShadingRef) newShadingRef
{
CGShadingRef gradient;
CGFloat domain[] = {0.0f, 1.0f};
CGFloat range[] = {0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f};
CGFunctionCallbacks callbacks;
callbacks.version = 0;
callbacks.evaluate = evaluateShading;
callbacks.releaseInfo = release;
CGPoint start = CGPointMake(0.0, 10.0f);
CGPoint end = CGPointMake(CGRectGetWidth(self.frame), 10.0f);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFunctionRef gradientFunction = CGFunctionCreate((__bridge void *)(self), 1, domain, 4, range, &callbacks);
if (self.reversed) {
gradient = CGShadingCreateAxial(colorspace, end, start, gradientFunction, NO, NO);
} else {
gradient = CGShadingCreateAxial(colorspace, start, end, gradientFunction, NO, NO);
}
CGFunctionRelease(gradientFunction);
CGColorSpaceRelease(colorspace);
return gradient;
}
- (CGShadingRef) shadingRef
{
if (!shadingRef_) {
shadingRef_ = [self newShadingRef];
}
return shadingRef_;
}
- (void) setColor:(WDColor *)color
{
switch (mode_) {
case WDColorSliderModeAlpha:
value_ = [color alpha];
break;
case WDColorSliderModeHue:
value_ = [color hue];
break;
case WDColorSliderModeBrightness:
value_ = [color brightness];
break;
case WDColorSliderModeSaturation:
value_ = [color saturation];
break;
case WDColorSliderModeRed:
case WDColorSliderModeRedBalance:
value_ = [color red];
break;
case WDColorSliderModeGreen:
case WDColorSliderModeGreenBalance:
value_ = [color green];
break;
case WDColorSliderModeBlue:
case WDColorSliderModeBlueBalance:
value_ = [color blue];
break;
default: break;
}
if (!color_ || color.hue != color_.hue || color.saturation != color_.saturation || color.brightness != color_.brightness) {
if (mode_ != WDColorSliderModeHue && shadingRef_) {
CGShadingRelease(shadingRef_);
shadingRef_ = NULL;
}
[self setNeedsDisplay];
}
color_ = color;
[self positionIndicator_];
if (self.reversed) {
[indicator_ setColor:[color colorWithAlphaComponent:(1.0f - color.alpha)]];
} else {
if (mode_ == WDColorSliderModeHue) {
color = [WDColor colorWithHue:color.hue saturation:1 brightness:1 alpha:1];
}
[indicator_ setColor:color];
}
}
- (void) setMode:(WDColorSliderMode)mode
{
mode_ = mode;
indicator_.alphaMode = (mode == WDColorSliderModeAlpha);
if (mode_ != WDColorSliderModeHue && shadingRef_) {
CGShadingRelease(shadingRef_);
shadingRef_ = NULL;
}
[self setNeedsDisplay];
}
- (void) setReversed:(BOOL)reversed
{
reversed_ = reversed;
[self setNeedsDisplay];
}
- (UIImage *) borderImage
{
static UIImage *borderImage = nil;
if (borderImage && !CGSizeEqualToSize(borderImage.size, self.bounds.size)) {
borderImage = nil;
}
if (!borderImage) {
borderImage = [UIImage imageNamed:@"slider_border.png"];
borderImage = [borderImage stretchableImageWithLeftCapWidth:16 topCapHeight:0];
UIGraphicsBeginImageContextWithOptions([self bounds].size, NO, 0);
[borderImage drawInRect:[self bounds]];
borderImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return borderImage;
}
- (void) drawRect:(CGRect)clip
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
CGContextSaveGState(ctx);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:8];
[path addClip];
if (mode_ == WDColorSliderModeAlpha) {
WDDrawCheckersInRect(ctx, bounds, 8);
}
if (mode_ == WDColorSliderModeHue) {
CGContextDrawImage(ctx, self.bounds, [self p_hueImage]);
} else {
CGContextDrawShading(ctx, [self shadingRef]);
}
CGContextRestoreGState(ctx);
[[self borderImage] drawInRect:bounds blendMode:kCGBlendModeMultiply alpha:0.333f];
}
- (float) indicatorCenterX_
{
CGRect trackRect = CGRectInset(self.bounds, kIndicatorInset, 0);
return roundf(value_ * CGRectGetWidth(trackRect) + CGRectGetMinX(trackRect));
}
- (void) computeValue_:(CGPoint)pt
{
CGRect trackRect = CGRectInset(self.bounds, kIndicatorInset, 0);
float percentage;
percentage = (pt.x - CGRectGetMinX(trackRect)) / CGRectGetWidth(trackRect);
percentage = WDClamp(0.0f, 1.0f, percentage);
value_ = percentage;
[self setNeedsDisplay];
}
- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint pt = [touch locationInView:self];
[self computeValue_:pt];
[self positionIndicator_];
return [super beginTrackingWithTouch:touch withEvent:event];
}
- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint pt = [touch locationInView:self];
[self computeValue_:pt];
[self positionIndicator_];
return [super continueTrackingWithTouch:touch withEvent:event];
}
@end
@implementation WDColorSlider (Private)
- (CGImageRef) p_hueImage
{
if (!hueImage_) {
[self p_buildHueImage];
}
return hueImage_;
}
- (void) p_buildHueImage
{
int x, y;
float r,g,b;
int width = CGRectGetWidth(self.bounds);
int height = CGRectGetHeight(self.bounds);
int bpr = width * 4;
UInt8 *data, *ptr;
ptr = data = calloc(1, sizeof(unsigned char) * height * bpr);
for (x = 0; x < width; x++) {
float angle = ((float) x) / width;
HSVtoRGB(angle, 1.0f, 1.0f, &r, &g, &b);
for (y = 0; y < height; y++) {
ptr[y * bpr + x*4] = 255;
ptr[y * bpr + x*4+1] = r * 255;
ptr[y * bpr + x*4+2] = g * 255;
ptr[y * bpr + x*4+3] = b * 255;
}
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, bpr, colorSpace, kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace);
hueImage_ = CGBitmapContextCreateImage(ctx);
// clean up
free(data);
CGContextRelease(ctx);
}
- (void) positionIndicator_
{
indicator_.sharpCenter = CGPointMake([self indicatorCenterX_], indicator_.center.y);
}
@end