UIColor to Hexadecimal (web color)

12,158

Solution 1

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

Solution 2

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!

Solution 3

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
Share:
12,158
fes
Author by

fes

Updated on August 05, 2022

Comments

  • fes
    fes over 1 year

    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 out from there?

    e.g CGColorGetComponents(color.CGColor)[0] * 256 ?