Skip to content

Instantly share code, notes, and snippets.

@Tricertops
Last active August 5, 2020 09:59
Show Gist options
  • Save Tricertops/6474052 to your computer and use it in GitHub Desktop.
Save Tricertops/6474052 to your computer and use it in GitHub Desktop.
UIColor method that approximates human-readable name. Uses no special algorithm, but hard-coded 10 names for 125 colors, defined by my plain sight. No guarantee you will see them the same. Returns one of these: white, blue, purple, red, orange, yellow, green, brown, gray, black.
// The Unlicense (https://unlicense.org)
- (NSString *)guessName {
unsigned char redComponent = roundf(self.redComponent*4);
unsigned char greenComponent = roundf(self.greenComponent*4);
unsigned char blueComponent = roundf(self.blueComponent*4);
// Component methods implemented below.
static NSString * const black = @"black";
static NSString * const red = @"red";
static NSString * const green = @"green";
static NSString * const blue = @"blue";
static NSString * const yellow = @"yellow";
static NSString * const white = @"white";
static NSString * const brown = @"brown";
static NSString * const purple = @"purple";
static NSString * const gray = @"gray";
static NSString * const orange = @"orange";
NSString * colors[5][5][5] = {
{ // blue 0
// red 0, 64, 128, 192, 255
{black, brown, brown, red, red}, // green 0
{green, green, brown, orange, orange}, // green 64
{green, green, green, brown, orange}, // green 128
{green, green, green, yellow, yellow}, // green 192
{green, green, green, green, yellow}, // green 255
},
{ // blue 64
{blue, purple, purple, red, red},
{green, gray, purple, orange, red},
{green, green, green, brown, orange},
{green, green, green, yellow, yellow},
{green, green, green, green, yellow},
},
{ // blue 128
{blue, blue, purple, purple, purple},
{blue, blue, purple, purple, purple},
{green, green, gray, purple, purple},
{green, green, green, green, yellow},
{green, green, green, green, yellow},
},
{ // blue 192
{blue, purple, purple, purple, purple},
{blue, blue, purple, purple, purple},
{blue, blue, blue, purple, purple},
{blue, blue, blue, gray, purple},
{blue, blue, blue, blue, yellow},
},
{ // blue 255
{blue, purple, purple, purple, purple},
{blue, blue, purple, purple, purple},
{blue, blue, blue, purple, purple},
{blue, blue, blue, blue, purple},
{blue, blue, blue, blue, white},
},
};
NSString *colorName = colors[blueComponent][greenComponent][redComponent];
return colorName;
}
- (CGFloat)redComponent {
CGFloat red;
BOOL hasRGBColorSpace = [self getRed:&red green:NULL blue:NULL alpha:NULL];
if (hasRGBColorSpace) return red;
BOOL hasGrayscaleColorSpace = [self getWhite:&red alpha:NULL];
if (hasGrayscaleColorSpace) return red;
return 0;
}
- (CGFloat)greenComponent {
CGFloat green;
BOOL hasRGBColorSpace = [self getRed:NULL green:&green blue:NULL alpha:NULL];
if (hasRGBColorSpace) return green;
BOOL hasGrayscaleColorSpace = [self getWhite:&green alpha:NULL];
if (hasGrayscaleColorSpace) return green;
return 0;
}
- (CGFloat)blueComponent {
CGFloat blue;
BOOL hasRGBColorSpace = [self getRed:NULL green:NULL blue:&blue alpha:NULL];
if (hasRGBColorSpace) return blue;
BOOL hasGrayscaleColorSpace = [self getWhite:&blue alpha:NULL];
if (hasGrayscaleColorSpace) return blue;
return 0;
}
@Tricertops
Copy link
Author

Few notes:

  • cyan is included in blue
  • magenta, violet and pink are included in purple
  • turquoise is included in green
  • brown is dark red or red mixed with green and yellow (I was getting lost)

This code rounds the color values, so it may not always give the very correct name. I would need to do this “grid” in higher resolution and with more names to be perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment