Skip to content

Instantly share code, notes, and snippets.

@cntrump
Last active November 9, 2020 09:06
Show Gist options
  • Select an option

  • Save cntrump/3b9bebd99f23601034f2c70521ddacf3 to your computer and use it in GitHub Desktop.

Select an option

Save cntrump/3b9bebd99f23601034f2c70521ddacf3 to your computer and use it in GitHub Desktop.
UIColor Extensions
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIColor (Extension)
+ (instancetype)colorWithRGB:(NSUInteger)value alpha:(CGFloat)alpha;
@end
#ifndef HexColor
# define HexColor(x) [UIColor colorWithRGB:(NSUInteger)(x) alpha:1]
# define HexColorA(x, a) [UIColor colorWithRGB:(NSUInteger)(x) alpha:a]
#endif
NS_ASSUME_NONNULL_END
#import "UIColor+Extension.h"
@implementation UIColor (Extension)
+ (instancetype)colorWithRGB:(NSUInteger)value alpha:(CGFloat)alpha {
CGFloat r = ((value & 0xff0000) >> 16) / 255.0;
CGFloat g = ((value & 0xff00) >> 8) / 255.0;
CGFloat b = (value & 0xff) / 255.0;
return [UIColor colorWithRed:r green:g blue:b alpha:alpha];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment