An upgrade from UIColor

This post introduces the BNColor class, a versatile color object that can be easily changed, saved to disk, and offers live switching between HSV and RGB color spaces – all features that are missing from the built-in UIColor class.

Sample code

The easiest way to see what the class can do is by example:

BNColor *color = [BNColor colorWithRed:0.5 green:1 blue:0];
color.hue = 0.75;  // Modifies color in the HSV space.
CGFloat newBlue = color.blue;  // Reads from the RGB space.

// Saving/loading. Supports keyed NSCoding; works with NSKeyed{Una,A}rchiver.
NSDictionary *dict = [NSDictionary dictionaryWithObject:color forKey:@"clr"];
[dict writeToFile:filePath atomically:YES];                   // Save.
dict = [NSDictionary dictionaryWithContentsOfFile:filePath];  // Load.

// String representations.  See header file for more.
NSLog(@"color is %@; css property code is %@", color, [color hexCode]);

// Can be converted to UIColor; return value is an autoreleased copy.
myView.backgroundColor = [color colorFromBNColor];

With this post, I’ve also started moving the moriarty library over to an open-source repository on github. So, yep, that means these classes are open to improvements from anyone – something I’ve been meaning to do for a little while now. Here’s the url:

https://github.com/tylerneylon/moriarty

How to install/use

This is a very lightweight class, so you can just copy the {h,m} files into your code tree, and add them to your Xcode project.

  1. Click the Downloads button on moriarty’s github page, and download whichever format you like best; uncompress the file.
  2. Drag whichever class(es) you want (both .h and .m files) onto the Classes group of your project in Xcode.  You might want to use Shared instead of Classes if it’s a universal app.
  3. Check the “Copy items…” box, and click the Add button (as in the screenshot here).

From here, you can just #import "BNColor.h" and you’re set to go.

2 Comments

  1. Posted March 28, 2011 at 10:23 am | Permalink

    Tyler, great job making an easy to use class.

    I wanted to say I’m using this in my next iOS app. I did not see any licensing terms, so I assume this code is free to use.

    I am giving you credit in the about screen of my app though.

    This class saved me some work, and I’m thankful.

    Have a nice day.

  2. Posted March 28, 2011 at 7:32 pm | Permalink

    Thanks, @Jimmy! Glad it’s useful. Let me know when your app is in the app store; I’ll check it out.

    All code I post here and on github is open source under the Apache 2 license, which basically means you can use it for free, including in commercial products, without having to ask my permission.

One Trackback

  1. […] “Nick Farina – Simplify your models with SMModelObject” [0-7]: http://bynomial.com/blog/?p=100 “Bynomial Code » An upgrade from UIColor“ [0-8]: https://github.com/nfarina/modelobject “nfarina/modelobject – […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*