views:

1058

answers:

3

I want to do something like this, but I cannot get a cooperative syntax.

static const UIColor *colorNavbar = [UIColor colorWithRed: 197.0/255.0 green: 169.0/255.0 blue: 140.0/255.0 alpha: 1.0];

I suppose that I could define macros, but they are ugly.

+2  A: 

I like to use categories to extend classes with new methods for this sort of thing. Here's an excerpt 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 code I can do things like:

self.view.backgroundColor = highlight? [UIColor paleYellowColor] : [UIColor whitecolor];

and my own defined colors fit right in alongside the system-defined ones.

(Incidentally, I am starting to think more in terms of HSB than RGB as I pay more attention to colors.)

UPDATE regarding precomputing the value: My hunch is that it's not worth it. But if you really wanted, 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 could make a macro do do the memoizing, too.

jasoncrawford
i like this approach. it accomplishes the organization I am looking for in the definition an usage. I was not familiar with (Extensions), I will have to read up on it as it may be appropriate for some other classes I am using.Finally, (probably getting OT), are there HSB 'picker' utilities like there are RBG pickers. I like IB, but I find it helpful to see the complementary colors together since, quite often, I want more than one color at a time.
mobibob
Oh yeah, but one problem I was trying to resolve, is getting the value precomputed. I don't want to perform three divisions every time I reference the color.
mobibob
Re color picking: I'm not an expert. I use the color picker in Interface Builder. It has a color wheel, sliders for either RGB or HSB, and also a crayon box. I like it.
jasoncrawford
I added an update regarding precomputing the value.
jasoncrawford
+1  A: 

I usually make a category of UIColor for each project:

@interface UIColor (ProjectName)

+(UIColor *) colorForSomeTable;
+(UIColor *) colorForSomeControl;
+(UIColor *) colorForSomeText;

@end

With the constants in the implementation:

@implementation UIColor (ProjectName)

+(UIColor *) colorForSomeTable { return [IUColor colorWithRed:...]; }

@end

I also do the same for UIFont and UIImage as needed.

drawnonward
+1  A: 

To expand on jasoncrawford's answer (I'd put this in as a comment, but you can't format code in the comments) if you want to precompute the values (or do it only once).

+ (UIColor *)paleYellowColor
{
    static UIColor* paleYellow = nil;
    if (paleYellow == nil)
    {
        paleYellow = [UIColor colorWithHueDegrees:60 saturation:0.2 brightness:1.0];
    }
    return paleYellow;
}

The reason your original idea doesn't work is because the compiler can only use initialisers outside of functions, not normal code. You could have achieved something like what you wanted with the initialize methosd e.g.

static UIColor* colorNavBar = nil;

+(void) initialize
{
    if (colorNavBar != nil)
    {
        colorNavBar = ....
    }
}

NB the const qualifier on your original definition is redundant since UIColor is immutable anyway.

JeremyP
Thanks those all all good tips and techniques. I will likely follow that pattern for some of my other more app specific design tasks. I am using the class/categories suggested in Jason's first post. This allows an elegant way to define a theme class of various colors.
mobibob
.. oh yeah, as for the const on immutable -- yup, got it! thanks for pointing it out.
mobibob