Aparte de la solución de cadena ampliamente utilizada, aquí hay una solución basada en hexadecimal (entero). Uso:
UIColor* color = lf_rgb(0x120aef);
log(@"color %.6x", color.hex_rgb);
Y obtendrá "color 120aef". Voy a poner estos códigos en https://github.com/superarts/LCategory, o puede copiar y pegar en su propio banco de código también:
#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
gran biblioteca. gracias por esto. – fes
No hay problema. ¡Espero que funcione para usted! – lewiguez