Skip to content

Commit

Permalink
Changed distance to match better algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyguitar committed Feb 20, 2014
1 parent ae10e59 commit b2dca6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
17 changes: 4 additions & 13 deletions Colours.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,13 @@ typedef NS_ENUM(NSInteger, ColorFormulation) {

#pragma mark - Distance between Colors
/**
* Returns a distance that one color is away from another color. The range is from 0 to 3, where 0 is matching and 3 is the exact opposite (white and black). Uses RGB to compare.
* Returns a float of the distance between 2 colors.
*
* @param color UIColor or NSColor to match against
* @param color Color to check self with.
*
* @return Distance apart.
* @return CGFloat
*/
- (CGFloat)rgbDistanceFromColor:(COLOR_CLASS *)color;

/**
* Returns a distance that one color is away from another color. The range is from 0 to 3, where 0 is matching and 3 is the exact opposite (white and black). Uses HSB to compare.
*
* @param color UIColor or NSColor to match against
*
* @return Distance apart.
*/
- (CGFloat)hsbDistanceFromColor:(COLOR_CLASS *)color;
- (CGFloat)distanceFromColor:(COLOR_CLASS *)color;


#pragma mark - Colors
Expand Down
40 changes: 16 additions & 24 deletions Colours.m
Original file line number Diff line number Diff line change
Expand Up @@ -270,38 +270,30 @@ - (instancetype)blackOrWhiteContrastingColor
- (instancetype)complementaryColor
{
NSMutableDictionary *hsba = [[self hsbaDictionary] mutableCopy];
float newH = [[self class] addDegrees:180.0f toDegree:([hsba[@"h"] floatValue]*360)];
[hsba setObject:@(newH) forKey:@"h"];
float newH = [[self class] addDegrees:180.0f toDegree:([hsba[kColoursHSBA_H] floatValue]*360)];
[hsba setObject:@(newH) forKey:kColoursHSBA_H];
return [[self class] colorFromHSBADictionary:hsba];

}


#pragma mark - Distance between Colors
- (CGFloat)rgbDistanceFromColor:(COLOR_CLASS *)color
- (CGFloat)distanceFromColor:(COLOR_CLASS *)color
{
return [self distanceFromColorArray:[color hsbaArray] colorType:ColorFormulationRGBA];
}

- (CGFloat)hsbDistanceFromColor:(COLOR_CLASS *)color {
return [self distanceFromColorArray:[color hsbaArray] colorType:ColorFormulationHSBA];
}

- (CGFloat)distanceFromColorArray:(NSArray *)colorArray colorType:(ColorFormulation)type {
// Get RGB/HSB points from both colors
double A1 = 0, A2 = 0, B1 = 0, B2 = 0, C1 = 0, C2 = 0;
NSArray *selfColorArray = type == ColorFormulationRGBA ? [self rgbaArray] : [self hsbaArray];
A1 = [selfColorArray[0] doubleValue];
A2 = [colorArray[0] doubleValue];
B1 = [selfColorArray[1] doubleValue];
B2 = [colorArray[1] doubleValue];
C1 = [selfColorArray[2] doubleValue];
C2 = [colorArray[2] doubleValue];
// General solution idea from: http://www.compuphase.com/cmetric.htm
NSArray *selfColorArray = [self rgbaArray];
NSArray *checkColorArray = [color rgbaArray];

// Set up variables
CGFloat meanR = ([selfColorArray[0] doubleValue]*255.0f + [checkColorArray[0] doubleValue]*255.0f)/2;
CGFloat deltaR = [selfColorArray[0] doubleValue]*255.0f - [checkColorArray[0] doubleValue]*255.0f;
CGFloat deltaG = [selfColorArray[1] doubleValue]*255.0f - [checkColorArray[1] doubleValue]*255.0f;
CGFloat deltaB = [selfColorArray[2] doubleValue]*255.0f - [checkColorArray[2] doubleValue]*255.0f;

// Calculate Distance
CGFloat distance = sqrtf((2 + (meanR/256))*(deltaR*deltaR) + 4*(deltaG*deltaG) + (2 + (255 - meanR)/256)*(deltaB*deltaB));

// Return Distance
// - Sum the squares of the distance between each 3D point
// - to return the correct distance.
return sqrtf(pow((A2 - A1), 2) + pow((B2 - B1), 2) + pow((C2 - C1), 2));
return distance;
}


Expand Down

0 comments on commit b2dca6b

Please sign in to comment.