00001 #ifndef _COLOR_H_
00002 #define _COLOR_H_
00003
00004 #include<iostream>
00005 template<typename Type>
00006 inline Type Clump2Type(Type el, Type Min, Type Max)
00007 {
00008 if( el < Min) return Min;
00009 else if( el > Max) return Max;
00010 else return el;
00011 }
00012
00013 namespace FemViewer {
00014
00015 struct RGBColor {
00016 union { struct { float r, g, b; }; float v[3]; };
00017 };
00018
00019 struct RGBAColor : public RGBColor{
00020 float a;
00021 };
00022 struct ColorHSV {
00023 float H, S, V;
00024 };
00025
00026 class HSV : public ColorHSV {
00027 public:
00028 HSV(float h = 0.f,float s = 0.f,float v=0.f) {
00029 H = h; S = s; V = v;
00030 }
00031 HSV(const ColorHSV& hsv) { H = hsv.H; S = hsv.S; V = hsv.V; }
00032 friend std::ostream& operator<< (std::ostream& os,const HSV hsv);
00033 };
00034
00035 inline std::ostream& operator<< (std::ostream& os,const HSV hsv) {
00036 return os << "Hue = " << hsv.H << " Sat = " << hsv.S << " Val = " << hsv.V << std::endl;
00037 }
00038
00039 class ColorRGB {
00040 public:
00041 float R, G, B;
00042
00043 explicit ColorRGB(float r = 0.0f, float g = 0.0f, float b = 0.0f)
00044 : R(r), G(g), B(b)
00045 {
00046 this->Clump2float();
00047 }
00048
00049 explicit ColorRGB(const float* col)
00050 {
00051 R = *col++;
00052 G = *col++;
00053 B = *col;
00054 }
00055
00056 operator float*() {
00057 return &this->R;
00058 }
00059
00060 ColorRGB(const ColorRGB& refCol);
00061 virtual ~ColorRGB(){}
00062
00063 void SetInterp(const ColorRGB& a, const ColorRGB& b, const double interp)
00064 {
00065 R = static_cast<float>(a.R+ (b.R-a.R) * interp);
00066 G = static_cast<float>(a.G+ (b.G-a.G) * interp);
00067 B = static_cast<float>(a.B+ (b.B-a.B) * interp);
00068 }
00069
00070
00071 ColorRGB& operator= (const ColorRGB& col);
00072
00073 void outcolor(const char * name) const;
00074
00075 friend std::ostream& operator<< (std::ostream& os, const ColorRGB& rhs);
00076
00077 protected:
00078 virtual void Clump2float();
00079
00080 };
00081
00082 inline std::ostream& operator<< (std::ostream& os, const ColorRGB& rhs)
00083 {
00084 return os << "RGB = ("<< int(rhs.R * 255) << ", " << int(rhs.G * 255) << ", " << int(rhs.B * 255) << ")\n";
00085 }
00086
00087 class ColorRGBA : public ColorRGB {
00088 public:
00089 float A;
00090
00091 ColorRGBA(float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 1.0f)
00092 : ColorRGB(r,g,b), A(a)
00093 {}
00094
00095 ColorRGBA(const ColorRGBA& rhs) : ColorRGB(rhs), A(rhs.A)
00096 {}
00097
00098 ColorRGBA& operator=(const ColorRGBA& rhs) {
00099 if (this == &rhs) return *this;
00100 ColorRGB::operator=(rhs);
00101 A = rhs.A;
00102 }
00103 };
00104
00105 ColorRGB ConvertHSVToRGB(const ColorHSV& hsv);
00106 ColorHSV ConvertRGBToHSV(const ColorRGB& rgb);
00107
00108 }
00109
00110 #endif
00111