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:
00037   float red;
00039   float green;
00041   float blue;
00042 public:
00044   csColor () { }
00046   csColor (float r, float g, float b) : red (r), green (g), blue (b)
00047   {}
00049   csColor (float v) : red (v), green (v), blue (v)
00050   {}
00052   csColor (const csColor& c)
00053   { red = c.red; green = c.green; blue = c.blue; }
00055   void Set (float r, float g, float b)
00056   { red = r; green = g; blue = b; }
00058   void Set (const csColor& c)
00059   { red = c.red; green = c.green; blue = c.blue; }
00061   void Clamp (float r, float g, float b)
00062   {
00063     if (red > r) red = r;
00064     if (green > g) green = g;
00065     if (blue > b) blue = b;
00066   }
00068   void ClampDown ()
00069   {
00070     if (red < 0) red = 0;
00071     if (green < 0) green = 0;
00072     if (blue < 0) blue = 0;
00073   }
00075   bool IsBlack () const
00076   {
00077     return (red == 0 && green == 0 && blue == 0);
00078   }
00080   bool IsBlack (float threshold) const
00081   {
00082     return (red < threshold && green < threshold && blue < threshold);
00083   }
00085   csColor& operator= (const csColor& c)
00086   { red = c.red; green = c.green; blue = c.blue; return *this; }
00088   csColor& operator*= (float f)
00089   { red *= f; green *= f; blue *= f; return *this; }
00091   csColor& operator+= (const csColor& c)
00092   { red += c.red; green += c.green; blue += c.blue; return *this; }
00094   csColor& operator-= (const csColor& c)
00095   { red -= c.red; green -= c.green; blue -= c.blue; return *this; }
00097   csColor& operator*= (const csColor& c)
00098   { red *= c.red; green *= c.green; blue *= c.blue; return *this; }
00100   csColor operator * (const float f)
00101   {
00102     csColor ret;
00103     ret.red = red*f;
00104     ret.green = green*f;
00105     ret.blue = blue*f;
00106     return ret;
00107   }
00109   bool operator== (const csColor& c) const
00110   { return red == c.red && green == c.green && blue == c.blue; }
00112   bool operator!= (const csColor& c) const
00113   { return red != c.red || green != c.green || blue != c.blue; }
00115   void Add (float r, float g, float b)
00116   { red += r; green += g; blue += b; }
00118   void Subtract (float r, float g, float b)
00119   { red -= r; green -= g; blue -= b; }
00121   float Luminance() const
00122   { return red*0.2126f + green*0.7152f + blue*0.0722f; }
00123 };
00124 
00126 inline csColor operator/ (const csColor& v, float f)
00127 { f = 1.0f/f; return csColor(v.red*f, v.green*f, v.blue*f); }
00129 inline csColor operator* (const csColor& v1, const csColor& v2)
00130 {
00131   return csColor (v1.red * v2.red,
00132                   v1.green * v2.green,
00133                   v1.blue * v2.blue);
00134 }
00135 
00136 
00138 inline csColor operator* (const csColor& s, float f)
00139 { csColor c (s); c *= f; return c; }
00140 
00142 inline csColor operator* (float f, const csColor& s)
00143 { csColor c (s); c *= f; return c; }
00144 
00146 inline csColor operator+ (const csColor& s1, const csColor& s2)
00147 { csColor c (s1); c += s2; return c; }
00149 inline csColor operator- (const csColor& s1, const csColor& s2)
00150 { csColor c (s1); c -= s2; return c; }
00151 
00155 class csColor4 : public csColor
00156 {
00157 public:
00159   float alpha;
00160 
00162   csColor4 () { }
00164   csColor4 (float r, float g, float b, float a = 1.0f) : csColor (r, g, b)
00165   { alpha = a; }
00166   csColor4 (const csColor& c) : csColor (c), alpha (1.0f) { }
00167   void Set (const csColor& c)
00168   {
00169     red = c.red;
00170     green = c.green;
00171     blue = c.blue;
00172     alpha = 1.0f;
00173   }
00174   void Set (const csColor4& c)
00175   {
00176     red = c.red;
00177     green = c.green;
00178     blue = c.blue;
00179     alpha = c.alpha;
00180   }
00181   void Set (float r, float g, float b)
00182   {
00183     red = r;
00184     green = g;
00185     blue = b;
00186     alpha = 1.0f;
00187   }
00188   void Set (float r, float g, float b, float a)
00189   {
00190     red = r;
00191     green = g;
00192     blue = b;
00193     alpha = a;
00194   }
00196   csColor4& operator= (const csColor4& c)
00197   {
00198     red = c.red;
00199     green = c.green;
00200     blue = c.blue;
00201     alpha = c.alpha;
00202     return *this;
00203   }
00205   csColor4& operator= (const csColor& c)
00206   { red = c.red; green = c.green; blue = c.blue; alpha = 1.0f; return *this; }
00208   csColor4& operator*= (float f)
00209   { red *= f; green *= f; blue *= f; alpha *= f; return *this; }
00211   csColor4& operator+= (const csColor4& c)
00212   {
00213     red += c.red;
00214     green += c.green;
00215     blue += c.blue;
00216     alpha += c.alpha;
00217     return *this;
00218   }
00220   csColor4& operator+= (const csColor& c)
00221   { red += c.red; green += c.green; blue += c.blue; return *this; }
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   csColor& operator-= (const csColor& c)
00233   { red -= c.red; green -= c.green; blue -= c.blue; return *this; }
00235   bool operator== (const csColor4& c) const
00236   {
00237     return red == c.red &&
00238            green == c.green &&
00239            blue == c.blue &&
00240            alpha == c.alpha;
00241   }
00243   bool operator!= (const csColor4& c) const
00244   {
00245     return red != c.red ||
00246            green != c.green ||
00247            blue != c.blue ||
00248            alpha != c.alpha;
00249   }
00250 };
00251 
00252 
00254 inline csColor4 operator/ (const csColor4& v, float f)
00255 { f = 1.0f/f; return csColor4(v.red*f, v.green*f, v.blue*f); }
00257 inline csColor4 operator* (const csColor4& v1, const csColor4& v2)
00258 {
00259   return csColor4 (v1.red * v2.red,
00260     v1.green * v2.green,
00261     v1.blue * v2.blue);
00262 }
00263 
00264 
00266 inline csColor4 operator* (const csColor4& s, float f)
00267 { csColor4 c (s); c *= f; return c; }
00268 
00270 inline csColor4 operator* (float f, const csColor4& s)
00271 { csColor4 c (s); c *= f; return c; }
00272 
00274 inline csColor4 operator+ (const csColor4& s1, const csColor4& s2)
00275 { csColor4 c (s1); c += s2; return c; }
00277 inline csColor4 operator- (const csColor4& s1, const csColor4& s2)
00278 { csColor4 c (s1); c -= s2; return c; }
00279 
00280 #endif // __CS_CSCOLOR_H__

Generated for Crystal Space 1.2.1 by doxygen 1.5.3