forked from InfinitApps/InfColorPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfColorSquarePicker.m
More file actions
179 lines (129 loc) · 4.87 KB
/
Copy pathInfColorSquarePicker.m
File metadata and controls
179 lines (129 loc) · 4.87 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
//==============================================================================
//
// InfColorSquarePicker.m
// InfColorPicker
//
// Created by Troy Gaul on 8/9/10.
//
// Copyright (c) 2011 InfinitApps LLC - http://infinitapps.com
// Some rights reserved: http://opensource.org/licenses/MIT
//
//==============================================================================
#import "InfColorSquarePicker.h"
#import "InfColorIndicatorView.h"
#import "InfHSBSupport.h"
//------------------------------------------------------------------------------
#define kContentInsetX 20
#define kContentInsetY 20
#define kIndicatorSize 24
//==============================================================================
@implementation InfColorSquareView
//------------------------------------------------------------------------------
@synthesize hue;
//------------------------------------------------------------------------------
- (void) updateContent
{
CGImageRef imageRef = createSaturationBrightnessSquareContentImageWithHue( hue * 360 );
self.image = [ UIImage imageWithCGImage: imageRef ];
CGImageRelease( imageRef );
}
//------------------------------------------------------------------------------
#pragma mark Properties
//------------------------------------------------------------------------------
- (void) setHue: (float) value
{
if( value != hue || self.image == nil ) {
hue = value;
[ self updateContent ];
}
}
//------------------------------------------------------------------------------
@end
//==============================================================================
@implementation InfColorSquarePicker
//------------------------------------------------------------------------------
@synthesize hue;
@synthesize value;
//------------------------------------------------------------------------------
#pragma mark Lifetime
//------------------------------------------------------------------------------
- (void) dealloc
{
[ indicator release ];
[ super dealloc ];
}
//------------------------------------------------------------------------------
#pragma mark Appearance
//------------------------------------------------------------------------------
- (void) setIndicatorColor
{
if( indicator == nil )
return;
indicator.color = [ UIColor colorWithHue: hue saturation: value.x
brightness: value.y alpha: 1.0f ];
}
//------------------------------------------------------------------------------
- (void) layoutSubviews
{
if( indicator == nil ) {
CGRect indicatorRect = { CGPointZero, { kIndicatorSize, kIndicatorSize } };
indicator = [ [ InfColorIndicatorView alloc ] initWithFrame: indicatorRect ];
[ self addSubview: indicator ];
}
[ self setIndicatorColor ];
CGFloat indicatorX = kContentInsetX + ( self.value.x * ( self.bounds.size.width - 2 * kContentInsetX ) );
CGFloat indicatorY = self.bounds.size.height - kContentInsetY
- ( self.value.y * ( self.bounds.size.height - 2 * kContentInsetY ) );
indicator.center = CGPointMake( indicatorX, indicatorY );
}
//------------------------------------------------------------------------------
#pragma mark Properties
//------------------------------------------------------------------------------
- (void) setHue: (float) newValue
{
if( newValue != hue ) {
hue = newValue;
[ self setIndicatorColor ];
}
}
//------------------------------------------------------------------------------
- (void) setValue: (CGPoint) newValue
{
if( !CGPointEqualToPoint( newValue, value ) ) {
value = newValue;
[ self sendActionsForControlEvents: UIControlEventValueChanged ];
[ self setNeedsLayout ];
}
}
//------------------------------------------------------------------------------
#pragma mark Tracking
//------------------------------------------------------------------------------
- (void) trackIndicatorWithTouch: (UITouch*) touch
{
CGRect bounds = self.bounds;
CGPoint touchValue;
touchValue.x = ( [ touch locationInView: self ].x - kContentInsetX )
/ ( bounds.size.width - 2 * kContentInsetX );
touchValue.y = ( [ touch locationInView: self ].y - kContentInsetY )
/ ( bounds.size.height - 2 * kContentInsetY );
touchValue.x = pin( 0.0f, touchValue.x, 1.0f );
touchValue.y = 1.0f - pin( 0.0f, touchValue.y, 1.0f );
self.value = touchValue;
}
//------------------------------------------------------------------------------
- (BOOL) beginTrackingWithTouch: (UITouch*) touch
withEvent: (UIEvent*) event
{
[ self trackIndicatorWithTouch: touch ];
return YES;
}
//------------------------------------------------------------------------------
- (BOOL) continueTrackingWithTouch: (UITouch*) touch
withEvent: (UIEvent*) event
{
[ self trackIndicatorWithTouch: touch ];
return YES;
}
//------------------------------------------------------------------------------
@end
//==============================================================================