Search code examples
iosobjective-chexuicolor

How to convert RGB to HTML color?


Please can you tell me how I can convert an RGB UIColor to a Hexadecimal HTML color code string?


Solution

  • - (NSString *)getHexStringForColor:(UIColor*)color {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        CGFloat r = components[0];
        CGFloat g = components[1];
        CGFloat b = components[2];
    
        return [NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)];
    }