ColorPicker.h
//
// ColorPicker.m
// Created by Song on 6/2/09.
// Copyright 2009 Ultie. All rights reserved.
//
#import
@interface ColorPicker : UIView {
UIView *selectedColorBox;
SEL colorChangeSelector;
id colorChangeObject;
}
@property(nonatomic) SEL colorChangeSelector;
@property(nonatomic, retain) id colorChangeObject;
-(void) onColorChange: (SEL) selector withObject: (id) object;
@end
ColorPicker.m
//
// ColorPicker.m
// Created by Song on 6/2/09.
// Copyright 2009 Ultie. All rights reserved.
//
#import "ColorPicker.h"
@implementation ColorPicker
@synthesize colorChangeSelector;
@synthesize colorChangeObject;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
selectedColorBox = [[UIView alloc] initWithFrame: CGRectMake(0, -3, frame.size.height + 6, frame.size.height + 6)];
selectedColorBox.backgroundColor = [UIColor redColor];
[self addSubview: selectedColorBox];
}
return self;
}
- (void) fill: (float []) color withIndex: (int) i{
int colors[] = {
255, 0, 0,
255, 0, 255,
0, 0, 255,
0, 255, 255,
0, 255, 0,
255, 255, 0,
255, 0, 0
};
int colorCount = sizeof(colors) / 3 / sizeof(int);
int p = self.bounds.size.width / (colorCount - 1);
int r,g,b;
int ii = i / p;
int colorA[] = {colors[ii * 3], colors[ii * 3 + 1], colors[ii * 3 + 2]};
int colorB[] = {colors[(ii + 1) * 3], colors[(ii + 1) * 3 + 1], colors[(ii + 1) * 3 + 2]};
r = colorA[0] - (colorA[0] - colorB[0])/p * (i % p);
g = colorA[1] - (colorA[1] - colorB[1])/p * (i % p);
b = colorA[2] - (colorA[2] - colorB[2])/p * (i % p);
color[0] = r / 256.0;
color[1] = g / 256.0;
color[2] = b / 256.0;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 1);
int colors[] = {
255, 0, 0,
255, 0, 255,
0, 0, 255,
0, 255, 255,
0, 255, 0,
255, 255, 0,
255, 0, 0
};
int colorCount = sizeof(colors) / 3 / sizeof(int);
int p = rect.size.width / (colorCount - 1);
for(int i = 0; i < rect.size.width; i++){
int r,g,b;
int ii = i / p;
int colorA[] = {colors[ii * 3], colors[ii * 3 + 1], colors[ii * 3 + 2]};
int colorB[] = {colors[(ii + 1) * 3], colors[(ii + 1) * 3 + 1], colors[(ii + 1) * 3 + 2]};
r = colorA[0] - (colorA[0] - colorB[0])/p * (i % p);
g = colorA[1] - (colorA[1] - colorB[1])/p * (i % p);
b = colorA[2] - (colorA[2] - colorB[2])/p * (i % p);
//NSLog(@"cc=%d p=%d i=%d ii=%d r=%d g=%d b=%d", colorCount, p, i, ii, r, g, b);
CGContextSetRGBStrokeColor(context, r/256.0, g/256.0, b/256.0, 1.0);
CGPoint addLines[] = {
CGPointMake(i, 0),
CGPointMake(i, rect.size.height)
};
CGContextAddLines(context, addLines, 2);
CGContextStrokePath(context);
}
CGContextRestoreGState(context);
}
- (void)dealloc {
[selectedColorBox release];
[colorChangeObject release];
[super dealloc];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: self];
float color[] = {0, 0, 0};
[self fill: color withIndex: point.x];
UIColor *uc = [[UIColor alloc] initWithRed: color[0] green: color[1] blue: color[2] alpha: 1];
selectedColorBox.backgroundColor = uc;
if(colorChangeObject != nil){
[colorChangeObject methodForSelector: colorChangeSelector](colorChangeObject, colorChangeSelector, uc);
}
[uc release];
selectedColorBox.center = CGPointMake(point.x, self.bounds.size.height/2);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan: touches withEvent: event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan: touches withEvent: event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan: touches withEvent: event];
}
-(void) onColorChange: (SEL) selector withObject: (id) object{
self.colorChangeSelector = selector;
self.colorChangeObject = object;
}
@end
Recent Comments