Skip to content

Instantly share code, notes, and snippets.

@akiroom
Last active December 11, 2015 11:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save akiroom/4593832 to your computer and use it in GitHub Desktop.
UIColor extensions
//
// UIColor+Utilities.h
//
// ref:
// https://gist.github.com/1219029
//
#import <UIKit/UIKit.h>
@interface UIColor (Utilities)
/*
[UIColor colorWithRGBHex:0xff0000]; // red
[UIColor colorWithRGBAHex:0x00ff00ff]; // green
[UIColor colorWithRGBAHex:0x0000FF80]; // translucent blue
*/
+ (UIColor *) colorWithRGBHex:(uint) hex;
+ (UIColor *) colorWithRGBAHex:(uint) hex;
@end
//
// UIColor+Utilities.m
//
#import "UIColor+Utilities.h"
@implementation UIColor (Utilities)
+ (UIColor *) colorWithRGBHex:(uint) hex
{
int red, green, blue;
blue = hex & 0xFF;
green = (hex >> 8) & 0xFF;
red = (hex >> 16) & 0xFF;
return [UIColor colorWithRed:red / 255.0
green:green / 255.0
blue:blue / 255.0
alpha:1.0];
}
+ (UIColor *) colorWithRGBAHex:(uint) hex
{
int red, green, blue, alpha;
alpha = hex & 0xFF;
blue = (hex >> 8) & 0xFF;
green = (hex >> 16) & 0xFF;
red = (hex >> 24) & 0xFF;
return [UIColor colorWithRed:red / 255.0f
green:green / 255.0f
blue:blue / 255.0f
alpha:alpha / 255.0f];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment