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:
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.
- Click the Downloads button on moriarty’s github page, and download whichever format you like best; uncompress the file.
- 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.
- 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