読者です 読者をやめる 読者になる 読者になる

16進数でUIColor をつくる

#import <UIKit/UIKit.h>

@interface UIColor (UIColorEx)

+(id)colorWithRGBA: (uint)rgba;
+(id)colorWithRGB: (uint)rgb;

@end
#import "UIColor+RGBA.h"

@implementation UIColor (RGBA)

+(id)colorWithRGBA: (uint)rgba {
    
    const uint r = (rgba & 0xFF000000) >> 24;
    const uint g = (rgba & 0x00FF0000) >> 16;
    const uint b = (rgba & 0x0000FF00) >> 8;
    const uint a = (rgba & 0x000000FF) >> 0;
    
    const CGFloat fr = (CGFloat)r / 255.f;
    const CGFloat fg = (CGFloat)g / 255.f;
    const CGFloat fb = (CGFloat)b / 255.f;
    const CGFloat fa = (CGFloat)a / 255.f;
    
    return [self colorWithRed:fr green:fg blue:fb alpha:fa];
}

+(id)colorWithRGB: (uint)rgb {

    const uint r = (rgb & 0xFF0000) >> 16;
    const uint g = (rgb & 0x00FF00) >> 8;
    const uint b = (rgb & 0x0000FF) >> 0;
    
    const CGFloat fr = (CGFloat)r / 255.f;
    const CGFloat fg = (CGFloat)g / 255.f;
    const CGFloat fb = (CGFloat)b / 255.f;
    const CGFloat fa = 1.f;

    return [self colorWithRed:fr green:fg blue:fb alpha:fa];
}

@end