Skip to content

Instantly share code, notes, and snippets.

@TomLiu
Created February 1, 2012 13:52
Show Gist options
  • Save TomLiu/1717023 to your computer and use it in GitHub Desktop.
Save TomLiu/1717023 to your computer and use it in GitHub Desktop.
//
// NSColor+Hex.h
//
// Created by 61 on 2/1/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import <AppKit/AppKit.h>
@interface NSColor (Hex)
+ (NSColor*)colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue;
+ (NSColor*)colorWithHex:(NSInteger)hexValue;
+ (NSColor*)whiteColorWithAlpha:(CGFloat)alphaValue;
+ (NSColor*)blackColorWithAlpha:(CGFloat)alphaValue;
@end
//
// NSColor+Hex.m
//
// Created by 61 on 2/1/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "NSColor+Hex.h"
@implementation NSObject (NSColor)
+ (NSColor*)colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
return [NSColor colorWithDeviceRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue];
}
+ (NSColor*)colorWithHex:(NSInteger)hexValue
{
return [NSColor colorWithHex:hexValue alpha:1.0];
}
+ (NSColor*)whiteColorWithAlpha:(CGFloat)alphaValue
{
return [NSColor colorWithHex:0xffffff alpha:alphaValue];
}
+ (NSColor*)blackColorWithAlpha:(CGFloat)alphaValue
{
return [NSColor colorWithHex:0x000000 alpha:alphaValue];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment