Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how to use these with the included Xcode project as well.
#import "Colours.h"
into the classes you want to use this category in and you're all set.
Cocoapods
pod 'Colours'
NSColor
Colours supports NSColor
out of the box! Just make sure you have the AppKit
framework installed (it comes that way for a new application) and you will be set. This README uses UIColor for its examples, just substitute NSColor and the methods are all the same.
Colours was set up to be exactly like using an Apple predefined system color. For instance, to get a stark red, you type [UIColor redColor]
. You don't get a lot of variation on this though without diving into the colorWithRed:green:blue:alpha:
method and customizing the heck out of it. Well, I took the liberty of creating 100 colors to use in the same system color way that Apple uses with iOS. So instead of the redColor example earlier, just substitute one of the colors from the giant palette above, like so: [UIColor indigoColor]
.
Beyond giving you a list of a ton of colors with no effort, this category also gives you some methods that allow different color manipulations and translations. Here's how you use these:
Hex String to and from a UIColor
UIColor *newColor = [UIColor colorFromHexString:@"#f587e4"];
NSString *hexString = [newColor hexString];
RGBA Array to and from a UIColor
The color -> array method creates an array of 4 NSNumbers representing the RGBA values of the color. These are not in the 0-255 range, but rather normalized in the 0-1 range. So if your R is 230, then it will be represented in the array as 0.902.
NSArray *colorArray = [[UIColor seafoamColor] rgbaArray];
UIColor *newColor = [UIColor colorFromRGBAArray:colorArray];
RGBA Dictionary to and from a UIColor
Similar to the array method above, this returns an NSDictionary that contains NSNumbers for the keys: @"r",@"g",@"b",@"a"
.
NSDictionary *colorDict = [[UIColor seafoamColor] rgbaDictionary];
UIColor *newColor = [UIColor colorFromRGBADictionary:colorDict];
HSBA Array & Dictionary to and from a UIColor
Like both of the RGBA methods above, you can also get the Hue, Saturation and Brightness values from a UIColor and create an array or dictionary out of them, or vice versa.
NSArray *colorArray = [[UIColor seafoamColor] hsbaArray];
NSDictionary *colorDict = [[UIColor seafoamColor] hsbaDictionary];
UIColor *newColor1 = [UIColor colorFromHSBAArray:colorArray];
UIColor *newColor2 = [UIColor colorFromHSBADictionary:colorDictionary];
Generating white or black that contrasts with a UIColor
A lot of times you may want to put text on top of a view that is a certain color, and you want to be sure that it will look good on top of it. With this method you will return either white or black, depending on the how well each of them contrast on top of it. Here's how you use this:
UIColor *contrastingColor = [[UIColor seafoamColor] blackOrWhiteContrastingColor];
Generating a complementary color
This method will create a UIColor instance that is the exact opposite color from another UIColor on the color wheel. The same saturation and brightness are preserved, just the hue is changed.
UIColor *complementary = [[UIColor seafoamColor] complementaryColor];
You can create a 5-color scheme based off of a UIColor using the following method. It takes in a UIColor and one of the ColorSchemeTypes defined in Colours. It returns an NSArray of 4 new UIColor objects to create a pretty nice color scheme that complements the root color you passed in.
NSArray *colorScheme = [color colorSchemeOfType:ColorSchemeType];
ColorSchemeTypes
- ColorSchemeAnalagous
- ColorSchemeMonochromatic
- ColorSchemeTriad
- ColorSchemeComplementary
Here are the different examples starting with a color scheme based off of [UIColor seafoamColor]
.
ColorSchemeAnalagous
ColorSchemeMonochromatic
ColorSchemeTriad
ColorSchemeComplementary
This project is distributed under the standard MIT License. Please use this and twist it in whatever fashion you wish - and recommend any cool changes to help the code.