home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume18 / mtvraytrace / part02 / color.c next >
Encoding:
C/C++ Source or Header  |  1989-03-26  |  6.8 KB  |  217 lines

  1. /***********************************************************************
  2.  * $Author: markv $
  3.  * $Revision: 1.2 $
  4.  * $Date: 88/09/12 12:53:47 $
  5.  * $Log:    color.c,v $
  6.  * Revision 1.2  88/09/12  12:53:47  markv
  7.  * Fixed problem in LookupColorbyName, had return ; and return(0).
  8.  * [ Thank you lint! ]
  9.  * 
  10.  * Revision 1.1  88/09/11  11:00:37  markv
  11.  * Initial revision
  12.  * 
  13.  ***********************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include "defs.h"
  17.  
  18. #define        NCOLORS        (142)
  19.  
  20. typedef struct t_color_entry {
  21.     char *    ce_name ;
  22.     Vec     ce_color ;
  23. } ColorEntry ;
  24.  
  25. #define LESS_THAN -1
  26. #define GREATER_THAN 1
  27. #define EQUAL_TO 0
  28.  
  29. /*
  30.  * Note: These colors must be in sorted order, because we binary search
  31.  * for them.
  32.  *
  33.  * They were swiped from the X-11 distribution.  Sorry....
  34.  */
  35.  
  36. ColorEntry Colors[] = {
  37.     "Aquamarine", {.439216, .858824, .576471},
  38.     "Black", {0, 0, 0},
  39.     "Blue", {0, 0, 1},
  40.     "BlueViolet", {.623529, .372549, .623529},
  41.     "Brown", {.647059, .164706, .164706},
  42.     "CadetBlue", {.372549, .623529, .623529},
  43.     "Coral", {1, .498039, 0},
  44.     "CornflowerBlue", {.258824, .258824, .435294},
  45.     "Cyan", {0, 1, 1},
  46.     "DarkGreen", {.184314, .309804, .184314},
  47.     "DarkOliveGreen", {.309804, .309804, .184314},
  48.     "DarkOrchid", {.6, .196078, .8},
  49.     "DarkSlateBlue", {.419608, .137255, .556863},
  50.     "DarkSlateGray", {.184314, .309804, .309804},
  51.     "DarkSlateGrey", {.184314, .309804, .309804},
  52.     "DarkTurquoise", {.439216, .576471, .858824},
  53.     "DimGray", {.329412, .329412, .329412},
  54.     "DimGrey", {.329412, .329412, .329412},
  55.     "Firebrick", {.556863, .137255, .137255},
  56.     "ForestGreen", {.137255, .556863, .137255},
  57.     "Gold", {.8, .498039, .196078},
  58.     "Goldenrod", {.858824, .858824, .439216},
  59.     "Gray", {.752941, .752941, .752941},
  60.     "Green", {0, 1, 0},
  61.     "GreenYellow", {.576471, .858824, .439216},
  62.     "Grey", {.752941, .752941, .752941},
  63.     "IndianRed", {.309804, .184314, .184314},
  64.     "Khaki", {.623529, .623529, .372549},
  65.     "LightBlue", {.74902, .847059, .847059},
  66.     "LightGray", {.658824, .658824, .658824},
  67.     "LightGrey", {.658824, .658824, .658824},
  68.     "LightSteelBlue", {.560784, .560784, .737255},
  69.     "LimeGreen", {.196078, .8, .196078},
  70.     "Magenta", {1, 0, 1},
  71.     "Maroon", {.556863, .137255, .419608},
  72.     "MediumAquamarine", {.196078, .8, .6},
  73.     "MediumBlue", {.196078, .196078, .8},
  74.     "MediumForestGreen", {.419608, .556863, .137255},
  75.     "MediumGoldenrod", {.917647, .917647, .678431},
  76.     "MediumOrchid", {.576471, .439216, .858824},
  77.     "MediumSeaGreen", {.258824, .435294, .258824},
  78.     "MediumSlateBlue", {.498039, 0, 1},
  79.     "MediumSpringGreen", {.498039, 1, 0},
  80.     "MediumTurquoise", {.439216, .858824, .858824},
  81.     "MediumVioletRed", {.858824, .439216, .576471},
  82.     "MidnightBlue", {.184314, .184314, .309804},
  83.     "Navy", {.137255, .137255, .556863},
  84.     "NavyBlue", {.137255, .137255, .556863},
  85.     "Orange", {.8, .196078, .196078},
  86.     "OrangeRed", {1, 0, .498039},
  87.     "Orchid", {.858824, .439216, .858824},
  88.     "PaleGreen", {.560784, .737255, .560784},
  89.     "Pink", {.737255, .560784, .560784},
  90.     "Plum", {.917647, .678431, .917647},
  91.     "Red", {1, 0, 0},
  92.     "Salmon", {.435294, .258824, .258824},
  93.     "SeaGreen", {.137255, .556863, .419608},
  94.     "Sienna", {.556863, .419608, .137255},
  95.     "SkyBlue", {.196078, .6, .8},
  96.     "SlateBlue", {0, .498039, 1},
  97.     "SpringGreen", {0, 1, .498039},
  98.     "SteelBlue", {.137255, .419608, .556863},
  99.     "Tan", {.858824, .576471, .439216},
  100.     "Thistle", {.847059, .74902, .847059},
  101.     "Turquoise", {.678431, .917647, .917647},
  102.     "Violet", {.309804, .184314, .309804},
  103.     "VioletRed", {.8, .196078, .6},
  104.     "Wheat", {.847059, .847059, .74902},
  105.     "White", {.988235, .988235, .988235},
  106.     "Yellow", {1, 1, 0},
  107.     "YellowGreen", {.6, .8, .196078},
  108.     "aquamarine", {.439216, .858824, .576471},
  109.     "black", {0, 0, 0},
  110.     "blue", {0, 0, 1},
  111.     "blue_violet", {.623529, .372549, .623529},
  112.     "brown", {.647059, .164706, .164706},
  113.     "cadet_blue", {.372549, .623529, .623529},
  114.     "coral", {1, .498039, 0},
  115.     "cornflower_blue", {.258824, .258824, .435294},
  116.     "cyan", {0, 1, 1},
  117.     "dark_green", {.184314, .309804, .184314},
  118.     "dark_olive_green", {.309804, .309804, .184314},
  119.     "dark_orchid", {.6, .196078, .8},
  120.     "dark_slate_blue", {.419608, .137255, .556863},
  121.     "dark_slate_gray", {.184314, .309804, .309804},
  122.     "dark_slate_grey", {.184314, .309804, .309804},
  123.     "dark_turquoise", {.439216, .576471, .858824},
  124.     "dim_gray", {.329412, .329412, .329412},
  125.     "dim_grey", {.329412, .329412, .329412},
  126.     "firebrick", {.556863, .137255, .137255},
  127.     "forest_green", {.137255, .556863, .137255},
  128.     "gold", {.8, .498039, .196078},
  129.     "goldenrod", {.858824, .858824, .439216},
  130.     "gray", {.752941, .752941, .752941},
  131.     "green", {0, 1, 0},
  132.     "green_yellow", {.576471, .858824, .439216},
  133.     "grey", {.752941, .752941, .752941},
  134.     "indian_red", {.309804, .184314, .184314},
  135.     "khaki", {.623529, .623529, .372549},
  136.     "light_blue", {.74902, .847059, .847059},
  137.     "light_gray", {.658824, .658824, .658824},
  138.     "light_grey", {.658824, .658824, .658824},
  139.     "light_steel_blue", {.560784, .560784, .737255},
  140.     "lime_green", {.196078, .8, .196078},
  141.     "magenta", {1, 0, 1},
  142.     "maroon", {.556863, .137255, .419608},
  143.     "medium_aquamarine", {.196078, .8, .6},
  144.     "medium_blue", {.196078, .196078, .8},
  145.     "medium_forest_green", {.419608, .556863, .137255},
  146.     "medium_goldenrod", {.917647, .917647, .678431},
  147.     "medium_orchid", {.576471, .439216, .858824},
  148.     "medium_sea_green", {.258824, .435294, .258824},
  149.     "medium_slate_blue", {.498039, 0, 1},
  150.     "medium_spring_green", {.498039, 1, 0},
  151.     "medium_turquoise", {.439216, .858824, .858824},
  152.     "medium_violet_red", {.858824, .439216, .576471},
  153.     "midnight_blue", {.184314, .184314, .309804},
  154.     "navy", {.137255, .137255, .556863},
  155.     "navy_blue", {.137255, .137255, .556863},
  156.     "orange", {.8, .196078, .196078},
  157.     "orange_red", {1, 0, .498039},
  158.     "orchid", {.858824, .439216, .858824},
  159.     "pale_green", {.560784, .737255, .560784},
  160.     "pink", {.737255, .560784, .560784},
  161.     "plum", {.917647, .678431, .917647},
  162.     "red", {1, 0, 0},
  163.     "salmon", {.435294, .258824, .258824},
  164.     "sea_green", {.137255, .556863, .419608},
  165.     "sienna", {.556863, .419608, .137255},
  166.     "sky_blue", {.196078, .6, .8},
  167.     "slate_blue", {0, .498039, 1},
  168.     "spring_green", {0, 1, .498039},
  169.     "steel_blue", {.137255, .419608, .556863},
  170.     "tan", {.858824, .576471, .439216},
  171.     "thistle", {.847059, .74902, .847059},
  172.     "turquoise", {.678431, .917647, .917647},
  173.     "violet", {.309804, .184314, .309804},
  174.     "violet_red", {.8, .196078, .6},
  175.     "wheat", {.847059, .847059, .74902},
  176.     "white", {.988235, .988235, .988235},
  177.     "yellow", {1, 1, 0},
  178.     "yellow_green", {.6, .8, .196078}
  179. } ;
  180.  
  181. int
  182. LookupColorByName(name, color)
  183.  char * name ;
  184.  Vec color ;
  185. {
  186.     int rc ;
  187.     rc = BinarySearch(name, 0, NCOLORS - 1 , Colors) ;
  188.     if (rc < 0) {
  189.         return(0) ;
  190.     }
  191.  
  192.     VecCopy(Colors[rc].ce_color, color) ;
  193.     return 1 ;
  194. }
  195.  
  196.  
  197. int 
  198. BinarySearch(name, l, h, array)
  199.  char * name ;
  200.  int l, h ;
  201.  ColorEntry array[] ;
  202. {
  203.     int m, rc ;
  204.     if (l > h)
  205.         return(-1) ;
  206.     
  207.     m = (l + h) / 2 ;
  208.  
  209.     rc = strcmp(name, array[m].ce_name) ;
  210.     if (rc == 0)
  211.         return m ;
  212.     else if (rc < 0)
  213.         return BinarySearch(name, l, m-1, array) ;
  214.     else
  215.         return BinarySearch(name, m + 1, h, array) ;
  216. }
  217.