public
Last active

UIColor category to allow specification of color like [UIColor colorWithRGB24:0xfcfc22];

  • Download Gist
UIColor+KDJPackedRGB.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// Copyright (C) 2013 Kristopher Johnson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
#import <UIKit/UIKit.h>
 
@interface UIColor (KDJPackedRGB)
 
// Return opaque color with specified 24-bit RGB components.
//
// e.g. [UIColor colorWithRGB24:0xff0000] is red;
// [UIColor colorWithRGB24:0xffff00] is yellow;
// [UIColor colorWithRGB24:0x8080c0] is a pale blue
 
+ (UIColor *)colorWithRGB24:(NSUInteger)rrggbb;
 
 
// Return color with specified RGB and alpha hex components.
//
// e.g. [UIColor colorWithRGBA32:0xff0000ff] is pure red;
// [UIColor colorWithRGBA32:0x8080cc80] is a pale translucent blue
 
+ (UIColor *)colorWithRGBA32:(NSUInteger)rrggbbaa;
 
 
// Return color with specified alpha and RGB hex components.
//
// e.g. [UIColor colorWithARGB32:0xffff0000] is pure red;
// [UIColor colorWithARGB32:0x808080c0] is a pale translucent blue
 
+ (UIColor *)colorWithARGB32:(NSUInteger)aarrggbb;
 
@end
UIColor+KDJPackedRGB.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
// Copyright (C) 2013 Kristopher Johnson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
#import "UIColor+KDJPackedRGB.h"
 
@implementation UIColor (KDJPackedRGB)
 
+ (UIColor *)colorWithRGB24:(NSUInteger)rrggbb {
NSUInteger redComponent = (rrggbb & 0x00ff0000) >> 16;
NSUInteger greenComponent = (rrggbb & 0x0000ff00) >> 8;
NSUInteger blueComponent = (rrggbb & 0x000000ff);
 
return [UIColor colorWithRed:(redComponent/255.0)
green:(greenComponent/255.0)
blue:(blueComponent/255.0)
alpha:1.0];
}
 
+ (UIColor *)colorWithRGBA32:(NSUInteger)rrggbbaa {
NSUInteger redComponent = (rrggbbaa & 0xff000000) >> 24;
NSUInteger greenComponent = (rrggbbaa & 0x00ff0000) >> 16;
NSUInteger blueComponent = (rrggbbaa & 0x0000ff00) >> 8;
NSUInteger alphaComponent = (rrggbbaa & 0x000000ff);
 
return [UIColor colorWithRed:(redComponent/255.0)
green:(greenComponent/255.0)
blue:(blueComponent/255.0)
alpha:(alphaComponent/255.0)];
}
 
+ (UIColor *)colorWithARGB32:(NSUInteger)aarrggbb {
NSUInteger alphaComponent = (aarrggbb & 0xff000000) >> 24;
NSUInteger redComponent = (aarrggbb & 0x00ff0000) >> 16;
NSUInteger greenComponent = (aarrggbb & 0x0000ff00) >> 8;
NSUInteger blueComponent = (aarrggbb & 0x000000ff);
 
return [UIColor colorWithRed:(redComponent/255.0)
green:(greenComponent/255.0)
blue:(blueComponent/255.0)
alpha:(alphaComponent/255.0)];
}
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.