Skip to content

Commit

Permalink
Added color sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyguitar committed Apr 12, 2014
1 parent b9e2c2e commit 5909814
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 4 deletions.
15 changes: 15 additions & 0 deletions Colours.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ typedef NS_ENUM(NSInteger, ColorDistance) {
ColorDistanceCIE2000,
};

typedef NS_ENUM(NSInteger, ColorComparison) {
ColorComparisonDarkness,
ColorComparisonLightness,
ColorComparisonDesaturated,
ColorComparisonSaturated,
ColorComparisonRed,
ColorComparisonGreen,
ColorComparisonBlue
};


#pragma mark - Color from Hex/RGBA/HSBA/CIE_LAB/CMYK
/**
Expand Down Expand Up @@ -366,6 +376,11 @@ typedef NS_ENUM(NSInteger, ColorDistance) {
- (CGFloat)distanceFromColor:(id)color type:(ColorDistance)distanceType;


#pragma mark - Compare Colors
+ (NSArray *)sortColors:(NSArray *)colors withComparison:(ColorComparison)comparison;
+ (NSComparisonResult)compareColor:(id)colorA andColor:(id)colorB withComparison:(ColorComparison)comparison;


#pragma mark - Colors
// System Colors
+ (instancetype)infoBlueColor;
Expand Down
73 changes: 71 additions & 2 deletions Colours.m
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,27 @@ - (CGFloat)distanceFromColor:(id)color type:(ColorDistance)distanceType {
}


#pragma mark - Compare Colors
+ (NSArray *)sortColors:(NSArray *)colors withComparison:(ColorComparison)comparison {
return [colors sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [self compareColor:obj1 andColor:obj2 withComparison:comparison];
}];
}

+ (NSComparisonResult)compareColor:(id)colorA andColor:(id)colorB withComparison:(ColorComparison)comparison {
if (![colorA isKindOfClass:[self class]] || ![colorB isKindOfClass:[self class]]) {
return NSOrderedSame;
}

// Check Colors
NSString *key = @"";
boolean_t greater = true;
NSDictionary *c1 = [colorA colorsForComparison:comparison key:&key greater:&greater];
NSDictionary *c2 = [colorB colorsForComparison:comparison key:&key greater:&greater];
return [self compareValue:[c1[key] floatValue] andValue:[c2[key] floatValue] greaterThan:greater];
}


#pragma mark - System Colors
+ (instancetype)infoBlueColor
{
Expand Down Expand Up @@ -1201,8 +1222,56 @@ + (float)addDegrees:(float)addDeg toDegree:(float)staticDeg
}
}

- (CGFloat)radiansFromDegree:(CGFloat)degree {
return degree * M_PI/180;

#pragma mark - Color Comparison
- (NSDictionary *)colorsForComparison:(ColorComparison)comparison key:(NSString **)key greater:(boolean_t *)greaterThan {
switch (comparison) {
case ColorComparisonRed:
*key = kColoursRGBA_R;
*greaterThan = true;
return [self rgbaDictionary];

case ColorComparisonGreen:
*key = kColoursRGBA_G;
*greaterThan = true;
return [self rgbaDictionary];

case ColorComparisonBlue:
*key = kColoursRGBA_B;
*greaterThan = true;
return [self rgbaDictionary];

case ColorComparisonDarkness:
*key = kColoursHSBA_B;
*greaterThan = false;
return [self hsbaDictionary];

case ColorComparisonLightness:
*key = kColoursHSBA_B;
*greaterThan = true;
return [self hsbaDictionary];

case ColorComparisonSaturated:
*key = kColoursHSBA_S;
*greaterThan = true;
return [self hsbaDictionary];

case ColorComparisonDesaturated:
*key = kColoursHSBA_S;
*greaterThan = false;
return [self hsbaDictionary];

default:
*key = kColoursRGBA_R;
*greaterThan = true;
return [self rgbaDictionary];
}
}

+ (NSComparisonResult)compareValue:(CGFloat)v1 andValue:(CGFloat)v2 greaterThan:(boolean_t)greaterThan {
CGFloat comparison = v1 - v2;
comparison = (greaterThan == true ? 1 : -1)*comparison;
return (comparison == 0.0 ? NSOrderedSame : (comparison < 0.0 ? NSOrderedDescending : NSOrderedAscending));
}


Expand Down
4 changes: 2 additions & 2 deletions Colours.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = 'Colours'
s.version = '5.2.1'
s.version = '5.3.0'
s.summary = '100s of beautiful, predefined Colors and Color methods. Works for iOS/OSX.'
s.author = {
'Ben Gordon' => '[email protected]'
}
s.source = {
:git => 'https://github.com/bennyguitar/Colours.git',
:tag => '5.2.1'
:tag => '5.3.0'
}
s.homepage = 'https://github.com/bennyguitar'
s.license = 'LICENSE'
Expand Down
48 changes: 48 additions & 0 deletions ColoursDemo/ColoursDemo Tests/ColoursDemo_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,54 @@ - (void)testColorDistance {
CGFloat distance = [[UIColor redColor] distanceFromColor:[UIColor redColor]];
XCTAssert(distance == 0, @"Color distnace from the same color does not provide accurate results.");
}


#pragma mark - Color Sorting
- (void)testColorSortLightness {
UIColor *c1 = [UIColor colorFromHSBAArray:@[@1,@1,@0.45,@1]];
UIColor *c2 = [UIColor colorFromHSBAArray:@[@1,@1,@0.88,@1]];
UIColor *c3 = [UIColor colorFromHSBAArray:@[@1,@1,@0.003,@1]];
UIColor *c4 = [UIColor colorFromHSBAArray:@[@1,@1,@0.12,@1]];
NSArray *sorted = [UIColor sortColors:@[c1, c2, c3, c4] withComparison:ColorComparisonLightness];
NSArray *check = @[c2, c1, c4, c3];
XCTAssertEqualObjects(check, sorted, @"Sorting Colors is not accurate.");
}

- (void)testColorSortDarkness {
UIColor *c1 = [UIColor colorFromHSBAArray:@[@1,@1,@0.45,@1]];
UIColor *c2 = [UIColor colorFromHSBAArray:@[@1,@1,@0.88,@1]];
UIColor *c3 = [UIColor colorFromHSBAArray:@[@1,@1,@0.003,@1]];
UIColor *c4 = [UIColor colorFromHSBAArray:@[@1,@1,@0.12,@1]];
NSArray *sorted = [UIColor sortColors:@[c1, c2, c3, c4] withComparison:ColorComparisonDarkness];
NSArray *check = @[c3, c4, c1, c2];
for (NSArray *colors in @[sorted, check]) {
for (UIColor *c in colors) {
NSLog(@"%@", c.hexString);
}
}
XCTAssertEqualObjects(check, sorted, @"Sorting Colors is not accurate.");
}
























Expand Down

0 comments on commit 5909814

Please sign in to comment.