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

时间:2013-10-22 19:15:28

标签: ios objective-c xcode uikit

我选择RGB中的颜色并将其保存为颜色名称为

的字符串

我的代码

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

当前输出:Color print: 1 0 0 1

预期输出:Color = Red

5 个答案:

答案 0 :(得分:1)

我认为没有办法打印颜色的名称。有很多这样的组合。您可以将RGB值打印为字符串:

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

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

答案 1 :(得分:1)

我刚试过这个,看起来效果很好。我选择的硬编码值取决于事物对我的看法。如果"明亮"随意更改它们。和"黑暗"对你来说意味着什么。

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

答案 2 :(得分:1)

浏览此链接.. 最适合我的解决方案。也可以帮助你们。

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

答案 3 :(得分:0)

试试这样: -

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

答案 4 :(得分:0)

基金会没有内置的方法可以做到这一点,但如果你真的想因为任何要求而这样做。这是你可以做的:

1-选择颜色列表及其名称here

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

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