Skip to content

Instantly share code, notes, and snippets.

@sarah-j-smith
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarah-j-smith/8706eeae389148d08d26 to your computer and use it in GitHub Desktop.
Save sarah-j-smith/8706eeae389148d08d26 to your computer and use it in GitHub Desktop.
Category on Cocos2D's CCColor class to create a color from a Hex string.

Create a CCColor from a Hex String

The hex strings are exactly as used for internet colors, in CSS and so on.

Examples for valid hex strings:

 NSString *reddishColorWithHalfOpacity = @"#FF05CB77";  
 NSString *greyColorWithFullOpactiy = @"#CCCCCC";

Example

 - (void)loadFromCCB {
     CCColor *customColor = [CCColor colorWithHexString:[self customColorString]];
     [self drawBorder:customColor];
}

In this example you've created a property on your custom class called customColorString and set this to a hex string inside your SpriteBuilder project. Your custom class has a drawBorder: function. By using this technique you can communicate these color values into your class instances without having to implement a SpriteBuilder plugin.

//
// CCColor+HexString.h
// SpaceBotAlpha
//
// Created by Sarah Smith on 19/02/2015.
// Copyright (c) 2015 Smithsoft. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CCColor.h>
@interface CCColor (HexString)
/**
* Create a color based on a web-style hex string, that is a string of
* 3 or 4 hex number terms, prefixed by a '#' symbol. Valid examples
* are #1a44ccff or #ccab00. Thus the string is of the form
* #rrggbb[aa] where r, g, b & a are 0-9, a-f or A-F
*
* @param Hex string to parse for the color value.
*
* @return Color value or nil if the color could not be parsed.
*/
+ (CCColor *)colorWithHexString:(NSString *)hexString;
/**
* Hex string representing this color.
*
* @return A hex string representation of the color.
*/
- (NSString *)hexString;
@end
//
// CCColor+HexString.m
// SpaceBotAlpha
//
// Created by Sarah Smith on 19/02/2015.
// Copyright (c) 2015 Smithsoft. All rights reserved.
//
#import "CCColor+HexString.h"
@implementation CCColor (HexString)
+ (CCColor *)colorWithHexString:(NSString *)hexString
{
hexString = [hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSUInteger len = [hexString length];
CCColor *hexColor = nil;
if (len > 0)
{
// Try first to match hex string with 4 terms
BOOL tryingThreeTermHexString = NO;
NSString *hx = @"^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})";
for ( ; hexColor == nil; )
{
NSRegularExpression *hexColorString = [NSRegularExpression regularExpressionWithPattern:hx options:0 error:nil];
NSArray *matches = [hexColorString matchesInString:hexString options:0 range:NSMakeRange(0, [hexString length])];
if ([matches count] == 1)
{
NSUInteger r = 0, g = 0, b = 0, a = 1;
NSTextCheckingResult *result = [matches objectAtIndex:0];
NSInteger termCount = [result numberOfRanges] - 1;
if (termCount == 3 || termCount == 4)
{
r = strtol([[hexString substringWithRange:[result rangeAtIndex:1]] UTF8String], NULL, 16);
g = strtol([[hexString substringWithRange:[result rangeAtIndex:2]] UTF8String], NULL, 16);
b = strtol([[hexString substringWithRange:[result rangeAtIndex:3]] UTF8String], NULL, 16);
if (termCount == 4)
{
a = strtol([[hexString substringWithRange:[result rangeAtIndex:4]] UTF8String], NULL, 16);
}
hexColor = [CCColor colorWithGLKVector4:GLKVector4Make(r, g, b, a)];
}
}
if (tryingThreeTermHexString) break;
hx = @"^#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})";
tryingThreeTermHexString = YES;
}
}
if (hexColor == nil)
{
CCLOGWARN(@"Error: [colorWithHexString:%@] expected \"#rrggbb\" "
"or \"#rrggbbaa\"where r, g, b & a are 0-9|a-f|A-F", hexString);
}
return hexColor;
}
- (NSString *)hexString
{
unsigned char r = [self red] * 255.0f;
unsigned char g = [self green] * 255.0f;
unsigned char b = [self blue] * 255.0f;
unsigned char a = [self alpha] * 255.0f;
return [NSString stringWithFormat:@"#%02x%02x%02x%02x", r, g, b, a];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment