Here are some useful macros I've made for this and other color controls:
In your case you would just use
getRGBA(myColor, red, green, blue, alpha);
NSLog(@"Red Value: %f", red);
NSLog(@"Blue Value: %f", green);
NSLog(@"Green Value: %f", blue);
Macros:
#define rgba(r,g,b,a) [UIColor colorWithRed:((float)(r))/255.0f green:((float)(g))/255.0f blue:((float)(b))/255.0f alpha:a]
#define rgb(r,g,b) rgba(r, g, b, 1.0f)
#define rgbaf(r,g,b,a) [UIColor colorWithRed:(r) green:(g) blue:(b) alpha:a]
#define rgbf(r,g,b) rgbaf(r, g, b, 1.0f)
#define rgba_fromColor(__color, __r, __g, __b, __a) \
CGFloat __r, __g, __b, __a;\
UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
[__unpackedColor getRed:&__r green:&__g blue:&__b alpha:&__a];
#define getRGBA(__color, __r, __g, __b, __a) rgba_fromColor(__color, __r, __g, __b, __a)
#define getRed(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return r;\
})()\
)
#define getGreen(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return g;\
})()\
)
#define getBlue(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return b;\
})()\
)
#define getAlpha(__color) (\
(^float (void){\
rgba_fromColor(__color, r, g, b, a);\
return a;\
})()\
)
#define hsba(h,s,b,a) [UIColor colorWithHue:((float)(h))/360.0f saturation:((float)(s))/100.0f brightness:((float)(b))/100.0f alpha:a]
#define hsb(h,s,b) hsba(h, s, b, 1.0f)
#define hsbaf(h,s,b,a) [UIColor colorWithHue:(h) saturation:(s) brightness:(b) alpha:a]
#define hsbf(h,s,b) rgbaf(h, s, b, 1.0f)
#define hsba_fromColor(__color, __h, __s, __b, __a) \
CGFloat __h, __s, __b, __a;\
UIColor *__unpackedColor = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:__color]];/*Bring system colors into compatible color-space (e.g. DarkGrayColor)*/\
[__unpackedColor getHue:&__h saturation:&__s brightness:&__b alpha:&__a];
#define getHSBA(__color, __h, __s, __b, __a) hsba_fromColor(__color, __h, __s, __b, __a)
#define getHue(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return h;\
})()\
)
#define getSaturation(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return s;\
})()\
)
#define getBrightness(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return b;\
})()\
)
/*
///already defined in RGBA macros
#define getAlpha(__color) (\
(^float (void){\
hsba_fromColor(__color, h, s, b, a);\
return a;\
})()\
)
*/
getColor
is bad for two reasons, first it uses the get prefix that is a common mistake for Java-devs, secondly it do not describe what kind of color it is. A better name is probablyuserSelectedColor
. Look at how UIKit name getters, and properties, and follow suit. – PeyloWuserSelectedColor
describe "what kind of color it is" better thangetUserSelectedColor
? Knowing that without theget
prefix, it's the exact same thing. I personally don't understand why Apple recommends not to use theget
prefix. I use it in some cases for easier autocomplete, and to avoid possible clashes with private APIs. For example, I have a category onUIColor
and I can easily type[UIColor get
and see a list of all my methods. And if I want a dark red color,darkRedColor
would cause problems if Apple decides to add it too, butgetDarkRedColor
wouldn't. – Iulian Onofrei