
/* Generated by Interface Builder */

#import "ColorMap.h"
#import <stdio.h>
#import <stdlib.h>

@implementation ColorMap

- init
{
	int i;
	
	[super init];
	for(i=0; i < NUMCOLORS; i++) {
		colormap[i].red = colormap[i].green = colormap[i].blue = i;
	}
	return self;
}

- initFromFile:(const char *)name
{
	FILE *cmap;
	char buf[255];		/* magic number */
	int a,b,c,i,end;
  
	[self init];
	cmap = fopen(name,"r");
	if(!cmap) {
		perror(name);
	}

	i = 0;
	while(!feof(cmap) && i <NUMCOLORS) {
		fgets(buf,sizeof buf,cmap);
		// unparseable lines are comments
		// as is anything after the <r g b> value on a single line
		// per Fractint 15.1 standard
		if(sscanf(buf,"%d %d %d",&a,&b,&c) == 3) {
			colormap[i].red = a;
			colormap[i].green = b;
			colormap[i].blue = c;
			i++;
		}
	}
	fclose(cmap);
	end = i;
	while (i<NUMCOLORS) {	// repeat map until full.
		colormap[i].red = colormap[i - end].red;
		colormap[i].green = colormap[i - end].green;
		colormap[i].blue = colormap[i - end].blue;
		i++;
	}
	return self;
}

- colorFor:(int)ref :(int *)r :(int *)g :(int *)b
{
	*r = colormap[ref].red;
	*g = colormap[ref].green;
	*b = colormap[ref].blue;
	return self;
}


@end
