17814

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

Question:

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

Answer1:

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.

Answer2:

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

Answer3:

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

<a href="https://github.com/daniel-beard/DBColorNames" rel="nofollow">https://github.com/daniel-beard/DBColorNames</a>

Answer4:

Try like this:-

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

Answer5:

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 <a href="http://en.wikipedia.org/wiki/List_of_colors" rel="nofollow">here</a>

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.

Recommend

  • Securing web server against MITM attack in Safari
  • Saving an array of NSURL to NSUserDefaults
  • image auto rotates while reading url from file upload (when it's a big image)?
  • Passing parameters in the Form constructor, winforms c#
  • Save vector of Objects to file C++
  • Laravel updating eloquent event: getting the data
  • nginx, upstream, cors fail
  • Unable to retrieve full text of title from JPEG metadata
  • Question mark icons showing up for quotation marks when there's a UTF-8 character encoding
  • HBase REST API Locking Rows
  • Liferay: After changing Public Virtual Host settings, can't log in
  • Hibernate @OneToOne with Shared Primary Key(bidirectional). Dependent entity not persisted in DB.
  • Django file upload: filename not sticking
  • django - make datetimefield accept unix timestamp
  • Using python to determine if a timestamp is under daylight savings time
  • Uploading image using SpringMVC 4.0 Multipart
  • Teradata Database 3130 Response limit exceeded?
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • a concept similar to pointers in as3?
  • LibGdx GLES2.0 cube texel stretching
  • How to retrieve a specific string from a specific list from a file with JSON in Python
  • custom string delimiters stringtemplate-4
  • retrieving data from url in iphone
  • C# - Most efficient way to iterate through multiple arrays/list
  • How to access meteor package name inside package?
  • netsh acl setting (need alternative method - registry settings?)
  • How to get latest version of a artifact on Bintray using JSONP
  • Tell Git to stop prompting me for conflicts when none really exist?
  • Unable to decode certificate at client new X509Certificate2()
  • Remove final comma from string in vb.net
  • Use of this Javascript
  • C++ Partial template specialization - design simplification
  • How to get next/previous record number?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Transpose CSV data with awk (pivot transformation)
  • Python: how to group similar lists together in a list of lists?
  • python regex in pyparsing
  • Append folder name and increment by 1 using batch script
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?