Marc Nijdam

Mental pressure relief valve

UIColor Shortcuts

Over the course of a number of projects I’ve been tinkering with how best to create the endless amounts of UIColor objects that need to be constructed. Most often the exact values of these come from visual designs that encode values as indexed RGB(A) value, HSV or in the most common case, actual mockups of the screen where I’m expected to just sample the color to get it close enough for the first few builds.

I initially used a category on UIColor added a couple of methods to provide integer 0-255 values for indexed RGB values.

I’ve since come to like a much simpler approach that (1) removes the verbosity of messages and (2) the float conversions. This is achieved through a couple of RGB(A) macros that I drop into my prefix file.

So instead of using

[UIColor colorWithRed:10 green:20 blue:30]

I’d use


While the former is more Objective-C style, the latter is way more efficient to read and after a while (and a bit of designer education) it’s much easier to tweak the 0-1 float values to the desired end result.

I use the excellent Classic Color Meter (AppStore link) to meter these values out of the mock-ups. Works great for me and hope it’s useful for others.