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

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(argb_t argb)
00064 {
00065         setARGB(argb);
00066 }
00067 
00068 
00069 float colour::getHue(void) const
00070 {
00071         float pRed = d_red;
00072         float pGreen = d_green;
00073         float pBlue = d_blue;
00074 
00075         float pMax = ceguimax(ceguimax(d_red, d_green), d_blue);
00076         float pMin = ceguimin(ceguimin(d_red, d_green), d_blue);
00077 
00078         float pHue;
00079 
00080         if( pMax == pMin )
00081         {
00082                 pHue = 0;
00083         }
00084         else
00085         {
00086                 if( pMax == pRed )
00087                 {
00088                         pHue = (pGreen - pBlue) / (pMax - pMin);
00089                 }
00090                 else if( pMax == pGreen )
00091                 {
00092                         pHue = 2 + (pBlue - pRed) / (pMax - pMin);
00093                 }
00094                 else
00095                 {
00096                         pHue = 4 + (pRed - pGreen) / (pMax - pMin);
00097                 }
00098         }
00099 
00100         float Hue = pHue / 6;
00101         if( Hue < 0 )
00102                 Hue += 1;
00103 
00104         return Hue;
00105 }
00106 
00107 
00108 float colour::getSaturation(void) const
00109 {
00110         float pMax = ceguimax(ceguimax(d_red, d_green), d_blue);
00111         float pMin = ceguimin(ceguimin(d_red, d_green), d_blue);
00112 
00113         float pLum = (pMax + pMin) / 2;
00114         float pSat;
00115 
00116         if( pMax == pMin )
00117         {
00118                 pSat = 0;
00119         }
00120         else
00121         {
00122                 if( pLum < 0.5 )
00123                         pSat = (pMax - pMin) / (pMax + pMin);
00124                 else
00125                         pSat = (pMax - pMin) / (2 - pMax - pMin);
00126         }
00127 
00128         return pSat;
00129 }
00130 
00131 
00132 float colour::getLumination(void) const
00133 {
00134         float pMax = ceguimax(ceguimax(d_red, d_green), d_blue);
00135         float pMin = ceguimin(ceguimin(d_red, d_green), d_blue);
00136 
00137         float pLum = (pMax + pMin) / 2;
00138         return pLum;
00139 }
00140 
00141 
00142 void colour::setARGB(argb_t argb)
00143 {
00144         d_argb = argb;
00145 
00146         d_blue  = static_cast<float>(argb & 0xFF) / 255.0f;
00147         argb >>= 8;
00148         d_green = static_cast<float>(argb & 0xFF) / 255.0f;
00149         argb >>= 8;
00150         d_red   = static_cast<float>(argb & 0xFF) / 255.0f;
00151         argb >>= 8;
00152         d_alpha = static_cast<float>(argb & 0xFF) / 255.0f;
00153 
00154         d_argbValid = true;
00155 }
00156 
00157 
00158 void colour::setHSL(float hue, float saturation, float luminance, float alpha)
00159 {
00160         d_alpha = alpha;
00161 
00162         float temp3[3];
00163 
00164         float pHue = hue;
00165         float pSat = saturation;
00166         float pLum = luminance;
00167 
00168         if( pSat == 0 )
00169         {
00170                 d_red = pLum;
00171                 d_green = pLum;
00172                 d_blue = pLum;
00173         }
00174         else
00175         {
00176                 float temp2;
00177                 if( pLum < 0.5f )
00178                 {
00179                         temp2 = pLum * (1 + pSat);
00180                 }
00181                 else
00182                 {
00183                         temp2 = pLum + pSat - pLum * pSat;
00184                 }
00185 
00186                 float temp1 = 2 * pLum - temp2;
00187 
00188                 temp3[0] = pHue + (1.0f / 3);
00189                 temp3[1] = pHue;
00190                 temp3[2] = pHue - (1.0f / 3);
00191 
00192                 for( int n = 0; n < 3; n ++ )
00193                 {
00194                         if( temp3[n] < 0 )
00195                                 temp3[n] ++;
00196                         if( temp3[n] > 1 )
00197                                 temp3[n] --;
00198 
00199                         if( (temp3[n] * 6) < 1 )
00200                         {
00201                                 temp3[n] = temp1 + (temp2 - temp1) * 6 * temp3[n];
00202                         }
00203                         else
00204                         {
00205                                 if( (temp3[n] * 2) < 1 )
00206                                 {
00207                                         temp3[n] = temp2;
00208                                 }
00209                                 else
00210                                 {
00211                                         if( (temp3[n] * 3) < 2 )
00212                                         {
00213                                                 temp3[n] = temp1 + (temp2 - temp1) * ((2.0f / 3) - temp3[n]) * 6;
00214                                         }
00215                                         else
00216                                         {
00217                                                 temp3[n] = temp1;
00218                                         }
00219                                 }
00220                         }
00221                 }
00222 
00223                 d_red = temp3[0];
00224                 d_green = temp3[1];
00225                 d_blue = temp3[2];
00226         }
00227 
00228         d_argbValid = false;
00229 }
00230 
00231 
00232 argb_t colour::calculateARGB(void) const
00233 {
00234         return (
00235                 static_cast<argb_t>(d_alpha * 255) << 24 |
00236                 static_cast<argb_t>(d_red * 255) << 16 |
00237                 static_cast<argb_t>(d_green * 255) << 8 |
00238                 static_cast<argb_t>(d_blue * 255)
00239         );
00240 }
00241 
00242 
00243 void colour::invertColour(void)
00244 {
00245         d_red   = 1.0f - d_red;
00246         d_green = 1.0f - d_green;
00247         d_blue  = 1.0f - d_blue;
00248 }
00249 
00250 
00251 void colour::invertColourWithAlpha(void)
00252 {
00253         d_alpha = 1.0f - d_alpha;
00254         d_red   = 1.0f - d_red;
00255         d_green = 1.0f - d_green;
00256         d_blue  = 1.0f - d_blue;
00257 }
00258 
00259 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:30 2005 for Crazy Eddies GUI System by  doxygen 1.4.3