I like to use categories to extend classes with new methods for this kind of thing. Here is a snippet of code I just wrote today:
@implementation UIColor (Extensions) + (UIColor *)colorWithHueDegrees:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness { return [UIColor colorWithHue:(hue/360) saturation:saturation brightness:brightness alpha:1.0]; } + (UIColor *)aquaColor { return [UIColor colorWithHueDegrees:210 saturation:1.0 brightness:1.0]; } + (UIColor *)paleYellowColor { return [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0]; } @end
Now in the code, I can do things like:
self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];
and my own specific colors go straight to system ones.
(By the way, I'm starting to think more about HSB than about RGB, as I pay more attention to colors.)
UPDATE regarding precomputing a value: My hunch is that it is not worth it. But if you really wanted to, you could memoize the values ββwith static variables:
+ (UIColor *)paleYellowColor { static UIColor *color = nil; if (!color) color = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0]; return color; }
You can make a macro, also do memoizing.
jasoncrawford Apr 27 2018-10-12T00: 00Z
source share