-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorPickerController.m
171 lines (143 loc) · 6.78 KB
/
ColorPickerController.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
static float redColor = 0.157;
static float greenColor = 0.627;
static float blueColor = 0.627;
static float alphaVal = 1.0;
#import "ColorPickerController.h"
@interface ColorPickerController ()
@end
@implementation ColorPickerController
@synthesize stdColorsView = _stdColorsView;
@synthesize selectedColor = _selectedColor;
@synthesize showselectedColor = _showselectedColor;
@synthesize stdColorsArray = _stdColorsArray;
@synthesize redArray = _redArray;
@synthesize greenArray = _greenArray;
@synthesize blueArray = _blueArray;
@synthesize redSlider = _redSlider;
@synthesize greenSlider = _greenSlider;
@synthesize blueSlider = _blueSlider;
@synthesize alphaSlider = _alphaSlider;
@synthesize redValue = _redValue;
@synthesize greenValue = _greenValue;
@synthesize blueValue = _blueValue;
@synthesize alphaValue = _alphaValue;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
if (_selectedColor == NULL)
{
_redValue = redColor;
_blueValue = blueColor;
_greenValue = greenColor;
_alphaValue = alphaVal;
_stdColorsArray = [NSArray arrayWithObjects:
[ UIColor brownColor],[ UIColor blackColor], [ UIColor darkGrayColor],
[ UIColor grayColor], [ UIColor lightGrayColor], [ UIColor whiteColor],
[ UIColor colorWithRed:redColor green:greenColor blue:blueColor alpha:alphaVal], [ UIColor cyanColor], [ UIColor blueColor],
[ UIColor greenColor], [ UIColor yellowColor], [ UIColor orangeColor],
[ UIColor redColor], [ UIColor magentaColor], [ UIColor purpleColor], nil];
_redArray = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.6],[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.33],
[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.66],[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.157],[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:0.5],
nil];
_greenArray = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.4],[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.33],
[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.66],[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.627],[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:0.5],
[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.0],
nil];
_blueArray = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:0.2],[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.33],
[NSNumber numberWithFloat:0.5],[NSNumber numberWithFloat:0.66],[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.627],[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:1.0],
[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.0],
[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:1.0],[NSNumber numberWithFloat:0.5],
nil];
_selectedColor = [UIColor colorWithRed:_redValue green:_greenValue blue:_blueValue alpha:_alphaValue];
}
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_stdColorsView.backgroundColor = [UIColor clearColor];
[_stdColorsView reloadData];
// Do any additional setup after loading the view from its nib.
}
-(void) viewWillAppear:(BOOL)animated
{
if (_selectedColor != NULL)
{
[_showselectedColor setBackgroundColor:_selectedColor];
[_selectedColor getRed:&_redValue green:&_greenValue blue:&_blueValue alpha:&_alphaValue];
_redSlider.value = _redValue;
_blueSlider.value = _blueValue;
_greenSlider.value = _greenValue;
_alphaSlider.value = _alphaValue;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Collection View Data Source Methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 15;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"myColor";
[collectionView registerNib:[UINib nibWithNibName:@"imageView" bundle:Nil] forCellWithReuseIdentifier:cellIdentifier];
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0,0.0, 50.0, 50.0)];
[label setContentMode:UIViewContentModeScaleAspectFill];
[label setClipsToBounds:YES];
[label setBackgroundColor:[_stdColorsArray objectAtIndex:indexPath.row]];
[cell.contentView addSubview:label];
return cell;
}
// Collection View Delegate methods
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
_redSlider.value = [((NSNumber*)[_redArray objectAtIndex:indexPath.row]) floatValue];
_greenSlider.value = [((NSNumber*)[_greenArray objectAtIndex:indexPath.row]) floatValue];
_blueSlider.value = [((NSNumber*)[_blueArray objectAtIndex:indexPath.row]) floatValue];
_alphaSlider.value = 1.0;
_selectedColor = [UIColor colorWithRed:_redSlider.value green:_greenSlider.value blue:_blueSlider.value alpha:_alphaSlider.value];
_showselectedColor.backgroundColor = _selectedColor;
}
-(IBAction)changeColor:(id)sender
{
switch (((UISlider*)sender).tag)
{
case 1:
_redValue = ((UISlider*)sender).value;
break;
case 2:
_greenValue = ((UISlider*)sender).value;
break;
case 3:
_blueValue = ((UISlider*)sender).value;
break;
case 4:
_alphaValue = ((UISlider*)sender).value;
break;
default:
break;
}
_selectedColor = [UIColor colorWithRed:_redValue green:_greenValue blue:_blueValue alpha:_alphaValue];
_showselectedColor.backgroundColor= _selectedColor;
}
@end