Reputation: 71
I select the color in RGB
and save it in string with color name
my code
color =[UIColor colorWithRed:255/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:pixel[3]/255.0];
Current Output: Color print: 1 0 0 1
Expected output: Color = Red
Upvotes: 0
Views: 3604
Reputation: 995
Go throuhg this link.. Best solution for me. Might be help you guys also.
https://github.com/daniel-beard/DBColorNames
Upvotes: 2
Reputation: 26385
I just tried this and it seems to work pretty well. The hard-coded values I chose are based on how things looked to me. Feel free to change them if "bright" and "dark" mean something else to you.
- (NSString*)colorNameFromColor:(NSColor*)chosenColor
{
NSColor* calibratedColor = [chosenColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
[calibratedColor getHue:&hue
saturation:&saturation
brightness:&brightness
alpha:nil];
// I found that when the saturation was below 1% I couldn't tell
// the color from gray
if (saturation <= 0.01)
{
saturation = 0.0;
}
NSString* colorName = @"";
// If saturation is 0.0, then this is a grayscale color
if (saturation == 0.0)
{
if (brightness <= 0.2)
{
colorName = @"black";
}
else if (brightness > 0.95)
{
colorName = @"white";
}
else
{
colorName = @"gray";
if (brightness < 0.33)
{
colorName = [@"dark " stringByAppendingString:colorName];
}
else if (brightness > 0.66)
{
colorName = [@"light " stringByAppendingString:colorName];
}
}
}
else
{
if ((hue <= 15.0 / 360.0) || (hue > 330.0 / 360.0))
{
colorName = @"red";
}
else if (hue < 45.0 / 360.0)
{
colorName = @"orange";
}
else if (hue < 70.0 / 360.0)
{
colorName = @"yellow";
}
else if (hue < 150.0 / 360.0)
{
colorName = @"green";
}
else if (hue < 190.0 / 360.0)
{
colorName = @"cyan";
}
else if (hue < 250.0 / 360.0)
{
colorName = @"blue";
}
else if (hue < 290.0 / 360.0)
{
colorName = @"purple";
}
else
{
colorName = @"magenta";
}
if (brightness < 0.5)
{
colorName = [@"dark " stringByAppendingString:colorName];
}
else if (brightness > 0.8)
{
colorName = [@"bright " stringByAppendingString:colorName];
}
}
return colorName;
}
Upvotes: 1
Reputation: 38162
I don't think there is a way to print the name of the color. There are plenty of such combinations. You can print the RGB values as a string though:
CGColorRef colorRef = [UIColor grayColor].CGColor;
NSString *colorString = [CIColor colorWithCGColor:colorRef].stringRepresentation;
NSLog(@"colorString = %@", colorString);
To print actual names you need to do more work on your own. Saving names with RGB values and then retrieving them based on your combinations.
Upvotes: 1
Reputation: 2267
There is no built-in way in Foundation to do this but if you really want to do this due to any requirement. Here's what you can do:
1- Pick the list of colors along with their names here
2- Save those color names somewhere against RGB value.
3- Now, you can pick color name which has the closest match to the RGB value.
Upvotes: 0
Reputation: 15025
Try like this:-
color =[UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f]
Upvotes: 0