Get Updates By E-mail 

Article Written By:
Johann Dowa

iOS UIColor Category Adds Hex Value Support (With Or W/O Alpha) Color Comparisons And More

  •  
  •  
  •  
  •  
  •  

There are a number of issues that come up when using UIColor because of how colors are treated, and recently I mentioned a Mac OS X colorpicker plugin that adds the capability of generating UIColor values and an Xcode plugin to help solve those issues.

Here’s a library from Nick Lockwood called ColorUtils that provides a number of UIColor convenience methods for solving any color related issues.

With this library you can:

– Create colors from hexadecmial strings (with or without alpha).
– Get the integer value of a UIColor (with or without alpha).
– Get the name associated with a UIColor if it exists.
– Check if the color is monochrome or RGB
– Compare colors.

As the readme states:

UIColor is a thin wrapper around CGColor, which supports a wide variety of different formats, making it very flexible. This flexibility seems to come at a bit of a cost to usability for common tasks however. For example, it’s non-trivial to access the red, green and blue components of an RGB color, and it is difficult to compare colors because [UIColor blackColor] is treated as different from [UIColor colorWithRed:0 green:0 blue:0 alpha:1] even though they are identical on screen. ColorUtils makes this tasks easy.

Another common problem is that RGBA UIColors are specified using four floating point values in the range 0 to 1, but virtually all graphics software treats colors as having integer components in the range 0 – 255, often represented as a hexadecimal string. ColorUtils lets you specify colors as hexadecimals so you can copy and paste values directly from PhotoShop.

You can download ColorUtils on Github here.

Included is a sample project showing ColorUtils in action – very straightforward to use.


  •  
  •  
  •  
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments