Color.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Color.cpp 3140 2012-06-18 15:03:02Z susancummins $"
00002 
00004 
00005 //============================================================================
00006 //
00007 //  This file is part of GPSTk, the GPS Toolkit.
00008 //
00009 //  The GPSTk is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU Lesser General Public License as published
00011 //  by the Free Software Foundation; either version 2.1 of the License, or
00012 //  any later version.
00013 //
00014 //  The GPSTk 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
00017 //  GNU 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 GPSTk; if not, write to the Free Software Foundation,
00021 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00022 //  
00023 //  Copyright 2004, The University of Texas at Austin
00024 //
00025 //============================================================================
00026 
00027 #include <sstream>
00028 #include <iomanip>
00029 #include <cctype>
00030 
00031 #include "Color.hpp"
00032 
00033 namespace vdraw
00034 {
00035   static const double COLORMAX = 255.0;
00036 
00037   void Color::getRGBTriplet( short& red, short& green, short& blue ) const
00038   {
00039     using namespace std;
00040 
00041     int temp = rgb;
00042 
00043     blue = temp & 0x0000FF;
00044     temp = (temp >> 8);
00045 
00046     green = temp & 0x0000FF;
00047     temp = (temp >> 8);
00048 
00049     red   = temp & 0x0000FF;
00050   }
00051 
00052   void Color::setRGBTriplet( short red, short green, short blue )
00053   {
00054     int lred   = red & 0xFFl, lgreen = green & 0xFFl, lblue = blue & 0xFFl;
00055     lred = (lred << 16);
00056     lgreen = (lgreen << 8);
00057     rgb = lred | lgreen | lblue;
00058   }
00059 
00060   void Color::getRGBTripletFractional( double& red, double& green,
00061       double& blue ) const
00062   {
00063     short rs, gs, bs;
00064     getRGBTriplet( rs, gs, bs);
00065 
00066     red   = rs/COLORMAX; 
00067     green = gs/COLORMAX;
00068     blue  = bs/COLORMAX;
00069   }
00070 
00071   void Color::setRGBTripletFractional( double red, double green, double blue )
00072   {
00073     short sred   = static_cast<short> (COLORMAX * red);
00074     short sgreen = static_cast<short> (COLORMAX * green);
00075     short sblue  = static_cast<short> (COLORMAX * blue);
00076 
00077     setRGBTriplet( sred, sgreen, sblue);
00078   }
00079 
00080   Color Color::interpolate(double dist, const Color &o) const
00081   {
00082     short r1,g1,b1,r2,g2,b2;
00083     getRGBTriplet(r1,g1,b1);
00084     o.getRGBTriplet(r2,g2,b2);
00085     Color c(
00086         (int)(r1+(r2-r1)*dist),
00087         (int)(g1+(g2-g1)*dist),
00088         (int)(b1+(b2-b1)*dist));
00089     return c;
00090   }
00091 
00092   void Color::setToString(const std::string& str)
00093   {
00094     std::string ls;
00095 
00096     // Eat the white spaces (including those inside)
00097     std::stringstream os(str);
00098     std::string temp;
00099     os >> temp;
00100     ls = temp;
00101     while (os >> temp) 
00102       ls += temp;
00103 
00104     rgb = BLACK;
00105 
00106     // Make sure this runs as expected in the tests..
00107     int i=0;
00108     while (ls[i])
00109     {
00110       ls[i] = tolower(ls[i]);
00111       //c[i]=tolower(s[i]);
00112       i++;
00113     }
00114 
00115     // Unfortunately switch/case only works on integral types. Blech.
00116     if ((ls == "r") || (ls=="red"))         rgb=RED;
00117     else if ((ls == "o") || (ls=="orange")) rgb=ORANGE;
00118     else if ((ls == "y") || (ls=="yellow")) rgb=YELLOW;
00119     else if ((ls == "g") || (ls=="green"))  rgb=GREEN;
00120     else if ((ls == "b") || (ls=="blue"))   rgb=BLUE;
00121     else if ((ls == "v") || (ls=="violet")) rgb=VIOLET;
00122     else if ((ls == "w") || (ls=="white"))  rgb=WHITE;
00123     else if ((ls == "k") || (ls=="brown"))  rgb=BROWN;
00124     else if ((ls == "n") || (ls=="black"))  rgb=BLACK;
00125 
00126     else if ((ls=="grey") || (ls=="gray"))  rgb=GREY;
00127     else if ((ls=="forestgreen") || (ls=="forest")) rgb=FOREST_GREEN;      
00128 
00129     else if (ls=="pink")         rgb=PINK;
00130     else if (ls=="cyan")         rgb=CYAN;
00131     else if (ls=="olive")        rgb=OLIVE;
00132     else if (ls=="khaki")        rgb=KHAKI;
00133     else if (ls=="skyblue")      rgb=SKY_BLUE;
00134     else if (ls=="turquoise")    rgb=TURQUOISE;
00135     else if (ls=="magenta")      rgb=MAGENTA;
00136     else if (ls=="maroon")       rgb=MAROON;
00137     else if (ls=="burntorange")  rgb=BURNT_ORANGE;
00138     else if (ls=="cardinal")     rgb=CARDINAL;
00139     else if (ls=="navy")         rgb=NAVY;
00140     else if (ls=="darkpurple")   rgb=DARK_PURPLE;
00141     else if (ls=="clear")        rgb=CLEAR;
00142 
00143     return;
00144   }
00145 } // namespace vdraw
00146 

Generated on Sun May 19 03:31:05 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1