Maybe the issue is avoiding a lot of typing for a longish list of colours? Back in the day when struct RGBColor was the mechanism, it was fairly easy to set up long lists of const values and refer to an item by name. Doing this with NSColor is not so straightforward, and making each colour a separate method is a lot of duplication. So here's a possible solution.

List your colours like this:

const float kNiceBlueColour[] = {0.2, 0.2, 0.9, 1.0};
const float kBrightRed[] = {1.0, 0, 0, 1.0};

etc. Numbers are listed in RGBA order.

Then add a class method to NSColor in a category:

+ (NSColor*)    calibratedRGBColorWithValues:(const float*) values;

Then you can write code such as:

NSColor* myColour = [NSColor calibratedRGBColorWithValues:kNiceBlueColour];


Naturally the implementation of the category method would go:

+ (NSColor*)    calibratedRGBColorWithValues:(const float*) values
{
        // sanity checking of <values> and possible caching omitted for clarity

return [self colorWithCalibratedRed:values[0] green:values[1] blue:values[2] alpha:values[3]];
}

Still too wordy? You could wrap that in a macro to make it even easier:


#define COLOUR( x )             ([NSColor calibratedRGBColorWithValues:x])

usage:

myColor = COLOUR( kLightBlueColour );




--Graham




On 06/08/2009, at 6:32 PM, Alastair Houghton wrote:

On 6 Aug 2009, at 07:58, Arie Pieter Cammeraat wrote:

I would like a more obj-c style, like
        globals.h:
                extern NSColor * const kNiceBlueColor

        globals.m:
        #import globals.h

NSColor * const kNiceBlueColor = [NSColor colorWithRed: 20 green: 20 blue: 240 alpha:1];

I found some examples of this kind for defining a NSString using the @"foo"-syntax. With NSColor, it doesn't seem to work. I get the error "initializer element is not constant". Anyone any thoughts on this?


You can do something like your example above, but because you aren't allowed to send messages (or to call functions) from initialisers of global variables in C, you'd need to initialise them in some other way (for instance from an appropriate +initialize method).

Other alternatives (possibly safer, since you can't rely on the order in which +initialize methods are called) include:

-  Adding your new colours as new methods on a category of NSColor.

- Adding your new colours as new methods on some global object in your application.

- Adding your new colours as new methods on some other appropriate object in your app.

In all of these alternative cases, you'd probably actually end up with a static NSColor * and implement something like this:

 + (NSColor *)myNiceBlueColour
 {
   static NSColor *myNiceBlueColour;

   if (!myNiceBlueColour)
myNiceBlueColour = [[NSColor alloc] initWithRed:0.1 green:0.1 blue:0.8 alpha:1.0];

   return myNiceBlueColour;
 }

(Remember, by the way, that you need to use floating point values between 0 and 1, rather than the integers you show in your example above.)

Kind regards,

Alastair.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to