Skip to content

Instantly share code, notes, and snippets.

@iamleeg
Last active December 29, 2015 03:09
Show Gist options
  • Save iamleeg/7605110 to your computer and use it in GitHub Desktop.
Save iamleeg/7605110 to your computer and use it in GitHub Desktop.
UIColor literals.
#import <UIKit/UIKit.h>
UIColor * operator"" _c(unsigned long long color)
{
unsigned long long redComponent = (color & 0xff0000 >> 16);
unsigned long long greenComponent = (color & 0x00ff00) >> 8;
unsigned long long blueComponent = color & 0xff;
float red = redComponent / 255.0;
float green = greenComponent / 255.0;
float blue = blueComponent / 255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
@diederikh
Copy link

Maybe make it OS X compatible by declaring:

  #if TARGET_OS_IPHONE
  #import <UIKit/UIKit.h>
  #else
     @compatibility_alias UIColor NSColor;
   #endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment