如何在目标c中打印RGB颜色的名称?

我selectRGB的颜色并将其保存为带有颜色名称的string

我的代码

 color =[UIColor colorWithRed:255/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:pixel[3]/255.0]; 

电stream输出: Color print: 1 0 0 1

预期输出: Color = Red

我不认为有办法打印颜色的名称。 有很多这样的组合。 您可以将RGB值作为string打印:

 CGColorRef colorRef = [UIColor grayColor].CGColor; NSString *colorString = [CIColor colorWithCGColor:colorRef].stringRepresentation; NSLog(@"colorString = %@", colorString); 

要打印实际的名字,你需要自己做更多的工作。 用RGB值保存名称,然后根据您的组合检索它们。

我只是试过这个,它似乎工作得很好。 我select的硬编码值是基于事物对我的看法。 如果“明亮”和“黑暗”对你来说意味着别的东西,随意改变它们。

 - (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; } 

去通过这个链接..最适合我的解决scheme。 可能也会帮助你们。

https://github.com/daniel-beard/DBColorNames

像这样尝试:

 color =[UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f] 

基金会没有内置的方法来做到这一点,但如果你真的想要这样做,由于任何要求。 以下是你可以做的事情:

1-在这里select颜色列表及其名称

2-将这些颜色名称保存在RGB值的某处。

3-现在,您可以select与RGB值最匹配的颜色名称。