How to print RGB color's name in objective c?

我是研究僧i 提交于 2019-12-13 03:59:22

问题


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


回答1:


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.




回答2:


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



回答3:


Go throuhg this link.. Best solution for me. Might be help you guys also.

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




回答4:


Try like this:-

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



回答5:


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.



来源:https://stackoverflow.com/questions/19526374/how-to-print-rgb-colors-name-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!