HyperspaceExplorer 0.7.1
|
00001 /* 00002 Hyperspace Explorer - visualizing higher-dimensional geometry 00003 Copyright (C) 2010 Lene Preuss <lene.preuss@gmail.com> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License along 00016 with this program; if not, write to the Free Software Foundation, Inc., 00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00018 00019 */ 00020 00021 #if !defined(COLOR_H) 00022 #define COLOR_H 00023 00024 #include <string> 00025 #include <sstream> 00026 00027 #include <QColor> 00028 00029 #include "Vector.h" 00030 00032 00039 class Color { 00040 public: 00041 Color() { r() = g() = b() = a() = 0.; } 00042 Color(float, float, float, float = 1.); 00043 Color(const VecMath::Vector<4> &); 00044 Color(const QColor &); 00045 00046 float &r() { return RGBA[0]; } 00047 float r() const { return RGBA[0]; } 00048 float &g() { return RGBA[1]; } 00049 float g() const { return RGBA[1]; } 00050 float &b() { return RGBA[2]; } 00051 float b() const { return RGBA[2]; } 00052 float &a() { return RGBA[3]; } 00053 float a() const { return RGBA[3]; } 00054 00056 operator float *() { return RGBA; } 00058 operator const float *() const { return RGBA; } 00059 Color operator *=(float); 00060 Color operator *=(const Color &); 00061 Color operator *(float); 00062 Color operator *(const Color &); 00063 Color operator +=(float); 00064 Color operator +(float); 00065 Color operator +=(const Color &); 00066 Color operator +(const Color &); 00067 void setComponentLowerLimit(float); 00068 void setComponentUpperLimit(float); 00069 00071 operator std::string() const { 00072 std::ostringstream o; 00073 o <<"[ "<< RGBA[0] << ", "<< RGBA[1]<< ", "<< RGBA[2]<< " ]" 00074 << std::ends; 00075 return o.str(); 00076 } 00077 00078 private: 00079 float RGBA[4]; 00080 }; 00081 00083 00087 inline Color::Color(float _r, float _g, float _b, float _a) { 00088 r() = _r; 00089 g() = _g; 00090 b() = _b; 00091 a() = _a; 00092 } 00093 00095 00096 inline Color::Color(const VecMath::Vector<4> &x) { 00097 r() = x[0]; 00098 g() = x[1]; 00099 b() = x[2]; 00100 a() = x[3]; 00101 } 00102 00104 00105 inline Color::Color(const QColor &col) { 00106 qreal qr, qg, qb, qa; 00107 col.getRgbF(&qr, &qg, &qb, &qa); 00108 r() = qr; 00109 g() = qg; 00110 b() = qb; 00111 a() = qa; 00112 } 00113 00116 inline Color Color::operator *=(float x) { 00117 for (unsigned i = 0; i < 3; i++) { 00118 RGBA[i] *= x; 00119 if (RGBA[i] > 1.) RGBA[i] = 1.; 00120 if (RGBA[i] < 0.) RGBA[i] = 0.; 00121 } 00122 return *this; 00123 } 00124 00127 inline Color Color::operator *=(const Color &x) { 00128 for (unsigned i = 0; i < 3; i++) { 00129 RGBA[i] *= x.RGBA[i]; 00130 #if 0 00131 if (RGBA[i] > 1.) RGBA[i] = 1.; 00132 if (RGBA[i] < 0.) RGBA[i] = 0.; 00133 #endif 00134 } 00135 return *this; 00136 } 00137 00140 inline Color Color::operator *(float x) { 00141 Color tmp(*this); 00142 return (tmp *= x); 00143 } 00144 00147 inline Color Color::operator *(const Color &x) { 00148 Color tmp(*this); 00149 return (tmp *= x); 00150 } 00151 00154 inline Color Color::operator +=(float x) { 00155 for (unsigned i = 0; i < 3; i++) { 00156 RGBA[i] += x; 00157 if (RGBA[i] > 1.) RGBA[i] = 1.; 00158 if (RGBA[i] < 0.) RGBA[i] = 0.; 00159 } 00160 return *this; 00161 } 00162 00165 inline Color Color::operator +(float x) { 00166 Color tmp(*this); 00167 return (tmp += x); 00168 } 00169 00172 inline Color Color::operator +=(const Color &x) { 00173 for (unsigned i = 0; i < 3; i++) { 00174 RGBA[i] += x.RGBA[i]; 00175 # if 0 00176 if (RGBA[i] > 1.) RGBA[i] = 1.; 00177 if (RGBA[i] < 0.) RGBA[i] = 0.; 00178 # endif 00179 } 00180 return *this; 00181 } 00182 00185 inline Color Color::operator +(const Color &x) { 00186 Color tmp(*this); 00187 return (tmp += x); 00188 } 00189 00192 inline void Color::setComponentLowerLimit(float l) { 00193 for (unsigned i = 0; i < 3; i++) if (RGBA[i] < l) RGBA[i] = l; 00194 } 00195 00198 inline void Color::setComponentUpperLimit(float u) { 00199 for (unsigned i = 0; i < 3; i++) if (RGBA[i] > u) RGBA[i] = u; 00200 } 00201 00202 #endif