home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / graphics / graphtal.lha / Graphtal / Color.C < prev    next >
C/C++ Source or Header  |  1992-11-20  |  3KB  |  117 lines

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