/* Create color scale for use in Tk programs * set colorscale [split [exec Tk_colorscale -s.7 -n10] " "] * command line options: * -d decimal output for humans * -m color map: rainbow, purple, gray, terrain, iron, astro, zebra * -n number of colors * -r reverse the order * -s saturation (between 0 and 1) * * The author of this software is Eric Grosse. Copyright (c) 1993 by AT&T. * Permission to use, copy, modify, and distribute this software for any * purpose without fee is hereby granted, provided that this entire notice * is included in all copies of any software which is or includes a copy * or modification of this software and in all copies of the supporting * documentation for such software. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ /* The following is defined in ftp://netlib.att.com/netlib/graphics/rainbow.c */ extern void rainbow(double h, double s, double v, double *r, double *g, double *b); #include #include #include #include extern int getopt(int,char**,char*); /* POSIX.2 unistd.h */ extern char *optarg; extern int optind; /* color maps */ #define RAINBOW 1 #define GRAY 2 #define TERRAIN 3 #define IRON 4 #define ASTRO 5 #define ZEBRA 6 #define PURPLE 7 int mapstr(char *s) { if(strcmp(s,"rainbow")==0) return(RAINBOW); if(strcmp(s,"purple")==0) return(PURPLE); if(strcmp(s,"gray")==0 || strcmp(s,"grey")==0) return(GRAY); if(strcmp(s,"terrain")==0 || strcmp(s,"topo")==0) return(TERRAIN); if(strcmp(s,"iron")==0 || strncmp(s,"hot",3)==0) return(IRON); if(strcmp(s,"astro")==0) return(ASTRO); if(strcmp(s,"zebra")==0) return(ZEBRA); fprintf(stderr,"color choices: rainbow, purple, gray, terrain, iron, astro, zebra\n"); return(RAINBOW); } void main(int argc, char**argv) { int r, g, b, reverse = 0, map, ncolors = 11, ihue, decimal = 0; double hue, sat = .99, red, green, blue, h; map = RAINBOW; {int c;while((c=getopt(argc,argv,"dm:n:rs:"))!=-1)switch(c){ case 'd': decimal = 1-decimal; break; case 'm': map = mapstr(optarg); break; case 'n': ncolors = atoi(optarg); break; case 'r': reverse = 1-reverse; break; case 's': sat = atof(optarg); break; }} if(sat>0.99) sat = 0.99; for(ihue = 0; ihue < ncolors; ihue++){ hue = ((double)ihue)/(ncolors-1); if(hue>1.) hue = 1.; if(reverse) hue = 1.-hue; switch(map){ case RAINBOW: rainbow(1.-hue, sat, 1., &red, &green, &blue); break; case PURPLE: rainbow(1.5*(1.-hue), sat, 1., &red, &green, &blue); break; case GRAY: red = hue; green = hue; blue = hue; break; case TERRAIN: h = 3*hue; if(h<.25){ red = 0; green = 0; blue = 0.25+2*h; }else if(h<2){ red = 0; green = 0.25+(2-h); blue = 0; }else if(h<2.7){ red = .75; green = .15; blue = .0; }else{ red = .9; green = .9; blue = .9; } break; case IRON: red = 3*(hue+.03); green = 3*(hue-.333333); blue = 3*(hue-.666667); break; case ASTRO: red = hue; green = hue; blue = (hue+.2)/1.2; break; case ZEBRA: red = (ihue+reverse) % 2; green = red; blue = red; } if(red>1.) red = 1.; if(green>1.) green = 1.; if(blue>1.) blue = 1.; if(red<0.) red = 0.; if(green<0.) green = 0.; if(blue<0.) blue = 0.; r = 255*red; g = 255*green; b = 255*blue; printf(decimal?" %d,%d,%d":" #%02x%02x%02x",r,g,b); if(ihue%20==19 && ihue!=ncolors-1) printf("\n"); } printf("\n"); exit(0); }