-
Notifications
You must be signed in to change notification settings - Fork 2
/
BPColorPickerViewController.m
153 lines (122 loc) · 3.26 KB
/
BPColorPickerViewController.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
//
// BPColorPickerViewController.m
// Skates
//
// Created by Jon Olson on 2/4/10.
// Copyright 2010 Ballistic Pigeon, LLC. All rights reserved.
//
#import "BPColorPickerViewController.h"
#import "BPColorWell.h"
#import "BPHueSliderView.h"
#import "BPLuminanceSaturationView.h"
@interface BPColorPickerViewController (Private)
- (void)updatePickedColor;
@end
@implementation BPColorPickerViewController
#pragma mark -
#pragma mark Construction and deallocation
- (id)init {
if (self = [super initWithNibName:@"BPColorPickerViewController" bundle:nil]) {
self.title = @"Color Picker";
}
return self;
}
- (void)dealloc {
[colorWell release];
[hueSliderView release];
[luminanceSaturationView release];
[pickedColor release];
[super dealloc];
}
#pragma mark -
#pragma mark Accessors
@synthesize colorWell, hueSliderView, luminanceSaturationView;
@synthesize delegate;
@synthesize pickedColor;
- (void)setPickedColor:(UIColor *)aColor {
if (aColor != pickedColor) {
[pickedColor release];
pickedColor = [aColor retain];
[self updatePickedColor];
}
}
#pragma mark -
#pragma mark View loading and unloading
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
- (void)viewDidLoad {
[super viewDidLoad];
[self updatePickedColor];
}
- (void)viewDidUnload {
self.colorWell = nil;
self.hueSliderView = nil;
self.luminanceSaturationView = nil;
}
#pragma mark -
#pragma mark Setting a new color externally
- (void)updatePickedColor {
if (!hueSliderView || !colorWell || !luminanceSaturationView)
return;
if (!pickedColor) {
self.pickedColor = [UIColor colorWithHue:0.0 saturation:1.0 brightness:1.0 alpha:1.0];
return;
}
const CGFloat *c = CGColorGetComponents(pickedColor.CGColor);
// Colors come in RGB. We want them in HSV. Do some converstion work per Wikipedia :)
NSUInteger min, max;
if (c[0] >= c[1] && c[0] >= c[2])
max = 0;
else if (c[1] >= c[0] && c[1] >= c[2])
max = 1;
else
max = 2;
if (c[0] <= c[1] && c[0] <= c[2])
min = 0;
else if (c[1] <= c[0] && c[1] <= c[2])
min = 1;
else
min = 2;
CGFloat h;
CGFloat scale = c[max] - c[min];
if (min == max)
h = 0.0;
else if (max == 0) {
h = 60.0 * ((c[1] - c[2]) / scale) + 360.0;
if (h > 360.0)
h -= 360.0;
}
else if (max == 1)
h = 60.0 * ((c[2] - c[0]) / scale) + 120.0;
else
h = 60.0 * ((c[0] - c[1]) / scale) + 240.0;
h /= 360.0;
CGFloat s = c[max] == 0.0 ? 0.0 : 1.0 - (c[min] / c[max]);
CGFloat v = c[max];
colorWell.color = pickedColor;
hueSliderView.selectedHue = h;
luminanceSaturationView.saturation = s;
luminanceSaturationView.luminance = v;
}
#pragma mark -
#pragma mark Sending color updates
- (void)updatedColor {
[pickedColor release];
pickedColor = [[UIColor colorWithHue:self.hueSliderView.selectedHue saturation:self.luminanceSaturationView.saturation brightness:self.luminanceSaturationView.luminance alpha:1.0] retain];
self.colorWell.color = pickedColor;
[delegate colorPicker:self didSelectColor:pickedColor];
}
#pragma mark -
#pragma mark UI actions
- (IBAction)updateHue:(id)sender {
BPHueSliderView *slider = sender;
luminanceSaturationView.baseHue = slider.selectedHue;
[self updatedColor];
}
- (IBAction)updateLuminanceAndSaturation:(id)sender {
[self updatedColor];
}
@end