wg_color.cpp

00001 // wg_color.cpp
00002 //
00003 // CRGBColor class
00004 //
00005 //
00006 // Copyright (c) 2002-2004 Rob Wiskow
00007 // rob-dev@boxedchaos.com
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022 //
00023 
00024 
00025 #include "wgui_include_config.h"
00026 #include "wg_color.h"
00027 #include "wg_error.h"
00028 #include "std_ex.h"
00029 #include <algorithm>
00030 
00031 namespace wGui
00032 {
00033 
00034 CRGBColor::CRGBColor(const Uint32* pColorValue, const SDL_PixelFormat* pFormat)
00035 {
00036      red = stdex::safe_static_cast<unsigned char>((pFormat->Rmask & *pColorValue) >> pFormat->Rshift);
00037      green = stdex::safe_static_cast<unsigned char>((pFormat->Gmask & *pColorValue) >> pFormat->Gshift);
00038      blue = stdex::safe_static_cast<unsigned char>((pFormat->Bmask & *pColorValue) >> pFormat->Bshift);
00039      alpha = stdex::safe_static_cast<unsigned char>((pFormat->Amask & *pColorValue) >> pFormat->Ashift);
00040 }
00041 
00042 CRGBColor::CRGBColor(std::wstring sColor) :
00043      alpha(0xFF)
00044 {
00045      std::transform(sColor.begin(), sColor.end(), sColor.begin(), toupper);
00046 
00047      if (sColor == L"WHITE")
00048      {
00049           red = 0xFF;
00050           green = 0xFF;
00051           blue = 0xFF;
00052      }
00053      else if (sColor == L"LIGHTGRAY")
00054      {
00055           red = 0xC0;
00056           green = 0xC0;
00057           blue = 0xC0;
00058      }
00059      else if (sColor == L"BLACK")
00060      {
00061           red = 0x00;
00062           green = 0x00;
00063           blue = 0x00;
00064      }
00065      else
00066      {
00067           std::vector<std::wstring> ColorStrings = stdex::DetokenizeString(sColor, L",");
00068           if (ColorStrings.size() >= 3)
00069           {
00070                red = stdex::safe_static_cast<unsigned char>(stdex::atoi(ColorStrings[0]));
00071                green = stdex::safe_static_cast<unsigned char>(stdex::atoi(ColorStrings[1]));
00072                blue = stdex::safe_static_cast<unsigned char>(stdex::atoi(ColorStrings[2]));
00073                if (ColorStrings.size() >= 4)
00074                {
00075                     alpha = stdex::safe_static_cast<unsigned char>(stdex::atoi(ColorStrings[3]));
00076                }
00077           }
00078           else
00079           {
00080                throw(Wg_Ex_App(L"CRGBColor::CRGBColor : Unable to instantiate CRGBColor from string."));
00081           }
00082      }
00083 }
00084 
00085 
00086 CRGBColor& CRGBColor::operator=(const CRGBColor& c)
00087 {
00088      red = c.red;
00089      green = c.green;
00090      blue = c.blue;
00091      alpha = c.alpha;
00092      return *this;
00093 }
00094 
00095 
00096 CRGBColor CRGBColor::operator+(const CRGBColor& c) const
00097 {
00098      double c1_ratio = stdex::safe_static_cast<double>(alpha) / 0xFF;
00099      double c2_ratio = stdex::safe_static_cast<double>(c.alpha) / 0xFF;
00100      double new_red = red * c1_ratio + c.red * c2_ratio;
00101      if (new_red > 255)
00102      {
00103           new_red = 255;
00104      }
00105      double new_green = green * c1_ratio + c.green * c2_ratio;
00106      if (new_green > 255)
00107      {
00108           new_green = 255;
00109      }
00110      double new_blue = blue * c1_ratio + c.blue * c2_ratio;
00111      if (new_blue > 255)
00112      {
00113           new_blue = 255;
00114      }
00115      double new_alpha = alpha + c.alpha;
00116      if (new_alpha > 255)
00117      {
00118           new_alpha = 255;
00119      }
00120      return CRGBColor(stdex::safe_static_cast<unsigned char>(new_red), stdex::safe_static_cast<unsigned char>(new_green),
00121           stdex::safe_static_cast<unsigned char>(new_blue), stdex::safe_static_cast<unsigned char>(new_alpha));
00122 }
00123 
00124 
00125 CRGBColor CRGBColor::operator|(const CRGBColor& c) const
00126 {
00127      return CRGBColor(red | c.red, green | c.green, blue | c.blue, alpha | c.alpha);
00128 }
00129 
00130 
00131 CRGBColor CRGBColor::operator&(const CRGBColor& c) const
00132 {
00133      return CRGBColor(red & c.red, green & c.green, blue & c.blue, alpha & c.alpha);
00134 }
00135 
00136 
00137 CRGBColor CRGBColor::operator^(const CRGBColor& c) const
00138 {
00139      return CRGBColor(red ^ c.red, green ^ c.green, blue ^ c.blue, alpha ^ c.alpha);
00140 }
00141 
00142 
00143 CRGBColor CRGBColor::MixNormal(const CRGBColor& c) const
00144 {
00145      double fg_ratio = stdex::safe_static_cast<double>(c.alpha) / 0xFF;
00146      double bg_ratio = stdex::safe_static_cast<double>(1.0 - fg_ratio);
00147      char new_red = stdex::safe_static_cast<unsigned char>(floor(red * bg_ratio + c.red * fg_ratio));
00148      char new_green = stdex::safe_static_cast<unsigned char>(floor(green * bg_ratio + c.green * fg_ratio));
00149      char new_blue = stdex::safe_static_cast<unsigned char>(floor(blue * bg_ratio + c.blue * fg_ratio));
00150      return CRGBColor(new_red, new_green, new_blue, alpha);
00151 }
00152 
00153 
00154 
00155 CRGBColor DEFAULT_BACKGROUND_COLOR = CRGBColor(0x60, 0x60, 0x60);
00156 CRGBColor DEFAULT_FOREGROUND_COLOR = CRGBColor(0xA0, 0xA0, 0xA0);
00157 CRGBColor DEFAULT_LINE_COLOR = CRGBColor(0x00, 0x00, 0x00);
00158 CRGBColor DEFAULT_DISABLED_LINE_COLOR = CRGBColor(0x40, 0x40, 0x40);
00159 CRGBColor COLOR_TRANSPARENT = CRGBColor(0x00, 0x00, 0x00, 0x00);
00160 CRGBColor COLOR_WHITE = CRGBColor(0xFF, 0xFF, 0xFF);
00161 CRGBColor COLOR_LIGHTGRAY = CRGBColor(0xC0, 0xC0, 0xC0);
00162 CRGBColor COLOR_GRAY = CRGBColor(0x80, 0x80, 0x80);
00163 CRGBColor COLOR_DARKGRAY = CRGBColor(0x40, 0x40, 0x40);
00164 CRGBColor COLOR_BLACK = CRGBColor(0x00, 0x00, 0x00);
00165 CRGBColor COLOR_BLUE = CRGBColor(0x00, 0x00, 0xFF);
00166 CRGBColor COLOR_RED = CRGBColor(0xFF, 0x00, 0x00);
00167 CRGBColor COLOR_GREEN = CRGBColor(0x00, 0xFF, 0x00);
00168 CRGBColor COLOR_YELLOW = CRGBColor(0xFF, 0xFF, 0x00);
00169 CRGBColor COLOR_CYAN = CRGBColor(0x00, 0xFF, 0xFF);
00170 CRGBColor COLOR_MAGENTA = CRGBColor(0xFF, 0x00, 0xFF);
00171 
00172 }
00173 

Generated on Wed May 16 23:11:25 2007 for wGui by  doxygen 1.5.1