UIColor to unsigned integer

ios, iphone

Solution

If you had looked at the sources, you would have found out that they use this unsigned integer value as a hexadecimal color code, where

colorcode = ((unsigned)(red * 255) << 16) + ((unsigned)(green * 255) << 8) + ((unsigned)(blue * 255) << 0)

So you can get such a hexadecimal value from an UIColor object using someting like this:

@implementation UIColor (Hex)

- (NSUInteger)colorCode
{
    float red, green, blue;
    if ([self getRed:&red green:&green blue:&blue alpha:NULL])
    {
        NSUInteger redInt = (NSUInteger)(red * 255 + 0.5);
        NSUInteger greenInt = (NSUInteger)(green * 255 + 0.5);
        NSUInteger blueInt = (NSUInteger)(blue * 255 + 0.5);

        return (redInt << 16) | (greenInt << 8) | blueInt;
    }

    return 0;
}

@end

And then use it like:

NSUInteger hexPurple = [[UIColor purpleColor] colorCode];

Problem

I found a category for `UIImage` for replacing color here The problem is that the method signature receives an unsigned integer color code: ``` - (UIImage *)imageByRemovingColorsWithMinColor:(uint)minColor maxColor:(uint)maxColor ``` How do I get proper unsigned integer value from a `UIColor`? I actually want to replace black color with purple.

Original source