Home > Xcode solutions > Color Picker library for iPhone

Color Picker library for iPhone

October 15th, 2010 Leave a comment Go to comments

When I created my Pie Chart Creator, I needed a class to display available colors and return clicked color after user selected color.
I created nice library – it may require some adjustments which I will add soon, but I want to share it now with others. Please remember, that it is based on new BSD license, which is explained in a source code and here
http://creativecommons.org/licenses/BSD/

//
// ColorPicker.h
//
// Created by ORPI.pl on 10-10-14.
// Copyright 2010 ORPI.pl, http://iphone.orpi.pl . All rights reserved.
//
// Using this library you agree to New BSD license http://creativecommons.org/licenses/BSD/
//
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Zend Technologies USA, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#import

@class ColorPicker;

@protocol ColorPickerDelegate

@required
- (void)colorPicker:(ColorPicker *)colorPicker didSelectItemAtIndex:(NSNumber *)indexPath;

@end

@interface ColorPicker : UIView {

NSArray *myColors;
int selectedItem;

CGFloat initialXPosition;
CGFloat initialYPosition;
CGFloat beginPositionX;
CGFloat beginPositionY;

NSMutableArray *normalisedChartValues;

}

@property(nonatomic,assign) id delegate;
@property (nonatomic,retain) NSArray *myColors;
@property (nonatomic) int selectedItem;

- (void)tappedItemId:(CGPoint)pos;

@end

//
// ColorPicker.m
//
// Created by ORPI.pl on 10-10-14.
// Copyright 2010 ORPI.pl, http://iphone.orpi.pl . All rights reserved.
//
// Using this library you agree to New BSD license http://creativecommons.org/licenses/BSD/
//
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of Zend Technologies USA, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#import "ColorPicker.h"
#define myNumber(x) [NSNumber numberWithFloat:x]
#define MINXMOVE 1
#define MINYMOVE 1
#define MAXXMOVE 7
#define MAXYMOVE 7

@implementation ColorPicker
@synthesize selectedItem;
@synthesize delegate;
@synthesize myColors;

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Initialization code

}
return self;
}

- (void)drawRect:(CGRect)rect {
// Drawing code

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);

CGContextSetLineWidth(ctx, 0.3);
CGContextSetStrokeColor(ctx, CGColorGetComponents([[UIColor colorWithHue:0.1 saturation:0.1 brightness:0.1 alpha:1.0] CGColor]));
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor whiteColor] CGColor]));
CGContextFillRect(ctx, rect);

int y=3;
int initx=7;
int x=6;
int rectSize=25;
int space=5;
CGContextSetLineWidth(ctx, 1.5);
NSMutableArray *chartValues=[NSMutableArray arrayWithCapacity:[myColors count]];

for (int i=0; i<[myColors count]; i++) { UIColor *fillColor=[UIColor colorWithHue:[[myColors objectAtIndex:i] floatValue] saturation:1 brightness:1 alpha:1.0]; CGContextSetFillColor(ctx, CGColorGetComponents([fillColor CGColor])); if (i%5==0 && i!=0) { y=y+rectSize+space; x=initx; } CGRect colorRect= CGRectMake(x, y, rectSize, rectSize); CGContextFillRect(ctx, colorRect); NSString *colorRectString=NSStringFromCGRect(colorRect); [chartValues addObject:colorRectString]; if (i==selectedItem) { CGContextStrokeRect(ctx, CGRectMake(x, y, rectSize, rectSize)); } x=x+rectSize+space; } normalisedChartValues=[chartValues retain]; } #pragma mark -- #pragma mark touches - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *allTouches = [event allTouches]; switch ([allTouches count]) { case 1: { CGPoint pos = [[[allTouches allObjects] objectAtIndex:0] locationInView:self]; beginPositionX=pos.x; beginPositionY=pos.y; } break; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *allTouches = [event allTouches]; switch ([allTouches count]) { case 1: { CGPoint pos = [[[allTouches allObjects] objectAtIndex:0] locationInView:self]; if (beginPositionX-pos.x

Tags: