Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Color.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: Color.cpp,v 1.5 2002/02/16 12:41:39 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/Graphics/Color.h"
00026 #include "Teddy/Graphics/Device.h"
00027 #include "Teddy/SysSupport/StdIO.h"
00028 
00029 
00030 namespace Teddy    {
00031 namespace Graphics {
00032 
00033 
00034 Color Color::BLACK         = Color( C_BLACK         );
00035 Color Color::WHITE         = Color( C_WHITE         );
00036 Color Color::RED           = Color( C_RED           );
00037 Color Color::GREEN         = Color( C_GREEN         );
00038 Color Color::BLUE          = Color( C_BLUE          );
00039 Color Color::GRAY          = Color( C_GRAY          );
00040 Color Color::CYAN          = Color( C_CYAN          );
00041 Color Color::MAGENTA       = Color( C_MAGENTA       );
00042 Color Color::YELLOW        = Color( C_YELLOW        );
00043 Color Color::ORANGE        = Color( C_ORANGE        );
00044 Color Color::DARK_RED      = Color( C_DARK_RED      );
00045 Color Color::DARK_GREEN    = Color( C_DARK_GREEN    );
00046 Color Color::DARK_BLUE     = Color( C_DARK_BLUE     );
00047 Color Color::DARK_CYAN     = Color( C_DARK_CYAN     );
00048 Color Color::DARK_MAGENTA  = Color( C_DARK_MAGENTA  );
00049 Color Color::DARK_YELLOW   = Color( C_DARK_YELLOW   );
00050 Color Color::DARK_ORANGE   = Color( C_DARK_ORANGE   );
00051 Color Color::LIGHT_RED     = Color( C_LIGHT_RED     );
00052 Color Color::LIGHT_GREEN   = Color( C_LIGHT_GREEN   );
00053 Color Color::LIGHT_BLUE    = Color( C_LIGHT_BLUE    );
00054 Color Color::LIGHT_CYAN    = Color( C_LIGHT_CYAN    );
00055 Color Color::LIGHT_MAGENTA = Color( C_LIGHT_MAGENTA );
00056 Color Color::LIGHT_YELLOW  = Color( C_LIGHT_YELLOW  );
00057 Color Color::LIGHT_ORANGE  = Color( C_LIGHT_ORANGE  );
00058 Color Color::GRAY_25       = Color( C_GRAY_25       );
00059 Color Color::GRAY_50       = Color( C_GRAY_50       );
00060 Color Color::GRAY_75       = Color( C_GRAY_75       );
00061 
00062 
00064 Color::Color(){
00065     this->rgba[0] = 0;
00066     this->rgba[1] = 0;
00067     this->rgba[2] = 0;
00068     this->rgba[3] = 1;
00069 }
00070 
00071 
00078 Color::Color( const float r, const float g, const float b ){
00079     this->rgba[0] = r;
00080     this->rgba[1] = g;
00081     this->rgba[2] = b;
00082     this->rgba[3] = 1;
00083 }
00084 
00085 
00093 Color::Color( const float r, const float g, const float b, const float a ){
00094     this->rgba[0] = r;
00095     this->rgba[1] = g;
00096     this->rgba[2] = b;
00097     this->rgba[3] = a;
00098 }
00099 
00100 
00105 Color::Color( const float rgba[4] ){
00106     this->rgba[0] = rgba[0];
00107     this->rgba[1] = rgba[1];
00108     this->rgba[2] = rgba[2];
00109     this->rgba[3] = rgba[3];
00110 }
00111 
00112 
00114 void Color::debug(){
00115 //  cout << "( " << rgba[0] << ", " << rgba[1] << ", " << rgba[2] << "; " << rgba[3] << " )";
00116 }
00117 
00118 
00120 void Color::glApply() const {
00121     glColor4fv( rgba );
00122 }
00123 
00124 
00131 Color Color::operator+( const Color &c ) const {
00132     return Color(
00133         rgba[0] + c.rgba[0],
00134         rgba[1] + c.rgba[1],
00135         rgba[2] + c.rgba[2],
00136         rgba[3] + c.rgba[3]
00137     );
00138 }
00139 
00140 
00147 Color &Color::operator+=( const Color &c ){
00148     rgba[0] += c.rgba[0];
00149     rgba[1] += c.rgba[1];
00150     rgba[2] += c.rgba[2];
00151     rgba[3] += c.rgba[3];
00152     return *this;
00153 }
00154 
00155 
00162 Color Color::operator-( const Color &c ) const {
00163     return Color(
00164         rgba[0] - c.rgba[0],
00165         rgba[1] - c.rgba[1],
00166         rgba[2] - c.rgba[2],
00167         rgba[3] - c.rgba[3]
00168     );
00169 }
00170 
00171 
00178 Color &Color::operator-=( const Color &c ){
00179     rgba[0] -= c.rgba[0];
00180     rgba[1] -= c.rgba[1];
00181     rgba[2] -= c.rgba[2];
00182     rgba[3] -= c.rgba[3];
00183     return *this;
00184 }
00185 
00186 
00193 Color Color::operator*( const float &k ) const {
00194     return Color(
00195         rgba[0] * k,
00196         rgba[1] * k,
00197         rgba[2] * k,
00198         rgba[3] * k
00199     );
00200 }
00201 
00202 
00209 Color &Color::operator*=( const float &k ){
00210     rgba[0] *= k;
00211     rgba[1] *= k;
00212     rgba[2] *= k;
00213     rgba[3] *= k;
00214     return( *this );
00215 }
00216 
00217 
00218 
00219 // Needed by rgb2hsv()
00220 float Color::maxrgb( float r, float g, float b ){
00221   float max;
00222   
00223   if( r > g){
00224     max = r;
00225   }else{
00226     max = g;
00227   }
00228   if( b > max ){
00229     max = b;
00230   }
00231   return max;
00232 }
00233 
00234 
00235 // Needed by rgb2hsv()
00236 float Color::minrgb( float r, float g, float b ){
00237   float min;
00238   
00239   if( r < g ){
00240     min = r;
00241   }else{
00242     min = g;
00243   }
00244   if( b < min ){
00245     min = b;
00246   }
00247   return min;
00248 }
00249 
00250 /* Taken from "Fund'l of 3D Computer Graphics", Alan Watt (1989)
00251    Assumes (r,g,b) range from 0.0 to 1.0
00252    Sets h in degrees: 0.0 to 360.;
00253       s,v in [0.,1.]
00254 */
00255 void Color::rgb2hsv( float r, float g, float b, float *hout, float *sout, float *vout ){
00256   float h=0;
00257   float s=1.0;
00258   float v=1.0;
00259   float max_v,min_v,diff,r_dist,g_dist,b_dist;
00260   float undefined = 0.0;
00261 
00262   max_v = maxrgb(r,g,b);
00263   min_v = minrgb(r,g,b);
00264   diff  = max_v - min_v;
00265   v     = max_v;
00266 
00267   if( max_v != 0 ){
00268     s = diff/max_v;
00269   }else{
00270     s = 0.0;
00271   }
00272   if( s == 0 ){
00273     h = undefined;
00274   }else {
00275     r_dist = (max_v - r)/diff;
00276     g_dist = (max_v - g)/diff;
00277     b_dist = (max_v - b)/diff;
00278     if( r == max_v ){
00279       h = b_dist - g_dist;
00280     }else{
00281         if( g == max_v ){
00282             h = 2 + r_dist - b_dist;
00283         }else{
00284             if( b== max_v ){
00285                 h = 4 + g_dist - r_dist;
00286             }else{
00287                 printf("rgb2hsv::How did I get here?\n");
00288             }
00289         }
00290     }
00291     h *= 60;
00292     if( h < 0){
00293       h += 360.0;
00294     }
00295   }
00296   *hout = h;
00297   *sout = s;
00298   *vout = v;
00299 }
00300 
00301 /* Taken from "Fund'l of 3D Computer Graphics", Alan Watt (1989)
00302    Assumes H in degrees, s,v in [0.,1.0];
00303    (r,g,b) range from 0.0 to 1.0
00304 */
00305 void Color::hsv2rgb( float hin, float s, float v, float *rout, float *gout, float *bout ){
00306   float h;                                                                       
00307   float r=0;
00308   float g=0;
00309   float b=0;
00310   float f,p,q,t;
00311   int   i;
00312 
00313   h = hin;
00314   if( s == 0 ) {
00315     r = v;
00316     g = v;
00317     b = v;
00318   }else {
00319       if(h == 360.){
00320         h = 0.0;
00321       }
00322     h /= 60.;
00323     i = (int) h;
00324     f = h - i;
00325     p = v*(1-s);
00326     q = v*(1-(s*f));
00327     t = v*(1-s*(1-f));
00328     switch(i) {
00329     case 0:
00330       r = v;
00331       g = t;
00332       b = p;
00333       break;
00334     case 1:
00335       r = q;
00336       g = v;
00337       b = p;
00338       break;
00339     case 2:
00340       r = p;
00341       g = v;
00342       b = t;
00343       break;
00344     case 3:
00345       r = p;
00346       g = q;
00347       b = v;
00348       break;
00349     case 4:
00350       r = t;
00351       g = p;
00352       b = v;
00353       break;
00354     case 5:
00355       r = v;
00356       g = p;
00357       b = q;
00358       break;
00359     default:
00360       r = 1.0;
00361       b = 1.0;
00362       b = 1.0;
00363       //printf("hsv2rgb::How did I get here?\n");
00364       // printf("h: %f, s: %f, v: %f; i:  %d\n",hin,s,v,i);
00365       break;
00366     }
00367   }
00368   *rout = r;
00369   *gout = g;
00370   *bout = b;
00371 }
00372 
00373 
00374 };  //  namespace Graphics
00375 };  //  namespace Teddy