forked from InfinitApps/InfColorPicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfColorBarPicker.m
More file actions
141 lines (100 loc) · 3.86 KB
/
Copy pathInfColorBarPicker.m
File metadata and controls
141 lines (100 loc) · 3.86 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
//==============================================================================
//
// InfColorBarPicker.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 "InfColorBarPicker.h"
#import "InfColorIndicatorView.h"
#import "InfHSBSupport.h"
//------------------------------------------------------------------------------
#define kContentInsetX 20
//==============================================================================
@implementation InfColorBarView
//------------------------------------------------------------------------------
static CGImageRef createContentImage()
{
float hsv[] = { 0.0f, 1.0f, 1.0f };
return createHSVBarContentImage( InfComponentIndexHue, hsv );
}
//------------------------------------------------------------------------------
- (void) drawRect: (CGRect) rect
{
CGImageRef image = createContentImage();
if( image ) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage( context, [ self bounds ], image );
CGImageRelease( image );
}
}
//------------------------------------------------------------------------------
@end
//==============================================================================
@implementation InfColorBarPicker
//------------------------------------------------------------------------------
@synthesize value;
//------------------------------------------------------------------------------
#pragma mark Lifetime
//------------------------------------------------------------------------------
- (void) dealloc
{
[ indicator release ];
[ super dealloc ];
}
//------------------------------------------------------------------------------
#pragma mark Drawing
//------------------------------------------------------------------------------
- (void) layoutSubviews
{
if( indicator == nil ) {
CGFloat kIndicatorSize = 24.0f;
indicator = [ [ InfColorIndicatorView alloc ] initWithFrame: CGRectMake( 0, 0, kIndicatorSize, kIndicatorSize ) ];
[ self addSubview: indicator ];
}
indicator.color = [ UIColor colorWithHue: value saturation: 1.0f
brightness: 1.0f alpha: 1.0f ];
CGFloat indicatorLoc = kContentInsetX + ( self.value * ( self.bounds.size.width - 2 * kContentInsetX ) );
indicator.center = CGPointMake( indicatorLoc, CGRectGetMidY( self.bounds ) );
}
//------------------------------------------------------------------------------
#pragma mark Properties
//------------------------------------------------------------------------------
- (void) setValue: (float) newValue
{
if( newValue != value ) {
value = newValue;
[ self sendActionsForControlEvents: UIControlEventValueChanged ];
[ self setNeedsLayout ];
}
}
//------------------------------------------------------------------------------
#pragma mark Tracking
//------------------------------------------------------------------------------
- (void) trackIndicatorWithTouch: (UITouch*) touch
{
float percent = ( [ touch locationInView: self ].x - kContentInsetX )
/ ( self.bounds.size.width - 2 * kContentInsetX );
self.value = pin( 0.0f, percent, 1.0f );
}
//------------------------------------------------------------------------------
- (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
//==============================================================================