/
PFColorAlert.mm
executable file
·376 lines (283 loc) · 14.7 KB
/
PFColorAlert.mm
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
#import "PFColorAlert.h"
#import "PFHaloHueView.h"
#import "PFColorLitePreviewView.h"
#import "PFColorLiteSlider.h"
#import "UIColor+PFColor.h"
#import <objc/runtime.h>
@interface PFColorAlertViewController : UIViewController
@end
@interface PFColorAlert() <PFHaloHueViewDelegate>
- (PFColorAlert *)init;
@property (nonatomic, retain) PFHaloHueView *haloView;
@property (nonatomic, retain) PFColorAlertViewController *mainViewController;
@property (nonatomic, retain) UIView *blurView;
@property (nonatomic, retain) UIButton *hexButton;
@property (nonatomic, retain) UIWindow *darkeningWindow;
@property (nonatomic, retain) PFColorLiteSlider *brightnessSlider;
@property (nonatomic, retain) PFColorLiteSlider *saturationSlider;
@property (nonatomic, retain) PFColorLiteSlider *alphaSlider;
@property (nonatomic, retain) PFColorLitePreviewView *litePreviewView;
@property (nonatomic, assign) BOOL isOpen;
@property (nonatomic, copy) void (^completionBlock)(UIColor *pickedColor);
@end
@implementation PFColorAlertViewController
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
@end
@implementation PFColorAlert
@synthesize popWindow;
@synthesize haloView;
@synthesize mainViewController;
@synthesize blurView;
@synthesize hexButton;
@synthesize darkeningWindow;
@synthesize brightnessSlider;
@synthesize saturationSlider;
@synthesize alphaSlider;
@synthesize litePreviewView;
@synthesize completionBlock;
- (PFColorAlert *)init
{
[super init];
self.isOpen = NO;
UIColor *startColor = [UIColor whiteColor];
self.darkeningWindow = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
self.darkeningWindow.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
CGRect winFrame = [UIScreen mainScreen].bounds;
winFrame.origin.x = (winFrame.size.width * 0.09f) / 2;
winFrame.origin.y = (winFrame.size.height * 0.09f) / 2;
winFrame.size.width = winFrame.size.width - (winFrame.size.width * 0.09f);
winFrame.size.height = winFrame.size.height - (winFrame.size.height * 0.09f);
self.popWindow = [[[UIWindow alloc] initWithFrame:winFrame] autorelease];
self.popWindow.layer.masksToBounds = true;
self.popWindow.layer.cornerRadius = 15;
self.mainViewController = [[[PFColorAlertViewController alloc] init] autorelease];
self.mainViewController.view.frame = CGRectMake(0, 0, winFrame.size.width, winFrame.size.height);
const CGRect mainFrame = self.mainViewController.view.frame;
Class blurCls;
if (objc_getClass("_UIBackdropView")) blurCls = objc_getClass("_UIBackdropView");
else blurCls = [UIView class];
if (blurCls != [UIView class])
{
NSObject *backSettings = [objc_getClass("_UIBackdropViewSettings") settingsForStyle:2010];
self.blurView = (UIView *)[[[blurCls alloc] initWithFrame:CGRectZero autosizesToFitSuperview:YES settings:backSettings] autorelease];
}
else
{
// UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
// self.blurView = [[[UIVisualEffectView alloc] initWithEffect:blurEffect] autorelease];
// self.blurView.frame = CGRectMake(0, 0, mainFrame.size.width, mainFrame.size.height);
self.blurView = [[[blurCls alloc] initWithFrame:mainFrame] autorelease];
self.blurView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9f];
}
[self.blurView removeFromSuperview]; // <-- not sure why you're doing this but okay ¯\_(ツ)_/¯
[self.mainViewController.view addSubview:self.blurView];
CGRect haloViewFrame = CGRectMake((mainFrame.size.width / 2) - (((mainFrame.size.width / 3) * 2) / 2), (mainFrame.size.width / 6),
(mainFrame.size.width / 3) * 2, (mainFrame.size.width / 3) * 2);
self.haloView =
// HUE HARDCODED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[[[PFHaloHueView alloc] initWithFrame:haloViewFrame minValue:0 maxValue:1 value:startColor.hue delegate:self] autorelease];
// [self.haloView makeReadyForDisplay];
[self.haloView removeFromSuperview];
[self.mainViewController.view addSubview:self.haloView];
const CGRect sliderFrame = CGRectMake((mainFrame.size.width / 2) - (((mainFrame.size.width / 3) * 2) / 2),
haloViewFrame.origin.y + haloViewFrame.size.height, (mainFrame.size.width / 3) * 2, 40);
self.saturationSlider = [[[PFColorLiteSlider alloc] initWithFrame:sliderFrame color:startColor style:PFSliderBackgroundStyleSaturation] autorelease];
[self.mainViewController.view addSubview:self.saturationSlider];
CGRect brightnessSliderFrame = sliderFrame;
brightnessSliderFrame.origin.y = brightnessSliderFrame.origin.y + brightnessSliderFrame.size.height;
self.brightnessSlider = [[[PFColorLiteSlider alloc] initWithFrame:brightnessSliderFrame color:startColor style:PFSliderBackgroundStyleBrightness] autorelease];
[self.mainViewController.view addSubview:self.brightnessSlider];
CGRect alphaSliderFrame = brightnessSliderFrame;
alphaSliderFrame.origin.y = alphaSliderFrame.origin.y + alphaSliderFrame.size.height;
self.alphaSlider = [[[PFColorLiteSlider alloc] initWithFrame:alphaSliderFrame color:startColor style:PFSliderBackgroundStyleAlpha] autorelease];
[self.mainViewController.view addSubview:self.alphaSlider];
self.alphaSlider.hidden = NO; // defaulting to NO
self.hexButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.hexButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.hexButton addTarget:self action:@selector(chooseHexColor) forControlEvents:UIControlEventTouchUpInside];
[self.hexButton setTitle:@"#" forState:UIControlStateNormal];
self.hexButton.frame = CGRectMake(self.mainViewController.view.frame.size.width - (25 + 10), 10, 25, 25);
[self.mainViewController.view addSubview:self.hexButton];
CGRect litePreviewViewFrame = CGRectMake((mainFrame.size.width / 2) - (((mainFrame.size.width / 6) * 2) / 2),
(((haloViewFrame.origin.y) + (haloViewFrame.size.height)) - (((mainFrame.size.width / 6) * 2)) - ((mainFrame.size.width / 6))),
(mainFrame.size.width / 6) * 2, (mainFrame.size.width / 6) * 2);
self.litePreviewView = [[[PFColorLitePreviewView alloc] initWithFrame:litePreviewViewFrame // HUE HARDCODED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mainColor:[UIColor colorWithHue:startColor.hue saturation:startColor.saturation brightness:startColor.brightness alpha:startColor.alpha] previousColor: startColor] autorelease];
[self.litePreviewView removeFromSuperview]; // why even xD adding view1 to view2 automatically removes view1 from its prior superview
[self.mainViewController.view addSubview:self.litePreviewView];
self.darkeningWindow.hidden = NO;
self.darkeningWindow.alpha = 0.0f;
[self.darkeningWindow makeKeyAndVisible];
self.popWindow.rootViewController = self.mainViewController;
self.popWindow.windowLevel = UIWindowLevelAlert - 1.0f;
self.popWindow.backgroundColor = [UIColor clearColor];
self.popWindow.hidden = NO;
self.popWindow.alpha = 0.0f;
[self makeViewDynamic:self.popWindow];
CGRect popWindowFrame = self.popWindow.frame;
popWindowFrame.origin.y = ([UIScreen mainScreen].bounds.size.height - popWindowFrame.size.height) / 2;
self.popWindow.frame = popWindowFrame;
[self.saturationSlider.slider addTarget:self action:@selector(saturationChanged:) forControlEvents:UIControlEventValueChanged];
[self.brightnessSlider.slider addTarget:self action:@selector(brightnessChanged:) forControlEvents:UIControlEventValueChanged];
[self.alphaSlider.slider addTarget:self action:@selector(alphaChanged:) forControlEvents:UIControlEventValueChanged];
[self setPrimaryColor:startColor];
return self;
}
- (PFColorAlert *)initWithStartColor:(UIColor *)startColor showAlpha:(BOOL)showAlpha
{
[self init];
[self.haloView setValue:startColor.hue];
[self.saturationSlider updateGraphicsWithColor:startColor];
[self.brightnessSlider updateGraphicsWithColor:startColor];
[self.alphaSlider updateGraphicsWithColor:startColor];
[self.litePreviewView setMainColor:[UIColor colorWithHue:startColor.hue saturation:startColor.saturation brightness:startColor.brightness alpha:startColor.alpha] previousColor:startColor];
[self setPrimaryColor:startColor];
self.alphaSlider.hidden = showAlpha ? 0 : 1; // invert logic
return self;
}
- (void)makeViewDynamic:(UIView *)view
{
CGRect dynamicFrame = view.frame;
if (!self.alphaSlider.hidden)
dynamicFrame.size.height =
self.alphaSlider.frame.origin.y + (self.mainViewController.view.frame.size.width / 6) + self.alphaSlider.frame.size.height;
else
dynamicFrame.size.height =
self.brightnessSlider.frame.origin.y + (self.mainViewController.view.frame.size.width / 6) + self.brightnessSlider.frame.size.height;
view.frame = dynamicFrame;
}
+ (PFColorAlert *)colorAlertWithStartColor:(UIColor *)startColor showAlpha:(BOOL)showAlpha
{
return [[[PFColorAlert alloc] initWithStartColor:startColor showAlpha:showAlpha] autorelease];
}
- (void)displayWithCompletion:(void (^)(UIColor *pickedColor))fcompletionBlock
{
if (self.isOpen) return;
self.completionBlock = fcompletionBlock;
[self retain];
[self.popWindow makeKeyAndVisible];
[UIView animateWithDuration:0.3f animations:^{
self.darkeningWindow.alpha = 1.0f;
self.popWindow.alpha = 1.0f;
} completion:^(BOOL finished) {
self.isOpen = YES;
UITapGestureRecognizer *tgr = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)] autorelease];
self.darkeningWindow.userInteractionEnabled = YES;
[self.darkeningWindow addGestureRecognizer:tgr];
}];
}
- (void)showWithStartColor:(UIColor *)startColor showAlpha:(BOOL)showAlpha completion:(void (^)(UIColor *pickedColor))fcompletionBlock
{
UIAlertView *deprecated = [[UIAlertView alloc] initWithTitle:@"libColorPicker" message:@"Hey! It appears like this preference bundle is trying to use deprecated methods to invoke the color picker and requires an update. Please inform the dev of this tweak about it." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[deprecated show];
[deprecated release];
}
- (void)setPrimaryColor:(UIColor *)primary
{
//UIColor *pr = [primary retain];
[self.litePreviewView updateWithColor:primary];
[self.saturationSlider updateGraphicsWithColor:primary];
[self.brightnessSlider updateGraphicsWithColor:primary];
[self.alphaSlider updateGraphicsWithColor:primary];
[self.haloView setValue:primary.hue];
}
- (void)hueChanged:(float)hue
{
[self.litePreviewView updateWithColor:
[UIColor colorWithHue:hue saturation:self.litePreviewView.mainColor.saturation brightness:self.litePreviewView.mainColor.brightness alpha:self.litePreviewView.mainColor.alpha]];
[self.saturationSlider updateGraphicsWithColor:
[UIColor colorWithHue:hue saturation:self.litePreviewView.mainColor.saturation brightness:self.litePreviewView.mainColor.brightness alpha:self.litePreviewView.mainColor.alpha]];
[self.brightnessSlider updateGraphicsWithColor:
[UIColor colorWithHue:hue saturation:self.litePreviewView.mainColor.saturation brightness:self.litePreviewView.mainColor.brightness alpha:self.litePreviewView.mainColor.alpha]];
[self.alphaSlider updateGraphicsWithColor:
[UIColor colorWithHue:hue saturation:self.litePreviewView.mainColor.saturation brightness:self.litePreviewView.mainColor.brightness alpha:self.litePreviewView.mainColor.alpha]];
}
- (void)saturationChanged:(UISlider *)_slider
{
[self.litePreviewView updateWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:_slider.value brightness:self.litePreviewView.mainColor.brightness alpha:self.litePreviewView.mainColor.alpha]];
[self.saturationSlider updateGraphicsWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:_slider.value brightness:self.litePreviewView.mainColor.brightness alpha:self.litePreviewView.mainColor.alpha]];
[self.alphaSlider updateGraphicsWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:_slider.value brightness:self.litePreviewView.mainColor.brightness alpha:self.litePreviewView.mainColor.alpha]];
}
- (void)brightnessChanged:(UISlider *)_slider
{
[self.litePreviewView updateWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:self.litePreviewView.mainColor.saturation brightness:_slider.value alpha:self.litePreviewView.mainColor.alpha]];
[self.brightnessSlider updateGraphicsWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:self.litePreviewView.mainColor.saturation brightness:_slider.value alpha:self.litePreviewView.mainColor.alpha]];
[self.alphaSlider updateGraphicsWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:self.litePreviewView.mainColor.saturation brightness:_slider.value alpha:self.litePreviewView.mainColor.alpha]];
}
- (void)alphaChanged:(UISlider *)_slider
{
[self.litePreviewView updateWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:self.litePreviewView.mainColor.saturation brightness:self.litePreviewView.mainColor.brightness alpha:_slider.value]];
[self.alphaSlider updateGraphicsWithColor:
[UIColor colorWithHue:self.litePreviewView.mainColor.hue saturation:self.litePreviewView.mainColor.saturation brightness:self.litePreviewView.mainColor.brightness alpha:_slider.value]];
}
- (void)chooseHexColor
{
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Hex Color" message:@"Enter a hex color or copy it to your pasteboard." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:@"Set", @"Copy", nil];
prompt.delegate = self;
[prompt setAlertViewStyle:UIAlertViewStylePlainTextInput];
[[prompt textFieldAtIndex:0] setText:[UIColor hexFromColor:self.litePreviewView.mainColor]];
[prompt show];
[prompt release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1)
{
if ([[alertView textFieldAtIndex:0].text hasPrefix:@"#"] && [UIColor PF_colorWithHex:[alertView textFieldAtIndex:0].text])
{
[self setPrimaryColor:[UIColor PF_colorWithHex:[alertView textFieldAtIndex:0].text]];
}
}
else if (buttonIndex == 2)
{
[[UIPasteboard generalPasteboard] setString:[UIColor hexFromColor:self.litePreviewView.mainColor]];
}
}
- (void)close
{
if (!self.isOpen) return;
[UIView animateWithDuration:0.3f animations:^{
self.darkeningWindow.alpha = 0.0f;
self.popWindow.alpha = 0.0f;
} completion:^(BOOL finished) {
if (self.completionBlock)
self.completionBlock(self.litePreviewView.mainColor);
self.popWindow.hidden = YES;
self.isOpen = NO;
[self release];
}];
}
- (void)dealloc
{
self.popWindow = nil;
self.haloView = nil;
self.mainViewController = nil;
self.blurView = nil;
self.hexButton = nil;
self.darkeningWindow = nil;
self.brightnessSlider = nil;
self.saturationSlider = nil;
self.alphaSlider = nil;
self.litePreviewView = nil;
self.completionBlock = nil;
[super dealloc];
}
@end