0

我选择颜色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

4

5 回答 5

2

浏览此链接.. 对我来说最好的解决方案。也可能对大家有所帮助。

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

于 2015-04-06T08:01:32.347 回答
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;
}
于 2013-10-27T16:30:29.477 回答
1

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

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

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

于 2013-10-22T19:28:22.707 回答
0

试试这样: -

color =[UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f]
于 2013-10-22T19:24:13.493 回答
0

Foundation 中没有内置的方法可以做到这一点,但如果你真的因为任何要求而想要这样做。以下是您可以执行的操作:

1- 在此处选择颜色列表及其名称

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

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

于 2013-10-22T19:39:39.883 回答