Tuesday, October 7, 2008

A Little Color in your Life

Here are three categories I wrote on UIColor for some of my projects. I don't know how useful they'll be for you, but somebody out there might find them useful, so I thought I'd share.

The first one, UIColor(OpenGL), allows you to set the color in OpenGL using a UIColor object from the UIKit. This won't work with UIColor objects that use color spaces other than RGBA and Grayscale, but it works just fine for those. All of the UIColor class methods like +whiteColor and +blueColor return colors with one of those two color spaces, so the vast majority of the time, this will work just fine.

UIColor-OpenGL.h

#import <UIKit/UIKit.h>
#import <OpenGLES/EAGL.h>
#import <OpenGLES/ES1/gl.h>
#import <OpenGLES/ES1/glext.h>
#import <OpenGLES/EAGLDrawable.h>

@interface UIColor(OpenGL)
- (void)setOpenGLColor;
@end


UIColor-OpenGL.m

#import "UIColor-OpenGL.h"

@implementation UIColor(OpenGL)
- (void)setOpenGLColor
{
CGColorRef color = self.CGColor;
int numComponents = CGColorGetNumberOfComponents(color);

if (numComponents == 2)
{
const CGFloat *components = CGColorGetComponents(color);
CGFloat all = components[0];
CGFloat alpha = components[1];

glColor4f(all,all, all, alpha);
}
else
{
const CGFloat *components = CGColorGetComponents(color);
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];
CGFloat alpha = components[3];
glColor4f(red,green, blue, alpha);
}

}
@end


This second one will generate a random color. This version always returns a color with an alpha value of 1.0f, however you could easily modify this to make that random as well.

UIColor-Random.h

#import <UIKit/UIKit.h>

@interface UIColor(Random)
+(UIColor *)randomColor;
@end


UIColor-Random.m

#import "UIColor-Random.h"

@implementation UIColor(Random)
+(UIColor *)randomColor
{
CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
@end


This last one is very much a work in progress. I've been adding additional class factory methods to UIColor with additional colors. I find the stock set of a dozen or so colors to be kind of limiting. When I'm done, I hope to have over a hundred additional shades, but as of right now, I've only added a fraction of that, though more are sure to be coming out of a project I'm working on right now.

UIColor-MoreColors.h

#import <UIKit/UIKit.h>

@interface UIColor(MoreColors)
+ (id)indigoColor;
+ (id)tealColor;
+ (id)violetColor;
+ (id)electricVioletColor;
+ (id)vividVioletColor;
+ (id)darkVioletColor;
+ (id)amberColor;
+ (id)darkAmberColor;
+ (id)lemonColor;
+ (id)roseColor;
+ (id)rubyColor;
+ (id)fireEndingRed;
@end



UIColor-MoreColors.m

#import "UIColor-MoreColors.h"

#define vendColor(r, g, b) static UIColor *ret; if (ret == nil) ret = [[UIColor colorWithRed:r green:g blue:b alpha:1.0] retain]; return ret

@implementation UIColor(MoreColors)
#pragma mark Blues
#pragma mark -
+ (id)indigoColor
{
vendColor(.294f, 0.0f, .509f);
}
+ (id)tealColor
{
vendColor(0.0f, 0.5f, 0.5f);
}
#pragma mark -
#pragma mark Purples
#pragma mark -
+ (id)violetColor
{
vendColor (.498f, 0.0f, 1.0f);
}
+ (id)electricVioletColor
{
vendColor(.506f, 0.0f, 1.0f);
}
+ (id)vividVioletColor
{
vendColor(.506f, 0.0f, 1.0f);
}
+ (id)darkVioletColor
{
vendColor(.58f, 0.0f, .827f);
}
#pragma mark -
#pragma mark Yellows
#pragma mark -
+ (id)amberColor
{
vendColor(1.0f, .75f, 0.0f);
}
+ (id)darkAmberColor
{
vendColor(1.0f, .494f, 0.0f);
}
+ (id)lemonColor
{
vendColor(1.0f, .914f, .0627f);
}
#pragma mark -
#pragma mark Reds
#pragma mark -
+ (id)roseColor
{
vendColor(1.0f, 0.0f, 0.5f);
}
+ (id)rubyColor
{
vendColor(0.8784f, .06667f, .3725f);
}
+ (id)fireEngineRed
{
vendColor(0.8078f, 0.0863f, 0.1255f);
}


That's all for today. If you have additional methods you'd like to contribute to any of these categories, by all means, send them to me at jeff-underscore-lamarche-ampersand-mac-dot-com, and I'll add them and re-publish them. Sorry for the obfuscation, but I get plenty of junk mail as it is.

No comments:

Post a Comment