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
/
Copy pathPFColorAlertViewController.xm
executable file
·283 lines (234 loc) · 12.5 KB
/
PFColorAlertViewController.xm
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
#import "PFColorAlertViewController.h"
#import "PFHaloHueView.h"
#import "PFColorLitePreviewView.h"
#import "PFColorLiteSlider.h"
#import "UIColor+PFColor.h"
@interface PFColorAlertViewController () <PFHaloHueViewDelegate> {
PFHaloHueView *_haloView;
UIView *_blurView;
UIButton *_hexButton;
PFColorLiteSlider *_brightnessSlider;
PFColorLiteSlider *_saturationSlider;
PFColorLiteSlider *_alphaSlider;
PFColorLitePreviewView *_litePreviewView;
UIColor *_tintColor;
}
@end
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 100000
typedef enum UIUserInterfaceStyle : NSInteger {
UIUserInterfaceStyleUnspecified,
UIUserInterfaceStyleLight,
UIUserInterfaceStyleDark
} UIUserInterfaceStyle;
#endif
@interface UITraitCollection (iOS12_13)
@property (nonatomic, readonly) UIUserInterfaceStyle userInterfaceStyle;
@end
@interface _UIBackdropViewSettings : NSObject
+ (id)settingsForStyle:(long long)style;
@end
@interface _UIBackdropView : UIView
- (id)initWithFrame:(CGRect)frame
autosizesToFitSuperview:(BOOL)fitsSuperview
settings:(_UIBackdropViewSettings *)settings;
@end
@implementation PFColorAlertViewController
- (id)initWithViewFrame:(CGRect)frame startColor:(UIColor *)startColor showAlpha:(BOOL)showAlpha {
self = [super init];
_tintColor = UIColor.blackColor;
self.view.frame = frame;
if (%c(_UIBackdropView)) {
int style = 2010;
if (@available(iOS 13, *)) {
if ([UIScreen mainScreen].traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
_tintColor = UIColor.whiteColor;
style = 1100;
}
}
_UIBackdropViewSettings *backSettings = [%c(_UIBackdropViewSettings) settingsForStyle:style];
_blurView = [[%c(_UIBackdropView) alloc] initWithFrame:CGRectZero
autosizesToFitSuperview:YES
settings:backSettings];
} else {
_blurView = [[UIView alloc] initWithFrame:frame];
_blurView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9f];
}
[self.view addSubview:_blurView];
float padding = frame.size.width / 8;
float width = frame.size.width - padding * 2;
CGRect haloViewFrame = CGRectMake(padding, padding, width, width);
_haloView = [[PFHaloHueView alloc] initWithFrame:haloViewFrame
minValue:0
maxValue:1
value:startColor.hue
tintColor:_tintColor
delegate:self];
[self.view addSubview:_haloView];
CGRect sliderFrame = CGRectMake(padding,
haloViewFrame.origin.y + haloViewFrame.size.height + padding / 2,
width,
40);
_saturationSlider = [[PFColorLiteSlider alloc] initWithFrame:sliderFrame
color:startColor
tintColor:_tintColor
style:PFSliderBackgroundStyleSaturation];
[self.view addSubview:_saturationSlider];
sliderFrame.origin.y = sliderFrame.origin.y + sliderFrame.size.height;
_brightnessSlider = [[PFColorLiteSlider alloc] initWithFrame:sliderFrame
color:startColor
tintColor:_tintColor
style:PFSliderBackgroundStyleBrightness];
[self.view addSubview:_brightnessSlider];
sliderFrame.origin.y = sliderFrame.origin.y + sliderFrame.size.height;
_alphaSlider = [[PFColorLiteSlider alloc] initWithFrame:sliderFrame
color:startColor
tintColor:_tintColor
style:PFSliderBackgroundStyleAlpha];
[self.view addSubview:_alphaSlider];
_hexButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_hexButton setTitleColor:_tintColor forState:UIControlStateNormal];
[_hexButton addTarget:self action:@selector(chooseHexColor) forControlEvents:UIControlEventTouchUpInside];
[_hexButton setTitle:@"#" forState:UIControlStateNormal];
_hexButton.frame = CGRectMake(self.view.frame.size.width - (25 + 10), 10, 25, 25);
[self.view addSubview:_hexButton];
float halfWidth = frame.size.width / 2;
float previewPadding = padding * 2;
float previewWidth = previewPadding * 2;
CGRect litePreviewViewFrame = CGRectMake(halfWidth - previewPadding,
haloViewFrame.origin.y + haloViewFrame.size.height / 2 - previewWidth / 2,
previewWidth,
previewWidth);
_litePreviewView = [[PFColorLitePreviewView alloc] initWithFrame:litePreviewViewFrame
tintColor:_tintColor
mainColor:startColor
previousColor:startColor];
[self.view insertSubview:_litePreviewView belowSubview:_haloView];
[_saturationSlider.slider addTarget:self action:@selector(saturationChanged:) forControlEvents:UIControlEventValueChanged];
[_brightnessSlider.slider addTarget:self action:@selector(brightnessChanged:) forControlEvents:UIControlEventValueChanged];
[_alphaSlider.slider addTarget:self action:@selector(alphaChanged:) forControlEvents:UIControlEventValueChanged];
_alphaSlider.hidden = !showAlpha;
return self;
}
- (float)topMostSliderLastYCoordinate {
UIView *firstVisibleSlider = !_alphaSlider.hidden ? _alphaSlider : _brightnessSlider;
return firstVisibleSlider.frame.origin.y + firstVisibleSlider.frame.size.height;
}
- (void)setPrimaryColor:(UIColor *)primary {
[_litePreviewView updateWithColor:primary];
[_saturationSlider updateGraphicsWithColor:primary];
[_brightnessSlider updateGraphicsWithColor:primary];
[_alphaSlider updateGraphicsWithColor:primary];
[_haloView setValue:primary.hue];
}
- (UIColor *)getColor {
return _litePreviewView.mainColor;
}
- (void)hueChanged:(float)hue {
UIColor *color = [UIColor colorWithHue:hue saturation:_litePreviewView.mainColor.saturation brightness:_litePreviewView.mainColor.brightness alpha:_litePreviewView.mainColor.alpha];
[_litePreviewView updateWithColor:color];
[_saturationSlider updateGraphicsWithColor:color hue:hue];
[_brightnessSlider updateGraphicsWithColor:color hue:hue];
[_alphaSlider updateGraphicsWithColor:color hue:hue];
}
- (void)saturationChanged:(UISlider *)slider {
UIColor *color = [UIColor colorWithHue:_litePreviewView.mainColor.hue saturation:slider.value brightness:_litePreviewView.mainColor.brightness alpha:_litePreviewView.mainColor.alpha];
[_litePreviewView updateWithColor:color];
[_saturationSlider updateGraphicsWithColor:color];
[_alphaSlider updateGraphicsWithColor:color];
}
- (void)brightnessChanged:(UISlider *)slider {
UIColor *color = [UIColor colorWithHue:_litePreviewView.mainColor.hue saturation:_litePreviewView.mainColor.saturation brightness:slider.value alpha:_litePreviewView.mainColor.alpha];
[_litePreviewView updateWithColor:color];
[_brightnessSlider updateGraphicsWithColor:color];
[_alphaSlider updateGraphicsWithColor:color];
}
- (void)alphaChanged:(UISlider *)slider {
UIColor *color = [UIColor colorWithHue:_litePreviewView.mainColor.hue saturation:_litePreviewView.mainColor.saturation brightness:_litePreviewView.mainColor.brightness alpha:slider.value];
[_litePreviewView updateWithColor:color];
[_alphaSlider updateGraphicsWithColor:color];
}
- (void)chooseHexColor {
NSString *title = @"Hex Color";
NSString *message = @"Enter a hex color or copy it to your pasteboard.";
NSString *cancelText = @"Close";
NSString *setText = @"Set";
NSString *copyText = @"Copy";
if (%c(UIAlertController)) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"#123456";
}];
// Set
[alertController addAction:[UIAlertAction actionWithTitle:setText
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
NSString *text = alertController.textFields.firstObject.text;
UIColor *color;
if ([text hasPrefix:@"#"] && (color = [UIColor PF_colorWithHex:text]))
[self setPrimaryColor:color];
}]];
// Copy
[alertController addAction:[UIAlertAction actionWithTitle:copyText
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[[UIPasteboard generalPasteboard] setString:[UIColor hexFromColor:[self getColor]]];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:cancelText
style:UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:cancelText
otherButtonTitles:setText, copyText, nil];
prompt.delegate = self;
[prompt setAlertViewStyle:UIAlertViewStylePlainTextInput];
[[prompt textFieldAtIndex:0] setText:[UIColor hexFromColor:[self getColor]]];
[prompt show];
#pragma clang diagnostic pop
}
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
NSString *text = [alertView textFieldAtIndex:0].text;
UIColor *color;
if ([text hasPrefix:@"#"] && (color = [UIColor PF_colorWithHex:text]))
[self setPrimaryColor:color];
} else if (buttonIndex == 2) {
[[UIPasteboard generalPasteboard] setString:[UIColor hexFromColor:[self getColor]]];
}
}
#pragma clang diagnostic pop
- (void)presentPasteHexStringQuestion:(NSString *)pasteboard {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Detected pasted color"
message:@"It seems like your pasteboard consists of a hex color. Would you like to use it?"
preferredStyle:UIAlertControllerStyleAlert];
// Set from hex color
[alertController addAction:[UIAlertAction actionWithTitle:@"Use pasteboard"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
[self setPrimaryColor:[UIColor PF_colorWithHex:pasteboard]];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"No"
style:UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
@end