Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active December 17, 2015 11:59
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopherjohnson/5606209 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/5606209 to your computer and use it in GitHub Desktop.
UIColor category to allow specification of color like [UIColor colorWithRGB24:0xfcfc22];
// 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
// 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
@kristopherjohnson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment