## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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
    00123 class csColor4 : public csColor
    00124 {
    00125 public:
    00127   float alpha;
    00128
    00130   csColor4 () { }
    00132   csColor4 (float r, float g, float b, float a = 1.0f) : csColor (r, g, b)
    00133   { alpha = a; }
    00134   csColor4 (const csColor& c) : csColor (c), alpha (1.0f) { }
    00135   void Set (const csColor& c)
    00136   {
    00137     red = c.red;
    00138     green = c.green;
    00139     blue = c.blue;
    00140     alpha = 1.0f;
    00141   }
    00142   void Set (const csColor4& c)
    00143   {
    00144     red = c.red;
    00145     green = c.green;
    00146     blue = c.blue;
    00147     alpha = c.alpha;
    00148   }
    00149   void Set (float r, float g, float b)
    00150   {
    00151     red = r;
    00152     green = g;
    00153     blue = b;
    00154     alpha = 1.0f;
    00155   }
    00156   void Set (float r, float g, float b, float a)
    00157   {
    00158     red = r;
    00159     green = g;
    00160     blue = b;
    00161     alpha = a;
    00162   }
    00164   csColor4& operator= (const csColor4& c)
    00165   {
    00166     red = c.red;
    00167     green = c.green;
    00168     blue = c.blue;
    00169     alpha = c.alpha;
    00170     return *this;
    00171   }
    00173   csColor4& operator= (const csColor& c)
    00174   { red = c.red; green = c.green; blue = c.blue; alpha = 1.0f; return *this; }
    00176   csColor4& operator*= (float f)
    00177   { red *= f; green *= f; blue *= f; alpha *= f; return *this; }
    00179   csColor4& operator+= (const csColor4& c)
    00180   {
    00181     red += c.red;
    00182     green += c.green;
    00183     blue += c.blue;
    00184     alpha += c.alpha;
    00185     return *this;
    00186   }
    00188   csColor4& operator+= (const csColor& c)
    00189   { red += c.red; green += c.green; blue += c.blue; return *this; }
    00191   csColor4& operator-= (const csColor4& c)
    00192   {
    00193     red -= c.red;
    00194     green -= c.green;
    00195     blue -= c.blue;
    00196     alpha -= c.alpha;
    00197     return *this;
    00198   }
    00200   csColor& operator-= (const csColor& c)
    00201   { red -= c.red; green -= c.green; blue -= c.blue; return *this; }
    00203   bool operator== (const csColor4& c) const
    00204   {
    00205     return red == c.red &&
    00206            green == c.green &&
    00207            blue == c.blue &&
    00208            alpha == c.alpha;
    00209   }
    00211   bool operator!= (const csColor4& c) const
    00212   {
    00213     return red != c.red ||
    00214            green != c.green ||
    00215            blue != c.blue ||
    00216            alpha != c.alpha;
    00217   }
    00218 };
    00219
    00221 inline csColor operator* (const csColor& s, float f)
    00222 { csColor c (s); c *= f; return c; }
    00223
    00225 inline csColor operator* (float f, const csColor& s)
    00226 { csColor c (s); c *= f; return c; }
    00227
    00229 inline csColor operator+ (const csColor& s1, const csColor& s2)
    00230 { csColor c (s1); c += s2; return c; }
    00232 inline csColor operator- (const csColor& s1, const csColor& s2)
    00233 { csColor c (s1); c -= s2; return c; }
    00234
    00235 #endif // __CS_CSCOLOR_H__