CrystalSpace

Public API Reference

csutil/cscolor.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_CSCOLOR_H__
00020 #define __CS_CSCOLOR_H__
00021 
00026 #include "csextern.h"
00027 
00033 class csColor
00034 {
00035 public:
00036 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
00037   union
00038   {
00039     struct 
00040     {
00041 #endif
00042 
00043       float red;
00045       float green;
00047       float blue;
00048 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
00049     };
00051     float m[3];
00052   };
00053 #endif
00054 
00055 public:
00057   csColor () { }
00059   csColor (float r, float g, float b) : red (r), green (g), blue (b)
00060   {}
00062   csColor (float v) : red (v), green (v), blue (v)
00063   {}
00065   csColor (const csColor& c)
00066   { red = c.red; green = c.green; blue = c.blue; }
00068   void Set (float r, float g, float b)
00069   { red = r; green = g; blue = b; }
00071   void Set (const csColor& c)
00072   { red = c.red; green = c.green; blue = c.blue; }
00074   void Clamp (float r, float g, float b)
00075   {
00076     if (red > r) red = r;
00077     if (green > g) green = g;
00078     if (blue > b) blue = b;
00079   }
00081   void ClampDown ()
00082   {
00083     if (red < 0) red = 0;
00084     if (green < 0) green = 0;
00085     if (blue < 0) blue = 0;
00086   }
00088   bool IsBlack () const
00089   {
00090     return (red == 0 && green == 0 && blue == 0);
00091   }
00093   bool IsBlack (float threshold) const
00094   {
00095     return (red < threshold && green < threshold && blue < threshold);
00096   }
00098   csColor& operator= (const csColor& c)
00099   { red = c.red; green = c.green; blue = c.blue; return *this; }
00101   csColor& operator*= (float f)
00102   { red *= f; green *= f; blue *= f; return *this; }
00104   csColor& operator+= (const csColor& c)
00105   { red += c.red; green += c.green; blue += c.blue; return *this; }
00107   csColor& operator-= (const csColor& c)
00108   { red -= c.red; green -= c.green; blue -= c.blue; return *this; }
00110   csColor& operator*= (const csColor& c)
00111   { red *= c.red; green *= c.green; blue *= c.blue; return *this; }
00113   csColor operator * (const float f)
00114   {
00115     csColor ret;
00116     ret.red = red*f;
00117     ret.green = green*f;
00118     ret.blue = blue*f;
00119     return ret;
00120   }
00122   bool operator== (const csColor& c) const
00123   { return red == c.red && green == c.green && blue == c.blue; }
00125   bool operator!= (const csColor& c) const
00126   { return red != c.red || green != c.green || blue != c.blue; }
00128   void Add (float r, float g, float b)
00129   { red += r; green += g; blue += b; }
00131   void Subtract (float r, float g, float b)
00132   { red -= r; green -= g; blue -= b; }
00134   float Luminance() const
00135   { return red*0.2126f + green*0.7152f + blue*0.0722f; }
00136 
00138 #ifdef __STRICT_ANSI__
00139   inline float operator[] (size_t n) const { return !n?red:n&1?green:blue; }
00140 #else
00141   inline float operator[] (size_t n) const { return m[n]; }
00142 #endif
00143 
00145 #ifdef __STRICT_ANSI__
00146   inline float & operator[] (size_t n) { return !n?red:n&1?green:blue; }
00147 #else
00148   inline float & operator[] (size_t n) { return m[n]; }
00149 #endif
00150 };
00151 
00153 inline csColor operator/ (const csColor& v, float f)
00154 { f = 1.0f/f; return csColor(v.red*f, v.green*f, v.blue*f); }
00156 inline csColor operator* (const csColor& v1, const csColor& v2)
00157 {
00158   return csColor (v1.red * v2.red,
00159                   v1.green * v2.green,
00160                   v1.blue * v2.blue);
00161 }
00162 
00163 
00165 inline csColor operator* (const csColor& s, float f)
00166 { csColor c (s); c *= f; return c; }
00167 
00169 inline csColor operator* (float f, const csColor& s)
00170 { csColor c (s); c *= f; return c; }
00171 
00173 inline csColor operator+ (const csColor& s1, const csColor& s2)
00174 { csColor c (s1); c += s2; return c; }
00176 inline csColor operator- (const csColor& s1, const csColor& s2)
00177 { csColor c (s1); c -= s2; return c; }
00178 
00182 class csColor4 : public csColor
00183 {
00184 public:
00186   float alpha;
00187 
00189   csColor4 () { }
00191   csColor4 (float r, float g, float b, float a = 1.0f) : csColor (r, g, b)
00192   { alpha = a; }
00193   csColor4 (const csColor& c) : csColor (c), alpha (1.0f) { }
00194   void Set (const csColor& c)
00195   {
00196     red = c.red;
00197     green = c.green;
00198     blue = c.blue;
00199     alpha = 1.0f;
00200   }
00201   void Set (const csColor4& c)
00202   {
00203     red = c.red;
00204     green = c.green;
00205     blue = c.blue;
00206     alpha = c.alpha;
00207   }
00208   void Set (float r, float g, float b)
00209   {
00210     red = r;
00211     green = g;
00212     blue = b;
00213     alpha = 1.0f;
00214   }
00215   void Set (float r, float g, float b, float a)
00216   {
00217     red = r;
00218     green = g;
00219     blue = b;
00220     alpha = a;
00221   }
00223   csColor4& operator= (const csColor4& c)
00224   {
00225     red = c.red;
00226     green = c.green;
00227     blue = c.blue;
00228     alpha = c.alpha;
00229     return *this;
00230   }
00232   csColor4& operator= (const csColor& c)
00233   { red = c.red; green = c.green; blue = c.blue; alpha = 1.0f; return *this; }
00235   csColor4& operator*= (float f)
00236   { red *= f; green *= f; blue *= f; alpha *= f; return *this; }
00238   csColor4& operator+= (const csColor4& c)
00239   {
00240     red += c.red;
00241     green += c.green;
00242     blue += c.blue;
00243     alpha += c.alpha;
00244     return *this;
00245   }
00247   csColor4& operator+= (const csColor& c)
00248   { red += c.red; green += c.green; blue += c.blue; return *this; }
00250   csColor4& operator-= (const csColor4& c)
00251   {
00252     red -= c.red;
00253     green -= c.green;
00254     blue -= c.blue;
00255     alpha -= c.alpha;
00256     return *this;
00257   }
00259   csColor& operator-= (const csColor& c)
00260   { red -= c.red; green -= c.green; blue -= c.blue; return *this; }
00262   bool operator== (const csColor4& c) const
00263   {
00264     return red == c.red &&
00265            green == c.green &&
00266            blue == c.blue &&
00267            alpha == c.alpha;
00268   }
00270   bool operator!= (const csColor4& c) const
00271   {
00272     return red != c.red ||
00273            green != c.green ||
00274            blue != c.blue ||
00275            alpha != c.alpha;
00276   }
00277 
00279   inline float operator[] (size_t n) const
00280   { return (n == 3) ? alpha : csColor::operator[] (n); }
00281 
00283   inline float & operator[] (size_t n)
00284   { return (n == 3) ? alpha : csColor::operator[] (n); }
00285 };
00286 
00287 
00289 inline csColor4 operator/ (const csColor4& v, float f)
00290 { f = 1.0f/f; return csColor4(v.red*f, v.green*f, v.blue*f, v.alpha*f); }
00292 inline csColor4 operator* (const csColor4& v1, const csColor4& v2)
00293 {
00294   return csColor4 (v1.red * v2.red,
00295     v1.green * v2.green,
00296     v1.blue * v2.blue,
00297     v1.alpha * v2.alpha);
00298 }
00299 
00300 
00302 inline csColor4 operator* (const csColor4& s, float f)
00303 { csColor4 c (s); c *= f; return c; }
00304 
00306 inline csColor4 operator* (float f, const csColor4& s)
00307 { csColor4 c (s); c *= f; return c; }
00308 
00310 inline csColor4 operator+ (const csColor4& s1, const csColor4& s2)
00311 { csColor4 c (s1); c += s2; return c; }
00313 inline csColor4 operator- (const csColor4& s1, const csColor4& s2)
00314 { csColor4 c (s1); c -= s2; return c; }
00315 
00316 #endif // __CS_CSCOLOR_H__

Generated for Crystal Space 2.0 by doxygen 1.7.6.1