Skip to content

Category for NSColor allowing one to easily set / get a color using hexadecimal notation

Notifications You must be signed in to change notification settings

faceleg/CINSColor-Hex

Repository files navigation

Why?

I finally realised that [UtilFunctions colorToHex:color] isn't as elegant as a category on NSColor.

Usage

Using a hexadecimal color to create an NSColor:

@try {
    // Both shorthand and full forms of hexadecimal colours are accepted
    [colorWell setColor:[NSColor colorWithHex:@"#F00"]];
    [colorWell setColor:[NSColor colorWithHex:@"#Ff0000"]];
}
@catch (NSException *exception) {
    NSLog(@"%@", [exception description]);
}

It'll throw an exception if one attempts to use a string that is not 3 or 6 characters in length, excluding the hash.


Getting the hexadecimal representation of a given NSColor:

NSString *hexColor = [color hexColor]

More Information

See this blog post: Extracting hex value from NSColor. Get this & more useful Cocoa Categories here: Cocoa Categories;

About

Category for NSColor allowing one to easily set / get a color using hexadecimal notation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published