home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / color.c < prev    next >
C/C++ Source or Header  |  1992-10-23  |  2KB  |  111 lines

  1. /*
  2.  * Color.C  - methods for color handling.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include <stream.h>
  19. #include <stdlib.h>
  20. #include "Globals.h"
  21. #include "Color.h"
  22. #include "table.h"
  23. #include "Error.h"
  24.  
  25. //___________________________________________________________ Color
  26.  
  27. struct cvector {
  28.   float r, g, b;
  29. };
  30.  
  31. declareTable(ColorTable, rcString, cvector);
  32. implementTable(ColorTable, rcString, cvector);
  33.  
  34. ColorTable* Color::colors = NULL;
  35. rcString Color::ColorFile;
  36.  
  37. void Color::setupColors()
  38. {
  39.   char* name;
  40.   if (name = getenv(COLOR_ENV))
  41.     ColorFile = name;
  42.   else
  43.     ColorFile = defaultColorFile;
  44.   readColors();
  45. }
  46.  
  47. Color::Color()
  48. : red(1), green(0), blue(0), name("red")
  49. {
  50.   /*
  51.    * color table already build up?
  52.    */
  53.   if (colors == NULL)
  54.     readColors();
  55. }
  56.  
  57. Color::Color(const rcString& colorName)
  58. {
  59.   /*
  60.    * color table already build up?
  61.    */
  62.   if (colors == NULL)
  63.     readColors();
  64.  
  65.   cvector rgbValues;
  66.   if (colors->lookup(colorName, rgbValues)) {
  67.     red   = rgbValues.r/255;
  68.     green = rgbValues.g/255;
  69.     blue  = rgbValues.b/255;
  70.     name  = colorName;
  71.   }
  72.   else {
  73.     Error(ERR_WARN, "unknown color " + colorName);
  74.     red   = rgbValues.r = 1;
  75.     green = rgbValues.g = 0;
  76.     blue  = rgbValues.b =0;
  77.     name  = colorName;
  78.  
  79.     /*
  80.      * The next time we will know the color!
  81.      */
  82.     colors->insert(colorName, rgbValues);
  83.   }
  84. }
  85.  
  86. void Color::readColors()
  87. {
  88.   ifstream inFile(ColorFile, ios::in);
  89.  
  90.   if (!inFile)
  91.     Error(ERR_PANIC, "could't open file '" + ColorFile + "' (is COLORFILE set?)");
  92.  
  93.   colors = new ColorTable(200);
  94.   char cName[300];
  95.   cvector rgbValues;
  96.  
  97.   long colorNum = 0;
  98.   while(inFile) {
  99.     inFile >> rgbValues.r >> rgbValues.g >> rgbValues.b >> cName;
  100.     if (inFile.bad())
  101.       Error(ERR_ABORT, "ill fomatted color file near line " 
  102.                    + rcString(form("%ld", colorNum)));
  103.  
  104.     colorNum++;
  105.     colors->insert(cName, rgbValues);
  106.   }
  107.  
  108.   inFile.close();
  109. }
  110.  
  111.