Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CEGUIcolour.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIcolour.cpp
00003         created:        20/8/2004
00004         author:         Paul D Turner (with code from Jeff Leigh)
00005         
00006         purpose:        Implementation of colour class methods.
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "CEGUIcolour.h"
00027 #include <algorithm>
00028 
00029 
00030 // Start of CEGUI namespace section
00031 namespace CEGUI
00032 {
00033 /*************************************************************************
00034         Construction & Destruction
00035 *************************************************************************/
00036 colour::colour(void) :
00037         d_alpha(1.0f),
00038         d_red(0.0f),
00039         d_green(0.0f),
00040         d_blue(0.0f),
00041         d_argb(0xFF000000),
00042         d_argbValid(true)
00043 {
00044 }
00045 
00046 
00047 colour::colour(const colour& val)
00048 {
00049         this->operator=(val);
00050 }
00051 
00052 
00053 colour::colour(float red, float green, float blue, float alpha) :
00054         d_alpha(alpha),
00055         d_red(red),
00056         d_green(green),
00057         d_blue(blue),
00058         d_argbValid(false)
00059 {
00060 }
00061 
00062 
00063 colour::colour(ulong argb)
00064 {
00065         setARGB(argb);
00066 }
00067 
00068 
00069 colour::~colour(void)
00070 {
00071 }
00072 
00073 
00074 float colour::getHue(void) const
00075 {
00076         float pRed = d_red;
00077         float pGreen = d_green;
00078         float pBlue = d_blue;
00079 
00080         float pMax = ceguimax(ceguimax(d_red, d_green), d_blue);
00081         float pMin = ceguimin(ceguimin(d_red, d_green), d_blue);
00082 
00083         float pHue;
00084 
00085         if( pMax == pMin )
00086         {
00087                 pHue = 0;
00088         }
00089         else
00090         {
00091                 if( pMax == pRed )
00092                 {
00093                         pHue = (pGreen - pBlue) / (pMax - pMin);
00094                 }
00095                 else if( pMax == pGreen )
00096                 {
00097                         pHue = 2 + (pBlue - pRed) / (pMax - pMin);
00098                 }
00099                 else
00100                 {
00101                         pHue = 4 + (pRed - pGreen) / (pMax - pMin);
00102                 }
00103         }
00104 
00105         float Hue = pHue / 6;
00106         if( Hue < 0 )
00107                 Hue += 1;
00108 
00109         return Hue;
00110 }
00111 
00112 
00113 float colour::getSaturation(void) const
00114 {
00115         float pRed = d_red;
00116         float pGreen = d_green;
00117         float pBlue = d_blue;
00118 
00119         float pMax = ceguimax(ceguimax(d_red, d_green), d_blue);
00120         float pMin = ceguimin(ceguimin(d_red, d_green), d_blue);
00121 
00122         float pLum = (pMax + pMin) / 2;
00123         float pSat;
00124 
00125         if( pMax == pMin )
00126         {
00127                 pSat = 0;
00128         }
00129         else
00130         {
00131                 if( pLum < 0.5 )
00132                         pSat = (pMax - pMin) / (pMax + pMin);
00133                 else
00134                         pSat = (pMax - pMin) / (2 - pMax - pMin);
00135         }
00136 
00137         return pSat;
00138 }
00139 
00140 
00141 float colour::getLumination(void) const
00142 {
00143         float pRed = d_red;
00144         float pGreen = d_green;
00145         float pBlue = d_blue;
00146 
00147         float pMax = ceguimax(ceguimax(d_red, d_green), d_blue);
00148         float pMin = ceguimin(ceguimin(d_red, d_green), d_blue);
00149 
00150         float pLum = (pMax + pMin) / 2;
00151         return pLum;
00152 }
00153 
00154 
00155 void colour::setARGB(ulong argb)
00156 {
00157         d_argb = argb;
00158 
00159         d_blue  = static_cast<float>(argb & 0xFF) / 255.0f;
00160         argb >>= 8;
00161         d_green = static_cast<float>(argb & 0xFF) / 255.0f;
00162         argb >>= 8;
00163         d_red   = static_cast<float>(argb & 0xFF) / 255.0f;
00164         argb >>= 8;
00165         d_alpha = static_cast<float>(argb & 0xFF) / 255.0f;
00166 
00167         d_argbValid = true;
00168 }
00169 
00170 
00171 void colour::setHSL(float hue, float saturation, float luminance, float alpha)
00172 {
00173         d_alpha = alpha;
00174 
00175         float temp3[3];
00176 
00177         float pHue = hue;
00178         float pSat = saturation;
00179         float pLum = luminance;
00180 
00181         if( pSat == 0 )
00182         {
00183                 d_red = pLum;
00184                 d_green = pLum;
00185                 d_blue = pLum;
00186         }
00187         else
00188         {
00189                 float temp2;
00190                 if( pLum < 0.5f )
00191                 {
00192                         temp2 = pLum * (1 + pSat);
00193                 }
00194                 else
00195                 {
00196                         temp2 = pLum + pSat - pLum * pSat;
00197                 }
00198 
00199                 float temp1 = 2 * pLum - temp2;
00200 
00201                 temp3[0] = pHue + (1.0f / 3);
00202                 temp3[1] = pHue;
00203                 temp3[2] = pHue - (1.0f / 3);
00204 
00205                 for( int n = 0; n < 3; n ++ )
00206                 {
00207                         if( temp3[n] < 0 )
00208                                 temp3[n] ++;
00209                         if( temp3[n] > 1 )
00210                                 temp3[n] --;
00211 
00212                         if( (temp3[n] * 6) < 1 )
00213                         {
00214                                 temp3[n] = temp1 + (temp2 - temp1) * 6 * temp3[n];
00215                         }
00216                         else
00217                         {
00218                                 if( (temp3[n] * 2) < 1 )
00219                                 {
00220                                         temp3[n] = temp2;
00221                                 }
00222                                 else
00223                                 {
00224                                         if( (temp3[n] * 3) < 2 )
00225                                         {
00226                                                 temp3[n] = temp1 + (temp2 - temp1) * ((2.0f / 3) - temp3[n]) * 6;
00227                                         }
00228                                         else
00229                                         {
00230                                                 temp3[n] = temp1;
00231                                         }
00232                                 }
00233                         }
00234                 }
00235 
00236                 d_red = temp3[0];
00237                 d_green = temp3[1];
00238                 d_blue = temp3[2];
00239         }
00240 
00241         d_argbValid = false;
00242 }
00243 
00244 
00245 ulong colour::calculateARGB(void) const
00246 {
00247         return (
00248                 static_cast<ulong>(d_alpha * 255) << 24 |
00249                 static_cast<ulong>(d_red * 255) << 16 |
00250                 static_cast<ulong>(d_green * 255) << 8 |
00251                 static_cast<ulong>(d_blue * 255)
00252         );
00253 }
00254 
00255 } // End of  CEGUI namespace section

Generated on Wed Feb 16 12:41:05 2005 for Crazy Eddies GUI System by  doxygen 1.3.9.1