This repository was archived by the owner on Dec 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPFColorViewController.xm
More file actions
executable file
·474 lines (383 loc) · 17.7 KB
/
PFColorViewController.xm
File metadata and controls
executable file
·474 lines (383 loc) · 17.7 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
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
//
// ColorViewController.m
// UIColors
//
// Created by Bailey Seymour on 1/23/14.
//
//
#import "PSSpecifier.h"
@interface UIViewController ()
- (id)initForContentSize:(CGSize)size;
@end
#if TARGET_OS_IPHONE
#import "PFColorViewController.h"
#import "PFColorPicker.h"
#import "UIColor+PFColor.h"
#import "PFColorTransparentView.h"
#import <objc/runtime.h>
@interface UIPushedView : UIView
@end
@implementation UIPushedView
- (void)setFrame:(CGRect)frame {
frame.origin.y = 64;
if (self.superview)
frame.size.height = self.superview.frame.size.height - 64;
[super setFrame:frame];
}
@end
@interface PFColorViewController () <PFColorPickerDelegate, UIAlertViewDelegate> {
UIColor *_loadedColor;
UIView *_backdrop;
PFColorTransparentView *_transparent;
CGSize _size;
// HSB
UISlider *_hueSlider;
UISlider *_saturationSlider;
UISlider *_brightnessSlider;
UISlider *_alphaSlider;
UIView *_controlsContainer;
UIBarButtonItem *_hexButton;
CGFloat _currentAlpha;
UIPushedView *_pushedView;
}
@property (nonatomic, retain) PFColorPicker *colorPicker;
@end
@implementation PFColorViewController
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
UIColor *colorFromDefaultsWithKey(NSString *defaults, NSString *key, NSString *fallback);
#ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
}
#endif
- (UIColor *)colorFromDefaults:(NSString *)def withKey:(NSString *)key {
UIColor *color = colorFromDefaultsWithKey(def, key, self.fallback);
_currentAlpha = color.alpha;
_alphaSlider.value = color.alpha;
return color;
}
#define isiPhone4 ([[UIScreen mainScreen] bounds].size.height == 480)
- (id)initForContentSize:(CGSize)size {
if ([%c(PSViewController) instancesRespondToSelector:@selector(initForContentSize:)])
self = [super initForContentSize:size];
else
self = [super init];
_size = size;
_pushedView = [[UIPushedView alloc] initWithFrame:CGRectMake(0, 64, size.width, size.height - 64)];
_pushedView.alpha = 0;
[self.view addSubview:_pushedView];
return self;
}
- (id)initForContentSize:(CGSize)size
defaults:(NSString *)cdefaults
key:(NSString *)ckey
usesRGB:(BOOL)cusesRGB
usesAlpha:(BOOL)cusesAlpha
postNotification:(NSString *)cpostNotification
fallback:(NSString *)cfallback {
self = [self initForContentSize:size];
self.defaults = cdefaults;
self.key = ckey;
self.usesRGB = cusesRGB;
self.usesAlpha = cusesAlpha;
self.postNotification = cpostNotification;
self.fallback = cfallback;
return self;
}
- (void)loadCustomViews {
_pushedView.frame = CGRectMake(0, 20 + 44, _size.width, _size.height - 64);
_currentAlpha = 1;
if (_transparent)
[_transparent setFrame:_pushedView.frame];
else
_transparent = [[PFColorTransparentView alloc] initWithFrame:_pushedView.frame];
if (!self.usesAlpha)
_transparent.hidden = YES;
CGFloat height = _pushedView.frame.size.height / 2;
if (isiPhone4)
height -= 40;
CGRect colorPickerFrame = CGRectMake(0, 0, _pushedView.frame.size.width, height);
if (self.colorPicker)
[self.colorPicker setFrame:colorPickerFrame];
else
self.colorPicker = [[PFColorPicker alloc] initWithFrame:colorPickerFrame];
[self.colorPicker makeReadyForDisplay];
[self.colorPicker setDelegate:self];
float controlsContainerHeight = self.usesAlpha ? 180 : 140;
CGRect controlsContainerFrame = CGRectMake(0,
_pushedView.frame.size.height - controlsContainerHeight,
self.colorPicker.frame.size.width,
controlsContainerHeight);
if (_controlsContainer)
[_controlsContainer setFrame:controlsContainerFrame];
else
_controlsContainer = [[UIView alloc] initWithFrame:controlsContainerFrame];
float halfWidth = _controlsContainer.frame.size.width / 2;
CGPoint red = CGPointMake(halfWidth, 30);
CGPoint green = CGPointMake(halfWidth, red.y + 40);
CGPoint blue = CGPointMake(halfWidth, green.y + 40);
CGPoint alpha = CGPointMake(halfWidth, blue.y + 40);
CGRect sliderFrame = CGRectMake(halfWidth, 0, _controlsContainer.frame.size.width - 40, 20);
Class viewClass;
if (%c(UIBackdropView))
viewClass = %c(UIBackdropView);
else
viewClass = [UIView class];
CGRect backdropFrame = CGRectMake(0, 0, _controlsContainer.frame.size.width, _controlsContainer.frame.size.height);
if (_backdrop)
[_backdrop setFrame:backdropFrame];
else
_backdrop = [[viewClass alloc] initWithFrame:backdropFrame];
_hexButton = [[UIBarButtonItem alloc] initWithTitle:@"#"
style:UIBarButtonItemStylePlain
target:self
action:@selector(chooseHexColor)];
self.navigationItem.rightBarButtonItem = _hexButton;
if (viewClass == [UIView class])
[_backdrop setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.5f]];
if (_hueSlider)
[_hueSlider setFrame:sliderFrame];
else
_hueSlider = [[UISlider alloc] initWithFrame:sliderFrame];
[_hueSlider addTarget:self action:@selector(hueSliderChanged) forControlEvents:UIControlEventValueChanged];
[_hueSlider setCenter:red];
[_hueSlider setMaximumValue:1];
[_hueSlider setMinimumValue:0];
_hueSlider.continuous = YES;
if (_saturationSlider)
[_saturationSlider setFrame:sliderFrame];
else
_saturationSlider = [[UISlider alloc] initWithFrame:sliderFrame];
[_saturationSlider addTarget:self action:@selector(hueSliderChanged) forControlEvents:UIControlEventValueChanged];
[_saturationSlider setCenter:green];
[_saturationSlider setMaximumValue:1];
[_saturationSlider setMinimumValue:0];
_saturationSlider.continuous = YES;
if (_brightnessSlider)
[_brightnessSlider setFrame:sliderFrame];
else
_brightnessSlider = [[UISlider alloc] initWithFrame:sliderFrame];
[_brightnessSlider addTarget:self action:@selector(hueSliderChanged) forControlEvents:UIControlEventValueChanged];
[_brightnessSlider setCenter:blue];
[_brightnessSlider setMaximumValue:1];
[_brightnessSlider setMinimumValue:0];
_brightnessSlider.continuous = YES;
if (_alphaSlider)
[_alphaSlider setFrame:sliderFrame];
else
_alphaSlider = [[UISlider alloc] initWithFrame:sliderFrame];
[_alphaSlider addTarget:self action:@selector(hueSliderChanged) forControlEvents:UIControlEventValueChanged];
[_alphaSlider setCenter:alpha];
[_alphaSlider setMaximumValue:1];
[_alphaSlider setMinimumValue:0];
if (!self.usesAlpha)
_alphaSlider.hidden = YES;
// UIColor *loadColor = [self colorFromDefaults:self.defaults withKey:self.key];
// currentAlpha = loadColor.alpha;
// _alphaSlider.value = currentAlpha;
// [self pickedColor:loadColor];
_alphaSlider.continuous = YES;
if (self.usesRGB) {
// Tint For RGB
if (![_hueSlider respondsToSelector:@selector(setTintColor:)]) {
_hueSlider.minimumTrackTintColor = [UIColor redColor];
_saturationSlider.minimumTrackTintColor = [UIColor greenColor];
_brightnessSlider.minimumTrackTintColor = [UIColor blueColor];
_alphaSlider.minimumTrackTintColor = [UIColor grayColor];
} else {
// iOS 7
_hueSlider.tintColor = [UIColor redColor];
_saturationSlider.tintColor = [UIColor greenColor];
_brightnessSlider.tintColor = [UIColor blueColor];
_alphaSlider.tintColor = [UIColor grayColor];
}
[_hueSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'R'] forState:UIControlStateNormal];
[_saturationSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'G'] forState:UIControlStateNormal];
[_brightnessSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'B'] forState:UIControlStateNormal];
[_alphaSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'A'] forState:UIControlStateNormal];
[_alphaSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'A'] forState:UIControlStateHighlighted];
} else {
// Tint for HSB
UIColor *black = [UIColor blackColor];
UIColor *gray = [UIColor grayColor];
if (![_hueSlider respondsToSelector:@selector(setTintColor:)]) {
_hueSlider.minimumTrackTintColor = black;
_saturationSlider.minimumTrackTintColor = black;
_brightnessSlider.minimumTrackTintColor = black;
_alphaSlider.minimumTrackTintColor = gray;
} else {
// iOS 7
_hueSlider.tintColor = black;
_saturationSlider.tintColor = black;
_brightnessSlider.tintColor = black;
_alphaSlider.tintColor = gray;
}
[_hueSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'H'] forState:UIControlStateNormal];
[_saturationSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'S'] forState:UIControlStateNormal];
[_brightnessSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'B'] forState:UIControlStateNormal];
[_alphaSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'A'] forState:UIControlStateNormal];
[_alphaSlider setThumbImage:[PFColorViewController thumbImageWithColor:[UIColor whiteColor] letter:'A'] forState:UIControlStateHighlighted];
}
if (!_transparent.superview)
[_pushedView addSubview:_transparent];
if (!self.colorPicker.superview)
[_pushedView addSubview:self.colorPicker];
if (!_controlsContainer.superview)
[_pushedView addSubview:_controlsContainer];
if (!_backdrop.superview)
[_controlsContainer addSubview:_backdrop];
if (!_hueSlider.superview)
[_controlsContainer addSubview:_hueSlider];
if (!_saturationSlider.superview)
[_controlsContainer addSubview:_saturationSlider];
if (!_brightnessSlider.superview)
[_controlsContainer addSubview:_brightnessSlider];
if (!_alphaSlider.superview)
[_controlsContainer addSubview:_alphaSlider];
if (self.defaults && self.key) {
_loadedColor = [self colorFromDefaults:self.defaults withKey:self.key];
_currentAlpha = _loadedColor.alpha;
_alphaSlider.value = _currentAlpha;
[self pickedColor:_loadedColor];
}
[UIView animateWithDuration:0.2
delay:0
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
[_pushedView setAlpha:1];
}
completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (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:_pushedView.backgroundColor]];
[prompt show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([[alertView textFieldAtIndex:0].text hasPrefix:@"#"] && [UIColor PF_colorWithHex:[alertView textFieldAtIndex:0].text])
[self pickedColor:[UIColor PF_colorWithHex:[alertView textFieldAtIndex:0].text]];
} else if (buttonIndex == 2) {
[[UIPasteboard generalPasteboard] setString:[UIColor hexFromColor:_pushedView.backgroundColor]];
}
}
#pragma clang diagnostic pop
+ (UIImage *)thumbImageWithColor:(UIColor *)color letter:(unichar)letter {
CGFloat size = 28.0f;
CGRect rect = CGRectMake(0.0f, 0.0f, size + 3, size + 8);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGSize shadowSize = CGSizeMake(0, 3);
CGContextSetShadowWithColor(context, shadowSize, 4, [UIColor colorWithWhite:0 alpha:0.25f].CGColor);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextAddArc(context, rect.size.width / 2, rect.size.width / 2, size / 2, size / 2, 2 * M_PI, 1);
CGContextSetStrokeColorWithColor(context, [[UIColor PF_colorWithHex:@"#f0f0f0"] colorWithAlphaComponent:0].CGColor);
CGContextDrawPath(context, kCGPathFill);
CGContextSetShadow(context, CGSizeMake(0, 0), 0);
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1, -1);
CGContextSetRGBFillColor(context, 0.0f, 0.0f, 0.0f, 1.0f);
CGContextSetLineWidth(context, 2.0f);
CGContextSetCharacterSpacing(context, 1.7f);
CGContextSetTextDrawingMode(context, kCGTextFill);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CGContextSelectFont(context, "Helvetica Neue Bold", 15.0f, kCGEncodingMacRoman);
CGContextShowTextAtPoint(context, 10, 15, [NSString stringWithCharacters:&letter length:1].UTF8String, 1);
#pragma clang diagnostic pop
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self performSelector:@selector(loadCustomViews) withObject:nil afterDelay:0];
[self.colorPicker performSelector:@selector(saveCache) withObject:nil afterDelay:0.5f];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if (self.postNotification && _loadedColor != [self colorFromDefaults:self.defaults withKey:self.key]) {
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
(CFStringRef)self.postNotification,
NULL,
NULL,
YES);
}
}
- (void)hueSliderChanged {
UIColor *color;
if (!self.usesRGB)
color = [UIColor colorWithHue:_hueSlider.value
saturation:_saturationSlider.value
brightness:_brightnessSlider.value
alpha:_alphaSlider.value];
else
color = [UIColor colorWithRed:_hueSlider.value
green:_saturationSlider.value
blue:_brightnessSlider.value
alpha:_alphaSlider.value];
[self pickedColor:color];
}
- (void)pickedColor:(UIColor *)color {
[_pushedView setBackgroundColor:color];
[self.view setBackgroundColor:color];
if (!self.usesRGB) {
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
[color getHue:&hue saturation:&saturation brightness:&brightness alpha:NULL];
[_hueSlider setValue:hue];
[_saturationSlider setValue:saturation];
[_brightnessSlider setValue:brightness];
} else {
CGFloat red;
CGFloat green;
CGFloat blue;
[color getRed:&red green:&green blue:&blue alpha:NULL];
[_hueSlider setValue:red];
[_saturationSlider setValue:green];
[_brightnessSlider setValue:blue];
}
if (self.usesAlpha) {
_transparent.alpha = 1 - _alphaSlider.value;
_currentAlpha = _alphaSlider.value;
}
NSMutableDictionary *preferencesPlist = [NSMutableDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", self.defaults]];
if (!preferencesPlist)
preferencesPlist = [NSMutableDictionary new];
NSString *saveValue;
if (self.usesAlpha)
saveValue = [NSString stringWithFormat:@"%@:%f", [UIColor hexFromColor:color], _currentAlpha]; //should be something like @"#a1a1a1:0.5" with the the decimal being the alpha you can ge the color and alpha seperately by [value componentsSeparatedByString:@":"]
else
saveValue = [UIColor hexFromColor:color]; // should be something like @"#a1a1a1"
if (saveValue && self.key) {
[preferencesPlist setObject:saveValue forKey:self.key];
[preferencesPlist writeToFile:[NSString stringWithFormat:@"/var/mobile/Library/Preferences/%@.plist", self.defaults] atomically:YES];
CFPreferencesSetAppValue((CFStringRef)self.key,(CFStringRef)saveValue, (CFStringRef)self.defaults);
CFPreferencesAppSynchronize((CFStringRef)self.defaults);
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
[self.colorPicker saveCache];
_size = self.view.frame.size;
_size.height = _size.height - 20 - 44;
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[self loadCustomViews];
}
completion:nil];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
@end
#endif