Skip to content

Instantly share code, notes, and snippets.

@nolili
Forked from yusuke/gist:3174898
Created July 25, 2012 07:25
Show Gist options
  • Save nolili/3174909 to your computer and use it in GitHub Desktop.
Save nolili/3174909 to your computer and use it in GitHub Desktop.
main.m
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface RGB : NSObject
{
unsigned char red, green, blue;
}
- (id)initWithRed:(int)r green:(int)g blue:(int)b;
- (id)blendColor:(RGB *)color;
- (void)print;
@end
static unsigned char roundUChar(int v){
if(v < 0) return 0;
if(v > 255) return 255;
return (unsigned char)v;
}
@implementation RGB
- (id)initWithRed:(int)r green:(int)g blue:(int)b{
if((self = [super init]) != nil){
red = roundUChar(r);
green = roundUChar(g);
blue = roundUChar(b);
}
return self;
}
- (id)blendColor:(RGB *)color{
red = ((unsigned int)red + color->red) /2;
green = ((unsigned int)green + color->green) /2;
blue = ((unsigned int)blue + color->blue) /2;
return self;
}
- (void)print{
printf("(%d, %d, %d)\n", red, green, blue);
}
@end
int main(void){
@autoreleasepool {
RGB *u, *w;
u = [[RGB alloc] initWithRed:255 green:127 blue:127];
w = [[RGB alloc] initWithRed:0 green:127 blue:64];
[u print];
[w print];
[[u blendColor:w] print];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment