Last active
December 11, 2015 03:28
-
-
Save radex/4537623 to your computer and use it in GitHub Desktop.
CSS-like hex notation colors in Objective C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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