Ipphones

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 7 October 2008

A Little Color in your Life

Posted on 15:58 by Unknown
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.
Email ThisBlogThis!Share to XShare to Facebook
Posted in Categories, free code, iPHone, iPhone SDK, snippet, UIColor | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

Categories

  • 3D Models
  • Ad Hoc Distribution
  • ADC
  • Address Book
  • Amazon
  • Anaglyphs
  • App Store
  • Apple
  • Apple DTS
  • Apple Store
  • Application Store
  • articles
  • Award
  • Background Processing
  • Barcodes
  • Beta
  • Blog
  • Blogger
  • Blogging
  • Blogs
  • Blogspot
  • Book project
  • Bug Reporting
  • Captain Obvious
  • Categories
  • Censorship
  • CFFoundation
  • CGAffineTransform
  • Clang Static Analyzer
  • Cocoa
  • Cocoa Touch
  • Code Reuse
  • Code Signing
  • Computer
  • conferences
  • Controller Classes
  • Core Animation
  • Daring Fireball
  • Database
  • Debugging
  • Defect
  • Delegates
  • Design Awards
  • Developer Certifications
  • Discussion Forums
  • Edit Mode
  • employment opportunities
  • Encryption
  • Enterprise
  • Errata
  • free code
  • Free software
  • Full Screen
  • Game Programming
  • Gestures
  • Getting Started
  • goof
  • Google Code
  • Google Maps
  • Gotcha
  • Help
  • HIG
  • HTTP PUT
  • Idiots
  • Idle Timer
  • Images
  • Instruments
  • Interface Builder
  • iPHone
  • iPhone Applications
  • iPhone Dev Center
  • iPhone Developers
  • iPhone OS 3.0
  • iPhone SDK
  • iPhone SDK PNG
  • iPhone Simulator
  • iPhoneSDK
  • iPod
  • Job Opportunities.
  • k
  • Key Value Observing
  • Keynote
  • KVO
  • Landscape Mode
  • Learn Cocoa
  • Learn Cocoa on the Mac
  • libxml
  • Licensing
  • Mac Developers
  • Mac OS X
  • Macworld Expo
  • Microsoft
  • NDA
  • NeHe
  • New Category
  • New Release
  • NSFileHandle
  • NSMutableArray
  • NSMutableURLRequest
  • NSXML
  • Object-Oriented Design
  • Objective-C
  • Open Source
  • OpenGL ES
  • Optimizations
  • Other blogs
  • Paired Arrays
  • Parsing
  • Particle Engine
  • Party
  • PeopleSoft
  • Performance
  • Persistence
  • Pink Screen of Death
  • Piracy
  • Pixar
  • Podcasts
  • Press Release WTF
  • Press Releases WTF
  • private APIs Google
  • Project Template
  • Properties
  • Random Numbers
  • Rant
  • Rejected
  • Resources
  • Responder Chain
  • REST
  • Reverse Engineering
  • Rumors
  • Runtime
  • Sample Code
  • Screencast
  • screenshot
  • Scroll Views
  • snippet
  • Snow Leopard.
  • SOAP
  • Sockets
  • Source
  • Splash Screen
  • SQLite
  • SQLitePersistentObjects
  • Steve Jobs
  • Steve-Note
  • Strings
  • Stupidity
  • Subversion
  • Table Views
  • Taps
  • Template
  • Tip
  • Tips
  • Tririga
  • tutorials
  • Twitter
  • UIAlertView
  • UIColor
  • UIImage
  • UIPickerView
  • UIScrollView
  • UITextField
  • UIView
  • UIWebView
  • Update
  • Utilities
  • UUID
  • Vacation
  • Version Control
  • Web Services
  • Writing
  • WTF
  • WWDC
  • Xcode
  • XML

Blog Archive

  • ►  2009 (141)
    • ►  May (14)
    • ►  April (30)
    • ►  March (48)
    • ►  February (26)
    • ►  January (23)
  • ▼  2008 (163)
    • ►  December (46)
    • ►  November (25)
    • ▼  October (44)
      • Elvis has Left the Building
      • Updated SQLitePersistence
      • Some More Open Source
      • Speaking of Wil Shipley
      • 500%
      • Better Template
      • CGAffineTransform 1.1 - A little more
      • Demystifying CGAffineTransform
      • I'm Flattered, I Think...
      • WSDL2ObjC
      • Table View Multi-Row Edit Mode
      • Captain Obvious Strikes Again
      • It's Beta, but it Shows they are listening...
      • SOAP Web Services Redux
      • Crimson FX Open Sourced
      • Another Open Source Project
      • Scroll Views
      • Cocoa Barcodes
      • Old is New
      • You Shouldn't, but You Will...
      • Getting the contents of a UIView as a UIImage
      • Shuffling Arrays
      • Encodings Can Byte You
      • Idle Timer
      • Starting in Landscape Mode without Status Bar
      • SQLite Persistent Object Update
      • Device vs. Simulator
      • Another Couple of Tips by Captain Obvious
      • Simulator
      • iPhone "Optimized" PNGs
      • Random Thoughts: rand() vs. arc4random()
      • Twitter
      • Another ADC Article Online
      • Completely Off-Topic
      • A Couple More Math Snippets for 2D Graphics
      • Circles, Ellipses, and Regular Polygons in OpenGL ES
      • Radar URLs & Bug Reporting
      • A Little Color in your Life
      • I Really Like the Layout
      • Did you know you could take screenshots without Xc...
      • A Bit About the Responder Chain
      • Handling Double Taps
      • Ego Boost
      • Holy %@#$!
    • ►  September (2)
    • ►  August (5)
    • ►  July (2)
    • ►  June (9)
    • ►  May (2)
    • ►  April (11)
    • ►  March (17)
Powered by Blogger.

About Me

Unknown
View my complete profile