/* Copyright (C) 1997, 1998, 1999, 2000 by Alex Pfaffe (Digital Dawn Graphics Inc) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef _ddgddgColor_Class #define _ddgddgColor_Class #include "math/ddgvec.h" #include "util/ddgstr.h" class WEXP ddgColor3 { public: unsigned char v[3]; ddgColor3( const ddgColor3 *c); ddgColor3( unsigned char r, unsigned char g, unsigned char b); ddgColor3( void ); operator const unsigned char* () { return v; } operator ddgColor3* () { return this; } unsigned char operator[](int n) const { return v[n]; } void set(const ddgColor3 *c) { v[0] = c->v[0]; v[1] = c->v[1]; v[2] = c->v[2]; } void set(unsigned char r, unsigned char g,unsigned char b) { v[0] = r; v[1] = g; v[2] = b; } void enable(void); void linterp(const ddgColor3 *a, const ddgColor3 *b, float t) { set(ddgUtil::linterp(a->v[0],b->v[0],t), ddgUtil::linterp(a->v[1],b->v[1],t), ddgUtil::linterp(a->v[2],b->v[2],t)); } void einterp(const ddgColor3 *a, const ddgColor3 *b, float t) { float x = ddgUtil::einterp(0,1,t); linterp(a,b,x); } void cinterp(const ddgColor3 *a, const ddgColor3 *b, float t) { float x = ddgUtil::cinterp(0,1,t); linterp(a,b,x); } void binterp(const ddgColor3 *a, const ddgColor3 *b, float t) { float x = ddgUtil::binterp(0,1,t); linterp(a,b,x); } unsigned char r(void) { return v[0]; } unsigned char g(void) { return v[1]; } unsigned char b(void) { return v[2]; } }; class WEXP ddgColor4 { public: unsigned char v[4]; ddgColor4( unsigned char r, unsigned char g, unsigned char b, unsigned char a); // ddgColor4( Vector4 *c); ddgColor4( void ); operator const unsigned char* () { return v; } operator ddgColor4* () { return this; } unsigned char operator[](int n) const { return v[n]; } void set(const ddgColor4 *c) { v[0] = c->v[0]; v[1] = c->v[1]; v[2] = c->v[2]; v[3] = c->v[3]; } void set(unsigned char r, unsigned char g,unsigned char b, unsigned char a) { v[0] = r; v[1] = g; v[2] = b; v[3] = a; } void enable(void); void linterp(const ddgColor4 *a, const ddgColor4 *b, float t) { set(ddgUtil::linterp(a->v[0],b->v[0],t), ddgUtil::linterp(a->v[1],b->v[1],t), ddgUtil::linterp(a->v[2],b->v[2],t), ddgUtil::linterp(a->v[3],b->v[3],t)); } void einterp(const ddgColor4 *a, const ddgColor4 *b, float t) { float x = ddgUtil::einterp(0,1,t); linterp(a,b,x); } void cinterp(const ddgColor4 *a, const ddgColor4 *b, float t) { float x = ddgUtil::cinterp(0,1,t); linterp(a,b,x); } void binterp(const ddgColor4 *a, const ddgColor4 *b, float t) { float x = ddgUtil::binterp(0,1,t); linterp(a,b,x); } static void test( ddgColor4 *cs, ddgColor4 *ce ); unsigned char r(void) { return v[0]; } unsigned char g(void) { return v[1]; } unsigned char b(void) { return v[2]; } unsigned char a(void) { return v[3]; } }; #ifdef DDGSTREAM WEXP ostream& WFEXP operator << ( ostream&s, ddgColor3 v ); WEXP ostream& WFEXP operator << ( ostream&s, ddgColor3* v ); WEXP istream& WFEXP operator >> ( istream& s, ddgColor3& v); WEXP ostream& WFEXP operator << ( ostream&s, ddgColor4 v ); WEXP ostream& WFEXP operator << ( ostream&s, ddgColor4* v ); WEXP istream& WFEXP operator >> ( istream& s, ddgColor4& v); #endif class WEXP ddgColorNode : public ddgListNode { float _key; ddgStr _name; bool _is4; public: union { ddgColor3 *color3; ddgColor4 *color4; }; ddgColorNode( ddgColor3 *c, float k = 0.0, char* name = 0 ) : color3(c), _key(k), _is4(false) { _name.assign(name); } ddgColorNode( ddgColor4 *c, float k = 0.0, char* name = 0 ) : color4(c), _key(k), _is4(true) { _name.assign(name); } float key(void) { return _key; } char* name(void) { return _name; } void name(char *name) { _name.assign(name); } bool is4(void) { return _is4; } }; #ifdef DDGSTREAM WEXP ostream& WFEXP operator << ( ostream&s, ddgColorNode v ); WEXP ostream& WFEXP operator << ( ostream&s, ddgColorNode* v ); #endif class WEXP ddgColorSet : public ddgList { typedef ddgList super; public: ddgColor3* find(float key) { ddgColorNode *c = findnode(key); return c ? c->color3 : (ddgColor3*) NULL; } ddgColorNode* findnode(float key) { ddgColorNode *c = (ddgColorNode*) head(); while (c && c->key() != key) c = (ddgColorNode *) c->next(); return c; } ddgColorNode *findnode(char* name) { ddgColorNode *c = (ddgColorNode*) head(); while (c && !ddgStr::equal(name,c->name())) c = (ddgColorNode *) c->next(); return c; } ddgColor3 *find(char* name) { ddgColorNode *c = findnode(name); return c ? c->color3 : (ddgColor3*) NULL; } ddgColorNode* findNode(float key) { ddgColorNode *c = (ddgColorNode*) head(); while (c && c->key() != key) c = (ddgColorNode *) c->next(); return c; } void spread(ddgColor3 *st, ddgColor3 *en, float stk, float endk, unsigned int n ); }; #endif