Main Page   Class Hierarchy   Compound List   File List   Compound Members  

color.h

00001 /*============================================================================
00002         File: color.h
00003      Purpose: 
00004     Revision: $Id: color.h,v 1.3 2003/01/13 19:40:57 philosophil Exp $
00005   Created by: Philippe Lavoie          (26 January 1999)
00006  Modified by: Martin Schuerch
00007 
00008  Copyright notice:
00009           Copyright (C) 1996-1999 Philippe Lavoie
00010  
00011           This library is free software; you can redistribute it and/or
00012           modify it under the terms of the GNU Library General Public
00013           License as published by the Free Software Foundation; either
00014           version 2 of the License, or (at your option) any later version.
00015  
00016           This library is distributed in the hope that it will be useful,
00017           but WITHOUT ANY WARRANTY; without even the implied warranty of
00018           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019           Library General Public License for more details.
00020  
00021           You should have received a copy of the GNU Library General Public
00022           License along with this library; if not, write to the Free
00023           Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024 =============================================================================*/
00025 #ifndef _Matrix_color_h_
00026 #define _Matrix_color_h_
00027 
00028 #include "matrix_global.h"
00029 #include "matrixTool.h"
00030 
00033 namespace PLib {
00034 
00045   class Color {
00046   public:
00047     unsigned char r,g,b ;
00048     Color(const unsigned char R=0, const unsigned char G=0, const unsigned char B=0) : r(R),g(G),b(B) {} 
00049     Color& operator+=(const Color& a) { r+=a.r ; g+=a.g ; b+=a.b; return *this ;} // the += operator
00050     Color& operator-=(const Color& a) { r+=a.r ; g+=a.g ; b+=a.b; return *this;} // the -= operator
00051     Color& operator*=(double a) { r = (unsigned char)(a*double(r)) ; g = (unsigned char)(a*double(g)) ; b = (unsigned char)(a*double(b)) ; return *this ; }
00052     Color& operator/=(double a) { r = (unsigned char)(double(r)/a) ; g = (unsigned char)(double(g)/a) ; b = (unsigned char)(double(b)/a) ; return *this ; }
00053     Color& operator=(const Color& a) { r=a.r ; g=a.g; b=a.b ; return *this ; } // the assignment operator
00054 
00055     void fromXYZ(double x, double y, double z) ;
00056     void toXYZ(double& x, double& y, double& z) ;
00057 
00058     void fromYIQ(double q, double i, double y);
00059     void toYIQ(double& q, double& i, double& y);
00060 
00061     void fromHSV(double h, double s, double v);
00062     void toHSV(double& h, double& s, double& v);
00063 
00064 
00065     friend Color operator*(const double d, const Color& a) ; // multiplies a color by a double 
00066     friend Color operator*(const Color& a, const Color& b) ; // multiplies a color by another color
00067     friend Color operator+(const Color& a, const Color& b) ; // Addition of two colors
00068     
00069     
00070     friend ostream& operator<<(ostream& os, const Color& point);
00071     friend istream& operator>>(istream& os, Color& point);
00072   };
00073   
00074   
00075   inline int operator==(const Color& a, const Color& b) { return (b.r==a.r)&&(b.g==a.g)&&(b.b==a.b) ; } // the equality operator
00076   inline int operator!=(const Color& a, const Color& b) { return !((b.r==a.r)||(b.g==a.g)||(b.b==a.b));} // the inequality operator
00077   
00078   
00079   inline int operator<(const Color& a, const Color& b){ 
00080     return (a.r<b.r && a.g<b.g && a.b<b.b) ; } // the smaller than operator
00081   inline int operator>(const Color& a, const Color& b){ 
00082     return (a.r>b.r && a.g>b.g && a.b>b.b) ; } // the greater than operator
00083   inline int operator<=(const Color& a, const Color& b){ 
00084     return (a.r<=b.r && a.g<=b.g && a.b<=b.b) ; } // the smaller or equal operator
00085   inline int operator>=(const Color& a, const Color& b){ 
00086     return (a.r>=b.r && a.g>=b.g && a.b>=b.b) ; } // the greater or equal operator
00087   
00088   const Color whiteColor(255,255,255);
00089   const Color redColor(255,0,0) ;
00090   const Color blueColor(0,0,255) ;
00091   const Color greenColor(0,255,0) ;
00092   const Color yellowColor(255,255,0) ;
00093   const Color cyanColor(0,255,255) ;
00094   const Color magentaColor(255,0,255);
00095   const Color gray80Color(204,204,204) ;
00096   const Color gray50Color(127,127,127) ;
00097   const Color blackColor(0,0,0) ;
00098   /*
00099   extern Color whiteColor ;
00100   extern Color redColor ;
00101   extern Color blueColor ;
00102   extern Color greenColor ;
00103   extern Color yellowColor ;
00104   extern Color cyanColor ;
00105   extern Color magentaColor ;
00106   extern Color gray80Color ;
00107   extern Color gray50Color ;
00108   extern Color blackColor ;
00109   */
00110 
00111   inline Color operator*(const double d, const Color& a) {
00112     Color result ;
00113     result.r = (unsigned char) (d*a.r) ;
00114     result.g = (unsigned char) (d*a.g) ;
00115     result.b = (unsigned char) (d*a.b) ;
00116     return result ;
00117   }
00118   
00119   inline Color operator*(const Color& a, const Color& b) {
00120     Color result;
00121     result.r = a.r*b.r ;
00122     result.g = a.g*b.g ;
00123     result.b = a.b*b.b ;
00124     return result ;
00125   }
00126   
00127   inline Color operator+(const Color& a, const Color& b) {
00128     Color result(a);
00129     result.r += b.r ;
00130     result.g += b.g ;
00131     result.b += b.b ;
00132     return result ;
00133   }
00134   
00145   class ColorF {
00146   public:
00147     float r,g,b ;
00148     
00149     ColorF(float R=0.0, float G=0.0, float B=0.0) : r(R),g(G),b(B) {} 
00150     
00151     ColorF& operator=(const ColorF& a) { r=a.r ; g=a.g; b=a.b ; return *this ; }
00152   };
00153   
00154 
00155 
00156 
00157 
00168   inline ostream& operator<<(ostream& os,const Color& c)
00169   {
00170     os << (int)c.r << " " << (int)c.g << " " << (int)c.b << " " ;
00171     return os;  
00172   }
00173 
00185   inline istream& operator>>(istream& os, Color& c){
00186     os >> c.r >> c.g >> c.b ;
00187     return os ;
00188   }
00189 
00196   inline void Color::fromXYZ(double x, double y, double z){
00197     r = (unsigned char)(255.0*(3.240479*x-1.537510*y-0.498535*z));
00198     g = (unsigned char)(255.0*(-0.969256*x+1.875992*y+0.041556*z));
00199     b = (unsigned char)(255.0*(0.055648*x-0.204043*y+1.057311*z));
00200   }
00201 
00202   inline void Color::toXYZ(double &x, double& y, double& z){
00203     
00204   }
00205 
00218   inline void Color::fromYIQ(double y, double i, double q){
00219     r = (unsigned char)(255.0*(1.0030893*y+0.954849*i+0.6178597*q));
00220     g = (unsigned char)(255.0*(0.9967760*y-0.27070623*i-0.64478833*q));
00221     b = (unsigned char)(255.0*(1.0084978*y-1.11048518*i+1.69956753125));
00222   }
00223 
00236   inline void Color::toYIQ(double &y, double &i, double &q){
00237     double R= double(r)/255.0 ;
00238     double G= double(g)/255.0 ;
00239     double B= double(b)/255.0 ;
00240     y = 0.299*R + 0.587*G + 0.114*B ;
00241     i = 0.596*R - 0.275*G - 0.321*B ;
00242     q = 0.212*R - 0.528*G + 0.311*B ;
00243   }
00244 
00255   inline void Color::fromHSV(double h, double s, double v){
00256     if(s==0.0){
00257       r=g=b=0;
00258       return;
00259     }
00260     if(h>=360)
00261       h = 0.0 ;
00262     if(h<=0.0)
00263       h = 0.0 ;
00264     h /= 60.0 ;
00265     int i = int(floor(h)) ;
00266     double f = h-double(i);
00267     double p = v*(1.0-s);
00268     double q = v*(1-(s*f));
00269     double t = v*(1-(s*(1-f)));
00270     switch(i){
00271     case 0: 
00272       r = (unsigned char)(255.0*v) ; 
00273       g = (unsigned char)(255.0*t) ; 
00274       b = (unsigned char)(255.0*p) ; break ; 
00275     case 1: 
00276       r = (unsigned char)(255.0*q) ; 
00277       g = (unsigned char)(255.0*v) ; 
00278       b = (unsigned char)(255.0*p) ; break ; 
00279     case 2: 
00280       r = (unsigned char)(255.0*p) ; 
00281       g = (unsigned char)(255.0*v) ; 
00282       b = (unsigned char)(255.0*t) ; break ; 
00283     case 3: 
00284       r = (unsigned char)(255.0*p) ; 
00285       g = (unsigned char)(255.0*q) ; 
00286       b = (unsigned char)(255.0*v) ; break ; 
00287     case 4: 
00288       r = (unsigned char)(255.0*t) ; 
00289       g = (unsigned char)(255.0*p) ; 
00290       b = (unsigned char)(255.0*v) ; break ; 
00291     case 5: 
00292     default:
00293       r = (unsigned char)(255.0*v) ; 
00294       g = (unsigned char)(255.0*p) ; 
00295       b = (unsigned char)(255.0*q) ; break ; 
00296     }
00297   }
00298 
00309   inline void Color::toHSV(double &h, double &s, double &v){
00310     double R = double(r)/255.0;
00311     double G = double(g)/255.0;
00312     double B = double(b)/255.0;
00313     
00314     double max = maximum(R,maximum(G,B));
00315     double min = minimum(R,minimum(G,B));
00316     
00317     int maxI = maximum(r,maximum(g,b));
00318 
00319     v = max ; 
00320     s = (max>0) ? (max-min)/max : 0 ; 
00321     h = 0 ;
00322     if(s>0){
00323       double delta = max-min ;
00324       if(r==maxI){
00325         h = (G-B)/delta ;
00326       }
00327       else{
00328         if(g==maxI){
00329           h = 2.0 + (B-R)/delta ;
00330         }
00331         else{
00332           h = 4.0 + (R-G)/delta ;
00333         }
00334       }
00335       h *= 60.0 ;
00336       if(h<0)
00337         h += 360.0 ; 
00338     }
00339   }
00340 
00341 
00342 } // end namespace
00343 
00344 
00345 #endif

Generated on Tue Jun 24 13:26:54 2003 for NURBS++ by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002