00001 #ifndef color_H
00002 #define color_H
00003
00004 #include <stdlib.h>
00005
00006 namespace math {
00007 template <typename T> class color {
00008 public:
00009 struct type {
00010 enum e: char {
00011 CONST,
00012 SINE,
00013 SAW,
00014 SQUARE,
00015 };
00016 };
00017
00018 color() {
00019 r = g = b = a = 0.0f;
00020 fr = fg = fb = 0.0;
00021 tr = tg = tb = type::e::CONST;
00022 }
00023 color(double newR, double newG, double newB, double newA) {
00024 r = newR;
00025 g = newG;
00026 b = newB;
00027 a = newA;
00028 }
00029 color(double newr, double newg, double newb, double newA, char newtr, char newtg, char newtb) {
00030
00031 tr = newtr;
00032 tg = newtg;
00033 tb = newtb;
00034
00035 switch(tr)
00036 {
00037 case type::e::SINE:
00038 r = 0.0;
00039 fr = newr;
00040 break;
00041 case type::e::SAW:
00042 r = 0.0;
00043 fr = newr;
00044 break;
00045 case type::e::CONST:
00046 default:
00047 r = newr;
00048 fr = 0.0;
00049 break;
00050
00051 }
00052 switch(tb)
00053 {
00054 case type::e::SINE:
00055 b = 0.0;
00056 fb = newb;
00057 break;
00058 case type::e::SAW:
00059 b = 0.0;
00060 fb = newb;
00061 break;
00062 case type::e::CONST:
00063 default:
00064
00065 b = newb;
00066 fb = 0.0;
00067 break;
00068
00069 }
00070 switch(tr)
00071 {
00072 case type::e::SINE:
00073 g = 0.0;
00074 fg = newg;
00075 break;
00076 case type::e::SAW:
00077 g = 0.0;
00078 fg = newg;
00079 break;
00080 case type::e::CONST:
00081 default:
00082 g = newg;
00083 fg = 0.0;
00084 break;
00085
00086 }
00087
00088 a = newA;
00089 }
00090
00091
00092
00093
00094
00095
00096 color(const color & rhs) {
00097 r = rhs.r;
00098 g = rhs.g;
00099 b = rhs.b;
00100 a = rhs.a;
00101 fr = rhs.fr;
00102 fg = rhs.fg;
00103 fb = rhs.fb;
00104 tr = rhs.tr;
00105 tg = rhs.tg;
00106 tb = rhs.tb;
00107 }
00108
00109
00110 ~color()
00111 {
00112 }
00113 void set(double newR, double newG, double newB, double newA) {
00114 r=newR;
00115 g=newG;
00116 b=newB;
00117 a=newA;
00118 }
00119 math::color<T> rand() {
00120 color ret;
00121 ret.r = (double)(::rand() % 1000) / 1000.0f;
00122 ret.g = (double)(::rand() % 1000) / 1000.0f;
00123 ret.b = (double)(::rand() % 1000) / 1000.0f;
00124 ret.a = 1.0;
00125 return ret;
00126 }
00127 void setR(double newR) {
00128 r = newR;
00129 }
00130 void setG(double newG) {
00131 g = newG;
00132 }
00133 void setB(double newB) {
00134 b = newB;
00135 }
00136 void setA(double newA) {
00137 a = newA;
00138 }
00139 double getR() const {
00140 return r;
00141 }
00142 double getG() const {
00143 return g;
00144 }
00145 double getB() const {
00146 return b;
00147 }
00148 double getA() const {
00149 return a;
00150 }
00151 void setBlack() {
00152 r = g = b = a = 1.0f;
00153 }
00154 void setWhite() {
00155 r = g = b = 0.0f;
00156 a = 1.0f;
00157 }
00158 void setGrey(double shade) {
00159 r = g = b = shade;
00160 a = 1.0f;
00161 }
00162 math::color<T> lerp(const color & c2, double factor) {
00163 return (*this) * (1.0f - factor) + c2 * factor;
00164 }
00165 math::color<T> operator+(const color & rhs) const {
00166 return color(r+rhs.r, g+rhs.g, b+rhs.b, a+rhs.a);
00167 }
00168 math::color<T> operator-(const color & rhs) const {
00169 return color(r-rhs.r, g-rhs.g, b-rhs.b, a-rhs.a);
00170 }
00171 math::color<T> operator*(const color & rhs) const {
00172 return color(r*rhs.r, g*rhs.g, b*rhs.b, a*rhs.a);
00173 }
00174 math::color<T> operator/(const color & rhs) const {
00175 return color(r/rhs.r, g/rhs.g, b/rhs.b, a/rhs.a);
00176 }
00177 math::color<T> operator*(const double rhs) const {
00178 return color(r*rhs, g*rhs, b*rhs, a*rhs);
00179 }
00180 math::color<T> operator/(const double rhs) const {
00181 return color(r/rhs, g/rhs, b/rhs, a/rhs);
00182 }
00183 bool operator ==(math::color<T> const & rhs) const {
00184 if(r != rhs.r) return false;
00185 if(g != rhs.g) return false;
00186 if(b != rhs.b) return false;
00187 if(a != rhs.a) return false;
00188
00189 return true;
00190 }
00191 bool operator!=(color<T> const & rhs) const {
00192 return !((*this)==rhs);
00193 }
00194 math::color<T> operator+=(color<T> const & rhs) {
00195 r += rhs.r;
00196 g += rhs.g;
00197 b += rhs.b;
00198 a += rhs.a;
00199
00200 return (*this);
00201 }
00202 math::color<T> operator-=(color<T> const & rhs) {
00203 (*this)=(*this)-rhs;
00204 return (*this);
00205 }
00206 math::color<T> operator*=(const color & rhs) {
00207 (*this)=(*this)*rhs; return (*this);
00208 }
00209 math::color<T> operator/=(const color & rhs) {
00210 (*this)=(*this)/rhs; return (*this);
00211 }
00212 math::color<T> operator*=(const double rhs) {
00213 (*this)=(*this)*rhs; return (*this);
00214 }
00215 math::color<T> operator/=(const double rhs) {
00216 (*this)=(*this)/rhs;
00217 return (*this);
00218 }
00219 math::color<T> operator-() const {
00220 return color(-r,-g, -b, -a);
00221 }
00222 math::color<T> operator+() const{
00223 return (*this);
00224 }
00225 void clampTo01() {
00226 if(r>1.0f) r=1.0f;
00227 if(r<0.0f) r=0.0f;
00228
00229 if(g>1.0f) g=1.0f;
00230 if(g<0.0f) g=0.0f;
00231
00232 if(b>1.0f) b=1.0f;
00233 if(b<0.0f) b=0.0f;
00234
00235 if(a>1.0f) a=1.0f;
00236 if(a<0.0f) a=0.0f;
00237 }
00238 void print() {
00239 printf("%f %f %f %f %f %f %f %i %i %i\n",r,g,b,a,fr,fg,fb,tr,tg,tb);
00240 }
00241
00242 double saw(double t, double f) {
00243 double a = t*f;
00244 double y = a - floor(a);
00245 printf("saw: a = %f y = %f\n", a, y);
00246 return y;
00247 }
00248 void step(double time) {
00249
00250
00251 switch(tr) {
00252 case type::e::CONST:
00253 break;
00254 case type::e::SINE:
00255 r = 0.5 * sin(2.0 * M_PI * fr * time) + 0.5;
00256 printf("sine r = %f\n", r);
00257 break;
00258 case type::e::SAW:
00259 r = saw(time, fr);
00260 break;
00261
00262 }
00263 switch(tg) {
00264 case type::e::CONST:
00265 break;
00266 case type::e::SINE:
00267 g = 0.5 * sin(2.0 * M_PI * fg * time) + 0.5;
00268 printf("g = %f\n", g);
00269 break;
00270 case type::e::SAW:
00271 g = saw(time, fg);
00272 break;
00273 }
00274 switch(tb) {
00275 case type::e::CONST:
00276 break;
00277 case type::e::SINE:
00278 b = 0.5 * sin(2.0 * M_PI * fb * time) + 0.5;
00279 break;
00280 case type::e::SAW:
00281 b = saw(time, fb);
00282 break;
00283 }
00284
00285 }
00286 math::color<T> operator*(double f) {
00287 math::color<T> ret(*this);
00288 ret *= f;
00289 return ret;
00290 }
00291 operator double* () const {
00292 return (double*)this;
00293 }
00294 operator double const * () const {
00295 return (double*)this;
00296 }
00297
00298
00299 public:
00300 double r;
00301 double g;
00302 double b;
00303 double a;
00304 double fr;
00305 double fg;
00306 double fb;
00307 char tr;
00308 char tg;
00309 char tb;
00310 };
00311
00312 const color<float> white( 1.0f, 1.0f, 1.0f, 1.0f);
00313 const color<float> black( 0.0f, 0.0f, 0.0f, 1.0f);
00314
00315 const color<float> red( 1.0f, 0.0f, 0.0f, 1.0f);
00316 const color<float> green( 0.0f, 1.0f, 0.0f, 1.0f);
00317 const color<float> blue( 0.0f, 0.0f, 1.0f, 1.0f);
00318
00319 const color<float> cyan( 0.0f, 1.0f, 1.0f, 1.0f);
00320 const color<float> magenta( 1.0f, 0.0f, 1.0f, 1.0f);
00321 const color<float> yellow( 1.0f, 1.0f, 0.0f, 1.0f);
00322 }
00323
00324 #endif //color_H