From 6291ee3e85c901eaa193588436715358465195e3 Mon Sep 17 00:00:00 2001 From: David Redenbaugh Date: Fri, 4 Sep 2015 11:38:25 -0700 Subject: [PATCH 1/6] support black and white and grayscale between, add a few tests, change delegate method to indicate touch type --- DKVerticalColorPicker/DKVerticalColorPicker.h | 19 ++- DKVerticalColorPicker/DKVerticalColorPicker.m | 142 +++++++++++++----- .../DKVerticalColorPickerTests.m | 47 ++++-- 3 files changed, 156 insertions(+), 52 deletions(-) diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.h b/DKVerticalColorPicker/DKVerticalColorPicker.h index d697642..c370f0d 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.h +++ b/DKVerticalColorPicker/DKVerticalColorPicker.h @@ -22,22 +22,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - + #import +static NSString *DKCOLOR_TOUCHES_BEGAN_TYPE = @"touchesBegan"; +static NSString *DKCOLOR_TOUCHES_MOVED_TYPE = @"touchesMoved"; +static NSString *DKCOLOR_TOUCHES_ENDED_TYPE = @"touchesEnded"; + +static const int END_OF_GRAYSCALE_SECTION = 30; + +static const int END_OF_WHITE_SECTION = END_OF_GRAYSCALE_SECTION + 5; + +static const double COLOR_SATURATION = 0.8; + /*! A delegate that gets notifications when the color picked changes. */ @protocol DKVerticalColorPickerDelegate @optional --(void)colorPicked:(UIColor *)color; +- (void)colorPicked:(UIColor *)aColor withTouchType:(NSString *)aTouchType; @end IB_DESIGNABLE @interface DKVerticalColorPicker : UIView -@property (nonatomic, weak) IBOutlet id delegate; //set after inited -@property (nonatomic) IBInspectable UIColor *selectedColor; //setting this will update the UI & notify the delegate +@property(nonatomic, weak) IBOutlet id delegate; //set after inited +@property(nonatomic) IBInspectable UIColor *selectedColor; //setting this will update the UI & notify the delegate +- (UIColor *)getColor:(CGFloat)aY; @end diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.m b/DKVerticalColorPicker/DKVerticalColorPicker.m index 519682f..44345b2 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.m +++ b/DKVerticalColorPicker/DKVerticalColorPicker.m @@ -27,7 +27,7 @@ of this software and associated documentation files (the "Software"), to deal @interface DKVerticalColorPicker () -@property (nonatomic) CGFloat currentSelectionY; +@property(nonatomic) CGFloat currentSelectionY; @end @@ -36,7 +36,8 @@ @implementation DKVerticalColorPicker - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; - if (self) { + if (self) + { self.currentSelectionY = 0.0; self.backgroundColor = [UIColor clearColor]; } @@ -47,36 +48,101 @@ - (instancetype)initWithFrame:(CGRect)frame - (instancetype)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; - if (self) { + if (self) + { self.currentSelectionY = 0.0; self.backgroundColor = [UIColor clearColor]; } return self; } -- (void)drawRect:(CGRect)rect { +- (void)drawRect:(CGRect)rect +{ // Drawing code [super drawRect:rect]; - + //draw wings [[UIColor blackColor] set]; CGFloat tempYPlace = self.currentSelectionY; - if (tempYPlace < 0.0) { + if (tempYPlace < 0.0) + { tempYPlace = 0.0; - } else if (tempYPlace >= self.frame.size.height) { + } else if (tempYPlace >= self.frame.size.height) + { tempYPlace = self.frame.size.height - 1.0; } CGRect temp = CGRectMake(0.0, tempYPlace, self.frame.size.width, 1.0); UIRectFill(temp); - + //draw central bar over it CGFloat cbxbegin = self.frame.size.width * 0.2; CGFloat cbwidth = self.frame.size.width * 0.6; - for (int y = 0; y < self.frame.size.height; y++) { - [[UIColor colorWithHue:(y/self.frame.size.height) saturation:1.0 brightness:1.0 alpha:1.0] set]; - CGRect temp = CGRectMake(cbxbegin, y, cbwidth, 1.0); - UIRectFill(temp); + for (int y = 0; y < self.frame.size.height; y++) + { + UIColor *theColor = [self getColor:y]; + [theColor set]; + CGRect theColorRect = CGRectMake(cbxbegin, y, cbwidth, 1.0); + UIRectFill(theColorRect); + } +} + +- (UIColor *)getColor:(CGFloat)aY +{ + CGFloat theMinY = 0; + CGFloat theMaxY = self.frame.size.height; + + if (aY < END_OF_GRAYSCALE_SECTION) + { + return [self getGrayscaleColor:aY + fromMinY:theMinY + toMaxY:END_OF_GRAYSCALE_SECTION]; } + else if (aY < END_OF_WHITE_SECTION) + { + return [self getWhiteColor]; + } + else + { + return [self getRainbowHueColor:aY fromMinY:END_OF_WHITE_SECTION toMaxY:theMaxY]; + } +} + +- (UIColor *)getWhiteColor +{ + return [UIColor colorWithHue:0 saturation:0 brightness:1 alpha:1.0]; +} + +- (UIColor *)getGrayscaleColor:(CGFloat)aY fromMinY:(CGFloat)aMinY toMaxY:(CGFloat)aMaxY +{ + CGFloat hue = 0; + CGFloat s = 0; + CGFloat b = mapInputToRange(aY, aMinY, aMaxY, 0, 1); + UIColor *theColor = [UIColor colorWithHue:hue saturation:s brightness:b alpha:1.0]; + return theColor; +} + +- (UIColor *)getRainbowHueColor:(CGFloat)aY fromMinY:(CGFloat)aMinY toMaxY:(CGFloat)aMaxY +{ + CGFloat hue = mapInputToRange(aY, aMinY, aMaxY, 0, 1); + CGFloat s = COLOR_SATURATION; + CGFloat b = 1; + UIColor *theColor = [UIColor colorWithHue:hue saturation:s brightness:b alpha:1.0]; + return theColor; +} + +CGFloat projectNormal(CGFloat n, CGFloat start, CGFloat end) +{ + return start + (n * (end - start)); +} + +CGFloat normalize(CGFloat value, CGFloat startValue, CGFloat endValue) +{ + return (value - startValue) / (endValue - startValue); +} + +CGFloat mapInputToRange(CGFloat input, CGFloat startValue, CGFloat endValue, CGFloat outputStart, CGFloat outputEnd) +{ + return projectNormal(MAX(0, MIN(1, normalize(input, startValue, endValue))), outputStart, outputEnd); } /*! @@ -93,9 +159,9 @@ - (void)setSelectedColor:(UIColor *)selectedColor [self setNeedsDisplay]; } _selectedColor = selectedColor; - if([self.delegate respondsToSelector:@selector(colorPicked:)]) + if ([self.delegate respondsToSelector:@selector(colorPicked:withTouchType:)]) { - [self.delegate colorPicked:_selectedColor]; + [self.delegate colorPicked:_selectedColor withTouchType:nil]; } } } @@ -104,42 +170,42 @@ - (void)setSelectedColor:(UIColor *)selectedColor - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { - //update color - self.currentSelectionY = [((UITouch *)[touches anyObject]) locationInView:self].y; - _selectedColor = [UIColor colorWithHue:(self.currentSelectionY / self.frame.size.height) saturation:1.0 brightness:1.0 alpha:1.0]; - //notify delegate - if([self.delegate respondsToSelector:@selector(colorPicked:)]) - { - [self.delegate colorPicked:self.selectedColor]; - } + [self updateColor:touches]; + [self notifyDelegate:DKCOLOR_TOUCHES_BEGAN_TYPE]; [self setNeedsDisplay]; } + +- (void)updateColor:(const NSSet *)touches +{ + self.currentSelectionY = [((UITouch *) [touches anyObject]) locationInView:self].y; + _selectedColor = [self getColor:self.currentSelectionY]; +} + - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { - //update color - self.currentSelectionY = [((UITouch *)[touches anyObject]) locationInView:self].y; - _selectedColor = [UIColor colorWithHue:(self.currentSelectionY / self.frame.size.height) saturation:1.0 brightness:1.0 alpha:1.0]; - //notify delegate - if([self.delegate respondsToSelector:@selector(colorPicked:)]) - { - [self.delegate colorPicked:self.selectedColor]; - } + [self updateColor:touches]; + [self notifyDelegate:DKCOLOR_TOUCHES_MOVED_TYPE]; [self setNeedsDisplay]; } + - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { - //update color - self.currentSelectionY = [((UITouch *)[touches anyObject]) locationInView:self].y; - _selectedColor = [UIColor colorWithHue:(self.currentSelectionY / self.frame.size.height) saturation:1.0 brightness:1.0 alpha:1.0]; - //notify delegate - if([self.delegate respondsToSelector:@selector(colorPicked:)]) + [self updateColor:touches]; + + [self notifyDelegate:DKCOLOR_TOUCHES_ENDED_TYPE]; + [self setNeedsDisplay]; +} + +- (void)notifyDelegate:(NSString *)aTouchType +{ + if ([self.delegate respondsToSelector:@selector(colorPicked:withTouchType:)]) { - [self.delegate colorPicked:self.selectedColor]; + [self.delegate colorPicked:self.selectedColor withTouchType:aTouchType]; } - [self setNeedsDisplay]; } + - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { - + } @end diff --git a/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m b/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m index 21be05f..559ac8c 100644 --- a/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m +++ b/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m @@ -8,6 +8,7 @@ #import #import +#import "DKVerticalColorPicker.h" @interface DKVerticalColorPickerTests : XCTestCase @@ -15,26 +16,52 @@ @interface DKVerticalColorPickerTests : XCTestCase @implementation DKVerticalColorPickerTests -- (void)setUp { +- (void)setUp +{ [super setUp]; // Put setup code here. This method is called before the invocation of each test method in the class. } -- (void)tearDown { +- (void)tearDown +{ // Put teardown code here. This method is called after the invocation of each test method in the class. [super tearDown]; } -- (void)testExample { - // This is an example of a functional test case. - XCTAssert(YES, @"Pass"); +- (void)testBlack +{ + int theHeight = 100; + int theY = 0; + DKVerticalColorPicker *thePicker = [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; + UIColor *theBlackColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:1]; + XCTAssertTrue([self isColor:theBlackColor + equalToColor:[thePicker getColor:theY]], @"should start with black"); } -- (void)testPerformanceExample { - // This is an example of a performance test case. - [self measureBlock:^{ - // Put the code you want to measure the time of here. - }]; +- (void)testWhite +{ + int theHeight = 100; + int theY = 33; + DKVerticalColorPicker *thePicker = [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; + UIColor *theWhiteColor = [UIColor colorWithHue:0 saturation:0 brightness:1 alpha:1]; + XCTAssertTrue([self isColor:theWhiteColor + equalToColor:[thePicker getColor:theY]], @"should start with white"); +} + +- (void)testColor +{ + int theHeight = 100; + int theY = 100; + DKVerticalColorPicker *thePicker = [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; + UIColor *theColor = [UIColor colorWithHue:1 saturation: 0.8 brightness:1 alpha:1]; + XCTAssertTrue([self isColor:theColor + equalToColor:[thePicker getColor:theY]], @"should end with color"); +} + + +- (BOOL)isColor:(UIColor *)aColor equalToColor:(UIColor *)otherColor +{ + return CGColorEqualToColor(aColor.CGColor, otherColor.CGColor); } @end From d8dbc6a49b3130f733c345cb06901778220a8151 Mon Sep 17 00:00:00 2001 From: David Redenbaugh Date: Fri, 4 Sep 2015 12:13:10 -0700 Subject: [PATCH 2/6] switch to an enum for touch types --- DKVerticalColorPicker/DKVerticalColorPicker.h | 10 ++++++---- DKVerticalColorPicker/DKVerticalColorPicker.m | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.h b/DKVerticalColorPicker/DKVerticalColorPicker.h index c370f0d..2319cf6 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.h +++ b/DKVerticalColorPicker/DKVerticalColorPicker.h @@ -25,9 +25,11 @@ SOFTWARE. #import -static NSString *DKCOLOR_TOUCHES_BEGAN_TYPE = @"touchesBegan"; -static NSString *DKCOLOR_TOUCHES_MOVED_TYPE = @"touchesMoved"; -static NSString *DKCOLOR_TOUCHES_ENDED_TYPE = @"touchesEnded"; +typedef NS_ENUM(NSInteger, DKColorPickerTouchType) { + DKColorPickerTouchTypeTouchesBegan, + DKColorPickerTouchTypeTouchesMoved, + DKColorPickerTouchTypeTouchesEnded +}; static const int END_OF_GRAYSCALE_SECTION = 30; @@ -40,7 +42,7 @@ static const double COLOR_SATURATION = 0.8; */ @protocol DKVerticalColorPickerDelegate @optional -- (void)colorPicked:(UIColor *)aColor withTouchType:(NSString *)aTouchType; +- (void)colorPicked:(UIColor *)aColor withTouchType:(DKColorPickerTouchType)aTouchType; @end IB_DESIGNABLE diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.m b/DKVerticalColorPicker/DKVerticalColorPicker.m index 44345b2..e13c7ff 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.m +++ b/DKVerticalColorPicker/DKVerticalColorPicker.m @@ -171,7 +171,7 @@ - (void)setSelectedColor:(UIColor *)selectedColor - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self updateColor:touches]; - [self notifyDelegate:DKCOLOR_TOUCHES_BEGAN_TYPE]; + [self notifyDelegate:DKColorPickerTouchTypeTouchesBegan]; [self setNeedsDisplay]; } @@ -184,7 +184,7 @@ - (void)updateColor:(const NSSet *)touches - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [self updateColor:touches]; - [self notifyDelegate:DKCOLOR_TOUCHES_MOVED_TYPE]; + [self notifyDelegate:DKColorPickerTouchTypeTouchesMoved]; [self setNeedsDisplay]; } @@ -192,11 +192,11 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self updateColor:touches]; - [self notifyDelegate:DKCOLOR_TOUCHES_ENDED_TYPE]; + [self notifyDelegate:DKColorPickerTouchTypeTouchesEnded]; [self setNeedsDisplay]; } -- (void)notifyDelegate:(NSString *)aTouchType +- (void)notifyDelegate:(DKColorPickerTouchType)aTouchType { if ([self.delegate respondsToSelector:@selector(colorPicked:withTouchType:)]) { From df3f10700c23b65430f1be4ad560d71f2a0c73c0 Mon Sep 17 00:00:00 2001 From: David Redenbaugh Date: Mon, 7 Sep 2015 20:01:19 -0700 Subject: [PATCH 3/6] fix test project --- DKVerticalColorPicker/DKVerticalColorPicker.m | 2 +- DKVerticalColorPicker/ViewController.h | 2 +- DKVerticalColorPicker/ViewController.m | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.m b/DKVerticalColorPicker/DKVerticalColorPicker.m index e13c7ff..54b747f 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.m +++ b/DKVerticalColorPicker/DKVerticalColorPicker.m @@ -161,7 +161,7 @@ - (void)setSelectedColor:(UIColor *)selectedColor _selectedColor = selectedColor; if ([self.delegate respondsToSelector:@selector(colorPicked:withTouchType:)]) { - [self.delegate colorPicked:_selectedColor withTouchType:nil]; + [self.delegate colorPicked:_selectedColor withTouchType: DKColorPickerTouchTypeTouchesEnded ]; } } } diff --git a/DKVerticalColorPicker/ViewController.h b/DKVerticalColorPicker/ViewController.h index a18e1d6..90e1a0a 100644 --- a/DKVerticalColorPicker/ViewController.h +++ b/DKVerticalColorPicker/ViewController.h @@ -11,7 +11,7 @@ @interface ViewController : UIViewController --(void)colorPicked:(UIColor *)color; +- (void)colorPicked:(UIColor *)aColor withTouchType:(DKColorPickerTouchType)aTouchType; @end diff --git a/DKVerticalColorPicker/ViewController.m b/DKVerticalColorPicker/ViewController.m index 08dc347..4e332ab 100644 --- a/DKVerticalColorPicker/ViewController.m +++ b/DKVerticalColorPicker/ViewController.m @@ -27,7 +27,7 @@ - (void)didReceiveMemoryWarning { // Dispose of any resources that can be recreated. } --(void)colorPicked:(UIColor *)color +- (void)colorPicked:(UIColor *)aColor withTouchType:(DKColorPickerTouchType)aTouchType { self.sampleView.backgroundColor = color; } From ef21c9ad12937802f063f98233701277d6d45b4f Mon Sep 17 00:00:00 2001 From: David Redenbaugh Date: Mon, 7 Sep 2015 20:32:52 -0700 Subject: [PATCH 4/6] fix usage of set selected color, add some tests --- DKVerticalColorPicker/DKVerticalColorPicker.h | 7 +- DKVerticalColorPicker/DKVerticalColorPicker.m | 65 ++++++++++++------- DKVerticalColorPicker/ViewController.m | 2 +- .../DKVerticalColorPickerTests.m | 22 ++++++- 4 files changed, 69 insertions(+), 27 deletions(-) diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.h b/DKVerticalColorPicker/DKVerticalColorPicker.h index 2319cf6..de2ff52 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.h +++ b/DKVerticalColorPicker/DKVerticalColorPicker.h @@ -31,11 +31,11 @@ typedef NS_ENUM(NSInteger, DKColorPickerTouchType) { DKColorPickerTouchTypeTouchesEnded }; -static const int END_OF_GRAYSCALE_SECTION = 30; +static const CGFloat END_OF_GRAYSCALE_SECTION = 30; -static const int END_OF_WHITE_SECTION = END_OF_GRAYSCALE_SECTION + 5; +static const CGFloat END_OF_WHITE_SECTION = END_OF_GRAYSCALE_SECTION + 5; -static const double COLOR_SATURATION = 0.8; +static const CGFloat COLOR_SATURATION = 0.8; /*! A delegate that gets notifications when the color picked changes. @@ -53,4 +53,5 @@ IB_DESIGNABLE @property(nonatomic) IBInspectable UIColor *selectedColor; //setting this will update the UI & notify the delegate - (UIColor *)getColor:(CGFloat)aY; +-(CGFloat) getYFromColor: (UIColor *) aColor; @end diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.m b/DKVerticalColorPicker/DKVerticalColorPicker.m index 54b747f..6dba06f 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.m +++ b/DKVerticalColorPicker/DKVerticalColorPicker.m @@ -86,10 +86,26 @@ - (void)drawRect:(CGRect)rect } } + +CGFloat projectNormal(CGFloat n, CGFloat start, CGFloat end) +{ + return start + (n * (end - start)); +} + +CGFloat normalize(CGFloat value, CGFloat startValue, CGFloat endValue) +{ + return (value - startValue) / (endValue - startValue); +} + +CGFloat mapInputToRange(CGFloat input, CGFloat startValue, CGFloat endValue, CGFloat outputStart, CGFloat outputEnd) +{ + return projectNormal(MAX(0, MIN(1, normalize(input, startValue, endValue))), outputStart, outputEnd); +} + - (UIColor *)getColor:(CGFloat)aY { CGFloat theMinY = 0; - CGFloat theMaxY = self.frame.size.height; + CGFloat theMaxY = [self getMaxY]; if (aY < END_OF_GRAYSCALE_SECTION) { @@ -107,6 +123,29 @@ - (UIColor *)getColor:(CGFloat)aY } } +- (CGFloat)getMaxY +{ + return self.frame.size.height; +} + +-(CGFloat) getYFromColor: (UIColor *) aColor +{ + CGFloat hue = 0.0, saturation = 0.0, brightness = 0.0, alpha = 0.0; + if ([aColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) + { + if( saturation == 0 ) + { + return brightness*END_OF_GRAYSCALE_SECTION; + } + else + { + return END_OF_WHITE_SECTION + hue*([ self getMaxY] - END_OF_WHITE_SECTION); + } + } + + return 0; +} + - (UIColor *)getWhiteColor { return [UIColor colorWithHue:0 saturation:0 brightness:1 alpha:1.0]; @@ -130,21 +169,6 @@ - (UIColor *)getRainbowHueColor:(CGFloat)aY fromMinY:(CGFloat)aMinY toMaxY:(CGFl return theColor; } -CGFloat projectNormal(CGFloat n, CGFloat start, CGFloat end) -{ - return start + (n * (end - start)); -} - -CGFloat normalize(CGFloat value, CGFloat startValue, CGFloat endValue) -{ - return (value - startValue) / (endValue - startValue); -} - -CGFloat mapInputToRange(CGFloat input, CGFloat startValue, CGFloat endValue, CGFloat outputStart, CGFloat outputEnd) -{ - return projectNormal(MAX(0, MIN(1, normalize(input, startValue, endValue))), outputStart, outputEnd); -} - /*! Changes the selected color, updates the UI, and notifies the delegate. */ @@ -152,12 +176,9 @@ - (void)setSelectedColor:(UIColor *)selectedColor { if (selectedColor != _selectedColor) { - CGFloat hue = 0.0, temp = 0.0; - if ([selectedColor getHue:&hue saturation:&temp brightness:&temp alpha:&temp]) - { - self.currentSelectionY = floorf(hue * self.frame.size.height); - [self setNeedsDisplay]; - } + self.currentSelectionY = [ self getYFromColor: selectedColor ]; + [self setNeedsDisplay]; + _selectedColor = selectedColor; if ([self.delegate respondsToSelector:@selector(colorPicked:withTouchType:)]) { diff --git a/DKVerticalColorPicker/ViewController.m b/DKVerticalColorPicker/ViewController.m index 4e332ab..fc72da2 100644 --- a/DKVerticalColorPicker/ViewController.m +++ b/DKVerticalColorPicker/ViewController.m @@ -29,7 +29,7 @@ - (void)didReceiveMemoryWarning { - (void)colorPicked:(UIColor *)aColor withTouchType:(DKColorPickerTouchType)aTouchType { - self.sampleView.backgroundColor = color; + self.sampleView.backgroundColor = aColor; } @end diff --git a/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m b/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m index 559ac8c..a6db464 100644 --- a/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m +++ b/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m @@ -41,7 +41,7 @@ - (void)testBlack - (void)testWhite { int theHeight = 100; - int theY = 33; + int theY = END_OF_GRAYSCALE_SECTION + 2.0f; DKVerticalColorPicker *thePicker = [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; UIColor *theWhiteColor = [UIColor colorWithHue:0 saturation:0 brightness:1 alpha:1]; XCTAssertTrue([self isColor:theWhiteColor @@ -58,6 +58,26 @@ - (void)testColor equalToColor:[thePicker getColor:theY]], @"should end with color"); } +- (void)testGetYFromColor +{ + DKVerticalColorPicker* thePicker = [self createTestColorPicker]; + + UIColor *theBlackColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:1]; + XCTAssertEqual( 0, [ thePicker getYFromColor: theBlackColor], @"should start with black at zero"); + + UIColor *theWhiteColor = [UIColor colorWithHue:0 saturation:0 brightness:1 alpha:1]; + XCTAssertEqual( END_OF_GRAYSCALE_SECTION, [ thePicker getYFromColor: theWhiteColor], @"should show white"); + + UIColor *theColor = [UIColor colorWithHue:1 saturation: 0.8 brightness:1 alpha:1]; + XCTAssertEqual( 100, [ thePicker getYFromColor: theColor], @"should end with red"); +} + +- (DKVerticalColorPicker*)createTestColorPicker +{ + int theHeight = 100; + return [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; +} + - (BOOL)isColor:(UIColor *)aColor equalToColor:(UIColor *)otherColor { From 4e440e8fbed8e6382c23bdfb723d760ecc9ab357 Mon Sep 17 00:00:00 2001 From: David Redenbaugh Date: Mon, 7 Sep 2015 20:34:32 -0700 Subject: [PATCH 5/6] use common init --- .../DKVerticalColorPickerTests.m | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m b/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m index a6db464..c6189fe 100644 --- a/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m +++ b/DKVerticalColorPickerTests/DKVerticalColorPickerTests.m @@ -30,9 +30,8 @@ - (void)tearDown - (void)testBlack { - int theHeight = 100; - int theY = 0; - DKVerticalColorPicker *thePicker = [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; + DKVerticalColorPicker* thePicker = [self createTestColorPicker]; + CGFloat theY = 0; UIColor *theBlackColor = [UIColor colorWithHue:0 saturation:0 brightness:0 alpha:1]; XCTAssertTrue([self isColor:theBlackColor equalToColor:[thePicker getColor:theY]], @"should start with black"); @@ -40,9 +39,8 @@ - (void)testBlack - (void)testWhite { - int theHeight = 100; - int theY = END_OF_GRAYSCALE_SECTION + 2.0f; - DKVerticalColorPicker *thePicker = [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; + DKVerticalColorPicker* thePicker = [self createTestColorPicker]; + CGFloat theY = END_OF_GRAYSCALE_SECTION + 2.0f; UIColor *theWhiteColor = [UIColor colorWithHue:0 saturation:0 brightness:1 alpha:1]; XCTAssertTrue([self isColor:theWhiteColor equalToColor:[thePicker getColor:theY]], @"should start with white"); @@ -50,9 +48,8 @@ - (void)testWhite - (void)testColor { - int theHeight = 100; - int theY = 100; - DKVerticalColorPicker *thePicker = [[DKVerticalColorPicker alloc] initWithFrame:CGRectMake(0, 0, 50, theHeight)]; + DKVerticalColorPicker* thePicker = [self createTestColorPicker]; + CGFloat theY = 100; UIColor *theColor = [UIColor colorWithHue:1 saturation: 0.8 brightness:1 alpha:1]; XCTAssertTrue([self isColor:theColor equalToColor:[thePicker getColor:theY]], @"should end with color"); From b746075c8a5c7614ee9bf18b55b0f1273a1ad8f2 Mon Sep 17 00:00:00 2001 From: David Redenbaugh Date: Mon, 7 Sep 2015 20:35:52 -0700 Subject: [PATCH 6/6] fix type usages --- DKVerticalColorPicker/DKVerticalColorPicker.m | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DKVerticalColorPicker/DKVerticalColorPicker.m b/DKVerticalColorPicker/DKVerticalColorPicker.m index 6dba06f..c26911c 100644 --- a/DKVerticalColorPicker/DKVerticalColorPicker.m +++ b/DKVerticalColorPicker/DKVerticalColorPicker.m @@ -69,14 +69,14 @@ - (void)drawRect:(CGRect)rect tempYPlace = 0.0; } else if (tempYPlace >= self.frame.size.height) { - tempYPlace = self.frame.size.height - 1.0; + tempYPlace = self.frame.size.height - 1.0f; } CGRect temp = CGRectMake(0.0, tempYPlace, self.frame.size.width, 1.0); UIRectFill(temp); //draw central bar over it - CGFloat cbxbegin = self.frame.size.width * 0.2; - CGFloat cbwidth = self.frame.size.width * 0.6; + CGFloat cbxbegin = self.frame.size.width * 0.2f; + CGFloat cbwidth = self.frame.size.width * 0.6f; for (int y = 0; y < self.frame.size.height; y++) { UIColor *theColor = [self getColor:y];