0

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

nomann
  • 2,257
  • 2
  • 21
  • 24
hemant
  • 71
  • 1
  • 9
  • What about overriding `description`, check if it matches any of the "known iOS" color, and print as? – Larme Oct 22 '13 at 21:29
  • [My answer here](http://stackoverflow.com/questions/19454398/java-applet-get-the-name-of-the-colour-being-used-in-paint/19461345#19461345) might be of use. – user1118321 Oct 23 '13 at 01:35

5 Answers5

2

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

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

Arpan Dixit
  • 995
  • 1
  • 12
  • 24
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.

Abhinav
  • 37,684
  • 43
  • 191
  • 309
  • no add color name but add only RGB Color code to fech color name Graycolor,bule,black ,etc.... – hemant Oct 22 '13 at 19:33
  • I get what you are looking for. But that is not possible. Even with the color name I mentioned above, you would get RGB and alpha components printed. Imagine there are so many color combinations! – Abhinav Oct 22 '13 at 19:37
  • i have touch image and fetch select ted image color and pass to webservice – hemant Oct 22 '13 at 19:47
1

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;
}
user1118321
  • 25,567
  • 4
  • 55
  • 86
0

Try like this:-

color =[UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f]
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • i have print no RGB Color code to name but i have print color name Black ,blue ,red etc... – hemant Oct 22 '13 at 19:28
  • i have tray this code out put : UIDeviceRGBColorSpace 0.258824 0.309804 0.356863 1 Request out put not color RGB code but Color name – hemant Oct 22 '13 at 19:31
0

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.

nomann
  • 2,257
  • 2
  • 21
  • 24
  • i have touch to image and fetch color name – hemant Oct 22 '13 at 19:43
  • my fetch color code unsigned char pixel[4] = {0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast); CGContextTranslateCTM(context, -point.x, -point.y); [self.view.layer renderInContext:context]; CGContextRelease(context); CGColorSpaceRelease(colorSpace); UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0]; – hemant Oct 22 '13 at 19:43