GUIColorScheme.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 //
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef GUIColorScheme_h
00020 #define GUIColorScheme_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <vector>
00033 #include <utils/iodevices/OutputDevice.h>
00034 
00035 
00036 // ===========================================================================
00037 // class definitions
00038 // ===========================================================================
00043 class GUIColorScheme {
00044 public:
00046     GUIColorScheme(const std::string& name, const RGBColor& baseColor,
00047                    const std::string& colName="", const bool isFixed=false)
00048             : myName(name), myIsInterpolated(!isFixed), myIsFixed(isFixed) {
00049         addColor(baseColor, 0, colName);
00050     }
00051 
00052     void setThreshold(const size_t pos, const SUMOReal threshold) {
00053         myThresholds[pos] = threshold;
00054     }
00055 
00056     void setColor(const size_t pos, const RGBColor& color) {
00057         myColors[pos] = color;
00058     }
00059 
00060     bool setColor(const std::string& name, const RGBColor& color) {
00061         std::vector<std::string>::iterator nameIt = myNames.begin();
00062         std::vector<RGBColor>::iterator colIt = myColors.begin();
00063         for (; nameIt != myNames.end(); ++nameIt, ++colIt) {
00064             if (*nameIt == name) {
00065                 (*colIt) = color;
00066                 return true;
00067             }
00068         }
00069         return false;
00070     }
00071 
00072     unsigned int addColor(const RGBColor& color, const SUMOReal threshold, const std::string& name="") {
00073         std::vector<RGBColor>::iterator colIt = myColors.begin();
00074         std::vector<SUMOReal>::iterator threshIt = myThresholds.begin();
00075         std::vector<std::string>::iterator nameIt = myNames.begin();
00076         unsigned int pos = 0;
00077         while (threshIt != myThresholds.end() && (*threshIt) < threshold) {
00078             ++threshIt;
00079             ++colIt;
00080             ++nameIt;
00081             pos++;
00082         }
00083         myColors.insert(colIt, color);
00084         myThresholds.insert(threshIt, threshold);
00085         myNames.insert(nameIt, name);
00086         return pos;
00087     }
00088 
00089     void removeColor(const size_t pos) {
00090         assert(pos < myColors.size());
00091         myColors.erase(myColors.begin()+pos);
00092         myThresholds.erase(myThresholds.begin()+pos);
00093         myNames.erase(myNames.begin()+pos);
00094     }
00095 
00096     void clear() {
00097         myColors.clear();
00098         myThresholds.clear();
00099         myNames.clear();
00100     }
00101 
00102     const RGBColor getColor(const SUMOReal value) const {
00103         if (myColors.size() == 1 || value < myThresholds.front()) {
00104             return myColors.front();
00105         }
00106         std::vector<RGBColor>::const_iterator colIt = myColors.begin()+1;
00107         std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin()+1;
00108         while (threshIt != myThresholds.end() && (*threshIt) <= value) {
00109             ++threshIt;
00110             ++colIt;
00111         }
00112         if (threshIt == myThresholds.end()) {
00113             return myColors.back();
00114         }
00115         if (!myIsInterpolated) {
00116             return *(colIt-1);
00117         }
00118         SUMOReal lowVal = *(threshIt-1);
00119         return RGBColor::interpolate(*(colIt-1), *colIt, (value-lowVal)/((*threshIt)-lowVal));
00120     }
00121 
00122     void setInterpolated(const bool interpolate, SUMOReal interpolationStart=0.f) {
00123         myIsInterpolated = interpolate;
00124         if (interpolate) {
00125             myThresholds[0] = interpolationStart;
00126         }
00127     }
00128 
00129     const std::string &getName() const {
00130         return myName;
00131     }
00132 
00133     const std::vector<RGBColor> &getColors() const {
00134         return myColors;
00135     }
00136 
00137     const std::vector<SUMOReal> &getThresholds() const {
00138         return myThresholds;
00139     }
00140 
00141     const bool isInterpolated() const {
00142         return myIsInterpolated;
00143     }
00144 
00145     const std::vector<std::string> &getNames() const {
00146         return myNames;
00147     }
00148 
00149     const bool isFixed() const {
00150         return myIsFixed;
00151     }
00152 
00153     void save(OutputDevice &dev) const {
00154         dev << "            <colorScheme name=\"" << myName;
00155         if (!myIsFixed) {
00156             dev << "\" interpolated=\"" << myIsInterpolated;
00157         }
00158         dev << "\">\n";
00159         std::vector<RGBColor>::const_iterator colIt = myColors.begin();
00160         std::vector<SUMOReal>::const_iterator threshIt = myThresholds.begin();
00161         std::vector<std::string>::const_iterator nameIt = myNames.begin();
00162         while (threshIt != myThresholds.end()) {
00163             dev << "                <entry color=\"" << (*colIt);
00164             if (!myIsFixed) {
00165                 dev << "\" threshold=\"" << (*threshIt);
00166             }
00167             if ((*nameIt) != "") {
00168                 dev << "\" name=\"" << (*nameIt);
00169             }
00170             dev << "\"/>\n";
00171             ++threshIt;
00172             ++colIt;
00173             ++nameIt;
00174         }
00175         dev << "            </colorScheme>\n";
00176     }
00177 
00178     bool operator==(const GUIColorScheme &c) const {
00179         return myName == c.myName && myColors == c.myColors && myThresholds == c.myThresholds && myIsInterpolated == c.myIsInterpolated;
00180     }
00181 
00182 private:
00183     std::string myName;
00184     std::vector<RGBColor> myColors;
00185     std::vector<SUMOReal> myThresholds;
00186     bool myIsInterpolated;
00187     std::vector<std::string> myNames;
00188     bool myIsFixed;
00189 
00190 };
00191 
00192 #endif
00193 
00194 /****************************************************************************/

Generated on Wed May 5 00:06:29 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6