home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Photo CD Demo 1
/
Demo.bin
/
graphtal
/
color.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-23
|
2KB
|
111 lines
/*
* Color.C - methods for color handling.
*
* Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
* All rights reserved.
*
* This software may be freely copied, modified, and redistributed
* provided that this copyright notice is preserved on all copies.
*
* You may not distribute this software, in whole or in part, as part of
* any commercial product without the express consent of the authors.
*
* There is no warranty or other guarantee of fitness of this software
* for any purpose. It is provided solely "as is".
*
*/
#include <stream.h>
#include <stdlib.h>
#include "Globals.h"
#include "Color.h"
#include "table.h"
#include "Error.h"
//___________________________________________________________ Color
struct cvector {
float r, g, b;
};
declareTable(ColorTable, rcString, cvector);
implementTable(ColorTable, rcString, cvector);
ColorTable* Color::colors = NULL;
rcString Color::ColorFile;
void Color::setupColors()
{
char* name;
if (name = getenv(COLOR_ENV))
ColorFile = name;
else
ColorFile = defaultColorFile;
readColors();
}
Color::Color()
: red(1), green(0), blue(0), name("red")
{
/*
* color table already build up?
*/
if (colors == NULL)
readColors();
}
Color::Color(const rcString& colorName)
{
/*
* color table already build up?
*/
if (colors == NULL)
readColors();
cvector rgbValues;
if (colors->lookup(colorName, rgbValues)) {
red = rgbValues.r/255;
green = rgbValues.g/255;
blue = rgbValues.b/255;
name = colorName;
}
else {
Error(ERR_WARN, "unknown color " + colorName);
red = rgbValues.r = 1;
green = rgbValues.g = 0;
blue = rgbValues.b =0;
name = colorName;
/*
* The next time we will know the color!
*/
colors->insert(colorName, rgbValues);
}
}
void Color::readColors()
{
ifstream inFile(ColorFile, ios::in);
if (!inFile)
Error(ERR_PANIC, "could't open file '" + ColorFile + "' (is COLORFILE set?)");
colors = new ColorTable(200);
char cName[300];
cvector rgbValues;
long colorNum = 0;
while(inFile) {
inFile >> rgbValues.r >> rgbValues.g >> rgbValues.b >> cName;
if (inFile.bad())
Error(ERR_ABORT, "ill fomatted color file near line "
+ rcString(form("%ld", colorNum)));
colorNum++;
colors->insert(cName, rgbValues);
}
inFile.close();
}