Utilice el método UIColor
: getHue:saturation:brightness:alpha:
De la documentación de Apple:
"devuelve los componentes que forman el color en el espacio de color HSB."
- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha
Ejemplo:
UIColor *testColor = [UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00];
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
BOOL success = [testColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
NSLog(@"success: %i hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", success, hue, saturation, brightness, alpha);
NSLog salida:
éxito: 1 matiz: 0,10, saturación: 0,79, brillo: 0,53, alfa: 1,00
Aquí es una versión corregida del método provisto por @WhiteTiger:
salida
// Test values
CGFloat red = 0.53;
CGFloat green = 0.37;
CGFloat blue = 0.11;
CGFloat hue = 0;
CGFloat saturation = 0;
CGFloat brightness = 0;
CGFloat minRGB = MIN(red, MIN(green,blue));
CGFloat maxRGB = MAX(red, MAX(green,blue));
if (minRGB==maxRGB) {
hue = 0;
saturation = 0;
brightness = minRGB;
} else {
CGFloat d = (red==minRGB) ? green-blue : ((blue==minRGB) ? red-green : blue-red);
CGFloat h = (red==minRGB) ? 3 : ((blue==minRGB) ? 1 : 5);
hue = (h - d/(maxRGB - minRGB))/6.0;
saturation = (maxRGB - minRGB)/maxRGB;
brightness = maxRGB;
}
NSLog(@"hue: %0.2f, saturation: %0.2f, value: %0.2f", hue, saturation, brightness);
NSLog:
tonalidad: 0,10, saturación: 0,79, valor: 0,53
duplicado Posible de http://stackoverflow.com/questions/9142427/uicolor-conversion-from-rgb-to-hsv-set-brightness-to-uicolor http://stackoverflow.com/ preguntas/3017553/conversión-de-rgb-a-hsl-con-objetivo-c http: // stackoverflow.com/questions/1050874/how-do-i-convert-rgb-into-hsv-in-cocoa-touch –