Skip to content

Instantly share code, notes, and snippets.

@radex
Last active December 11, 2015 03:28
Show Gist options
  • Save radex/4537623 to your computer and use it in GitHub Desktop.
Save radex/4537623 to your computer and use it in GitHub Desktop.
CSS-like hex notation colors in Objective C
// header
#ifdef TARGET_OS_IPHONE
#define NativeColor UIColor
#else
#define NativeColor NSColor
#endif
NativeColor* hexColor(int hex);
NativeColor* hexaColor(int hex, float a);
// implementation
NativeColor* hexColor(int hex)
{
return hexaColor(hex, 1.0);
}
NativeColor* hexaColor(int hex, float a)
{
int r = (hex & 0xff0000) >> 16;
int g = (hex & 0x00ff00) >> 8;
int b = (hex & 0x0000ff);
return [NativeColor colorWithRed:r / 255.0
green:g / 255.0
blue:b / 255.0
alpha:a];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment