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)
00047   { red = r; green = g; blue = b; }
00049   csColor (const csColor& c)
00050   { red = c.red; green = c.green; blue = c.blue; }
00052   void Set (float r, float g, float b)
00053   { red = r; green = g; blue = b; }
00055   void Set (const csColor& c)
00056   { red = c.red; green = c.green; blue = c.blue; }
00058   void Clamp (float r, float g, float b)
00059   {
00060     if (red > r) red = r;
00061     if (green > g) green = g;
00062     if (blue > b) blue = b;
00063   }
00065   void ClampDown ()
00066   {
00067     if (red < 0) red = 0;
00068     if (green < 0) green = 0;
00069     if (blue < 0) blue = 0;
00070   }
00072   csColor& operator= (const csColor& c)
00073   { red = c.red; green = c.green; blue = c.blue; return *this; }
00075   csColor& operator*= (float f)
00076   { red *= f; green *= f; blue *= f; return *this; }
00078   csColor& operator+= (const csColor& c)
00079   { red += c.red; green += c.green; blue += c.blue; return *this; }
00081   csColor& operator-= (const csColor& c)
00082   { red -= c.red; green -= c.green; blue -= c.blue; return *this; }
00084   csColor& operator*= (const csColor& c)
00085   { red *= c.red; green *= c.green; blue *= c.blue; return *this; }
00087   csColor operator * (const float f)
00088   {
00089     csColor ret;
00090     ret.red = red*f;
00091     ret.green = green*f;
00092     ret.blue = blue*f;
00093     return ret;
00094   }
00096   bool operator== (const csColor& c) const
00097   { return red == c.red && green == c.green && blue == c.blue; }
00099   bool operator!= (const csColor& c) const
00100   { return red != c.red || green != c.green || blue != c.blue; }
00102   void Add (float r, float g, float b)
00103   { red += r; green += g; blue += b; }
00105   void Subtract (float r, float g, float b)
00106   { red -= r; green -= g; blue -= b; }
00107 };
00108 
00110 inline csColor operator/ (const csColor& v, float f)
00111 { f = 1.0f/f; return csColor(v.red*f, v.green*f, v.blue*f); }
00113 inline csColor operator* (const csColor& v1, const csColor& v2)
00114 {
00115   return csColor (v1.red * v2.red,
00116                   v1.green * v2.green,
00117                   v1.blue * v2.blue);
00118 }
00119 
00120 
00122 inline csColor operator* (const csColor& s, float f)
00123 { csColor c (s); c *= f; return c; }
00124 
00126 inline csColor operator* (float f, const csColor& s)
00127 { csColor c (s); c *= f; return c; }
00128 
00130 inline csColor operator+ (const csColor& s1, const csColor& s2)
00131 { csColor c (s1); c += s2; return c; }
00133 inline csColor operator- (const csColor& s1, const csColor& s2)
00134 { csColor c (s1); c -= s2; return c; }
00135 
00139 class csColor4 : public csColor
00140 {
00141 public:
00143   float alpha;
00144 
00146   csColor4 () { }
00148   csColor4 (float r, float g, float b, float a = 1.0f) : csColor (r, g, b)
00149   { alpha = a; }
00150   csColor4 (const csColor& c) : csColor (c), alpha (1.0f) { }
00151   void Set (const csColor& c)
00152   {
00153     red = c.red;
00154     green = c.green;
00155     blue = c.blue;
00156     alpha = 1.0f;
00157   }
00158   void Set (const csColor4& c)
00159   {
00160     red = c.red;
00161     green = c.green;
00162     blue = c.blue;
00163     alpha = c.alpha;
00164   }
00165   void Set (float r, float g, float b)
00166   {
00167     red = r;
00168     green = g;
00169     blue = b;
00170     alpha = 1.0f;
00171   }
00172   void Set (float r, float g, float b, float a)
00173   {
00174     red = r;
00175     green = g;
00176     blue = b;
00177     alpha = a;
00178   }
00180   csColor4& operator= (const csColor4& c)
00181   {
00182     red = c.red;
00183     green = c.green;
00184     blue = c.blue;
00185     alpha = c.alpha;
00186     return *this;
00187   }
00189   csColor4& operator= (const csColor& c)
00190   { red = c.red; green = c.green; blue = c.blue; alpha = 1.0f; return *this; }
00192   csColor4& operator*= (float f)
00193   { red *= f; green *= f; blue *= f; alpha *= f; return *this; }
00195   csColor4& operator+= (const csColor4& c)
00196   {
00197     red += c.red;
00198     green += c.green;
00199     blue += c.blue;
00200     alpha += c.alpha;
00201     return *this;
00202   }
00204   csColor4& operator+= (const csColor& c)
00205   { red += c.red; green += c.green; blue += c.blue; return *this; }
00207   csColor4& operator-= (const csColor4& c)
00208   {
00209     red -= c.red;
00210     green -= c.green;
00211     blue -= c.blue;
00212     alpha -= c.alpha;
00213     return *this;
00214   }
00216   csColor& operator-= (const csColor& c)
00217   { red -= c.red; green -= c.green; blue -= c.blue; return *this; }
00219   bool operator== (const csColor4& c) const
00220   {
00221     return red == c.red &&
00222            green == c.green &&
00223            blue == c.blue &&
00224            alpha == c.alpha;
00225   }
00227   bool operator!= (const csColor4& c) const
00228   {
00229     return red != c.red ||
00230            green != c.green ||
00231            blue != c.blue ||
00232            alpha != c.alpha;
00233   }
00234 };
00235 
00236 
00238 inline csColor4 operator/ (const csColor4& v, float f)
00239 { f = 1.0f/f; return csColor4(v.red*f, v.green*f, v.blue*f); }
00241 inline csColor4 operator* (const csColor4& v1, const csColor4& v2)
00242 {
00243   return csColor4 (v1.red * v2.red,
00244     v1.green * v2.green,
00245     v1.blue * v2.blue);
00246 }
00247 
00248 
00250 inline csColor4 operator* (const csColor4& s, float f)
00251 { csColor4 c (s); c *= f; return c; }
00252 
00254 inline csColor4 operator* (float f, const csColor4& s)
00255 { csColor4 c (s); c *= f; return c; }
00256 
00258 inline csColor4 operator+ (const csColor4& s1, const csColor4& s2)
00259 { csColor4 c (s1); c += s2; return c; }
00261 inline csColor4 operator- (const csColor4& s1, const csColor4& s2)
00262 { csColor4 c (s1); c -= s2; return c; }
00263 
00264 #endif // __CS_CSCOLOR_H__

Generated for Crystal Space 1.0.2 by doxygen 1.4.7