Is there an easy way to convert UIColor
to a hexadecimal value ?
Or do we have to get the RGB components with CGColorGetComponents
and then work it
There isn't, as far as I know, any built in solution.
Note however, that you should multiply by 255
instead of 256
.
Getting an hexadecimal representation is an easy task, so you won't have many troubles to build the string manually.
NSLog(@"%X%X%X", redInteger, greenInteger, blueInteger);
This is how I do did it with Swift 3:
extension UIColor {
var hex:String{
get{
var red:CGFloat = 0
var blue:CGFloat = 0
var green:CGFloat = 0
var alpha:CGFloat = 0
self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
let rgb:Int = (Int)(red*255)<<16 | (Int)(green*255)<<8 | (Int)(blue*255)<<0
return String.localizedStringWithFormat("#%06x", rgb)
}
}
}
Or you can make it a method. I just like how color.hex
looks vs color.hex()
.
I would consider using Erica Sadun's UIColor category. It includes a lot of functionality for free, including hex representations. It's pretty easy to use, just add it to whatever class header you're using it in or, add it to the pre-compiled header for ultimate flexibility. If you're adding to the pre-compiled header, do so similar to something like this:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "UIColor-Expanded.h"
#endif
Then You can use it like so NSLog(@"%@", [myColor hexStringFromColor]);
GitHub link to the UIColor category: https://github.com/erica/uicolor-utilities
ArsTechnica article about it: http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars
I also had to convert a UIColor to its hex components.
As already pointed out by lewiguez there is a very good category at github that does all that stuff.
But because I wanted to learn how it is done I made my own simple implementation for RGB colours.
+ (NSString*)colorToWeb:(UIColor*)color
{
NSString *webColor = nil;
// This method only works for RGB colors
if (color &&
CGColorGetNumberOfComponents(color.CGColor) == 4)
{
// Get the red, green and blue components
const CGFloat *components = CGColorGetComponents(color.CGColor);
// These components range from 0.0 till 1.0 and need to be converted to 0 till 255
CGFloat red, green, blue;
red = roundf(components[0] * 255.0);
green = roundf(components[1] * 255.0);
blue = roundf(components[2] * 255.0);
// Convert with %02x (use 02 to always get two chars)
webColor = [[NSString alloc]initWithFormat:@"%02x%02x%02x", (int)red, (int)green, (int)blue];
}
return webColor;
}
All feedback is welcome!
Other than the widely used string based solution, here's a hex (integer) based solution. Usage:
UIColor* color = lf_rgb(0x120aef);
log(@"color %.6x", color.hex_rgb);
And you'll get "color 120aef". I'll put these code in https://github.com/superarts/LCategory, or you can copy-paste into your own code bank too:
#define lf_rgb(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface UIColor (lc_rgb)
- (NSInteger)hex_rgb;
@end
@implementation UIColor (lc_rgb)
- (NSInteger)hex_rgb
{
CGFloat r, g, b, a;
BOOL result = [self getRed:&r green:&g blue:&b alpha:&a];
// log(@"rgba: %f, %f, %f, %f", r * 255, g * 255, b * 255, a * 255);
if ((result == NO) || (a != 1.0f))
return -1;
return
(NSInteger)(r * 255) * 256 * 256 +
(NSInteger)(g * 255) * 256 +
(NSInteger)(b * 255) * 1;
}
@end