Skip to content

Instantly share code, notes, and snippets.

@robb
Created April 1, 2011 14:57
Show Gist options
  • Save robb/898273 to your computer and use it in GitHub Desktop.
Save robb/898273 to your computer and use it in GitHub Desktop.
NSColor+Hex
//
// NSColor+Hex.h
// SoundCloud
//
// Created by Robert Böhnke on 4/1/11.
// Copyright 2011 Soundcloud Ltd. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSColor (Hex)
+ (NSColor *)colorWithRGB:(NSUInteger)hex;
+ (NSColor *)colorWithDeviceRGB:(NSUInteger)hex;
+ (NSColor *)colorWithCalibratedRGB:(NSUInteger)hex;
@end
//
// NSColor+Hex.m
// SoundCloud
//
// Created by Robert Böhnke on 4/1/11.
// Copyright 2011 Soundcloud Ltd. All rights reserved.
//
#import "NSColor+Hex.h"
@implementation NSColor (Hex)
+ (NSColor *)colorWithDeviceRGB:(NSUInteger)hex;
{
hex &= 0xFFFFFF;
NSUInteger red = (hex & 0xFF0000) >> 16;
NSUInteger green = (hex & 0x00FF00) >> 8;
NSUInteger blue = hex & 0x0000FF;
return [NSColor colorWithDeviceRed:(red / 255.0f) green:(green / 255.0f) blue:(blue / 255.0f) alpha: 1.0];
}
+ (NSColor *)colorWithCalibratedRGB:(NSUInteger)hex;
{
hex &= 0xFFFFFF;
NSUInteger red = (hex & 0xFF0000) >> 16;
NSUInteger green = (hex & 0x00FF00) >> 8;
NSUInteger blue = hex & 0x0000FF;
return [NSColor colorWithCalibratedRed:(red / 255.0f) green:(green / 255.0f) blue:(blue / 255.0f) alpha: 1.0];
}
+ (NSColor *)colorWithRGB:(NSUInteger)hex;
{
return [NSColor colorWithCalibratedRGB:hex];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment