alpar@1971: /* -*- C++ -*- alpar@1971: * alpar@1971: * This file is a part of LEMON, a generic C++ optimization library alpar@1971: * alpar@2553: * Copyright (C) 2003-2008 alpar@1971: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1971: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@1971: * alpar@1971: * Permission to use, modify and distribute this software is granted alpar@1971: * provided that this copyright notice appears in all copies. For alpar@1971: * precise terms see the accompanying LICENSE file. alpar@1971: * alpar@1971: * This software is provided "AS IS" with no warranty of any kind, alpar@1971: * express or implied, and with no claim as to its suitability for any alpar@1971: * purpose. alpar@1971: * alpar@1971: */ alpar@1971: alpar@1971: #ifndef LEMON_COLOR_H alpar@1971: #define LEMON_COLOR_H alpar@1971: alpar@1971: #include alpar@2569: #include alpar@1971: #include alpar@1971: alpar@1971: alpar@1971: ///\ingroup misc alpar@1971: ///\file alpar@1971: ///\brief Tools to manage RGB colors. alpar@1971: /// alpar@1971: ///\author Alpar Juttner alpar@1971: alpar@1971: namespace lemon { alpar@1971: deba@2230: deba@2398: /// \addtogroup misc deba@2398: /// @{ alpar@2174: deba@2398: ///Data structure representing RGB colors. alpar@1971: deba@2398: ///Data structure representing RGB colors. deba@2398: class Color deba@2398: { deba@2398: double _r,_g,_b; deba@2398: public: deba@2398: ///Default constructor deba@2398: Color() {} deba@2398: ///Constructor deba@2398: Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; deba@2398: ///Set the red component deba@2398: double & red() {return _r;} deba@2398: ///Return the red component deba@2398: const double & red() const {return _r;} deba@2398: ///Set the green component deba@2398: double & green() {return _g;} deba@2398: ///Return the green component deba@2398: const double & green() const {return _g;} deba@2398: ///Set the blue component deba@2398: double & blue() {return _b;} deba@2398: ///Return the blue component deba@2398: const double & blue() const {return _b;} deba@2398: ///Set the color components deba@2398: void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; deba@2398: }; alpar@1971: alpar@2174: /// White color constant alpar@2174: extern const Color WHITE; alpar@2174: /// Black color constant alpar@2174: extern const Color BLACK; alpar@2174: /// Red color constant alpar@2174: extern const Color RED; alpar@2174: /// Green color constant alpar@2174: extern const Color GREEN; alpar@2174: /// Blue color constant alpar@2174: extern const Color BLUE; alpar@2174: /// Yellow color constant alpar@2174: extern const Color YELLOW; alpar@2174: /// Magenta color constant alpar@2174: extern const Color MAGENTA; alpar@2174: /// Cyan color constant alpar@2174: extern const Color CYAN; alpar@2174: /// Grey color constant alpar@2174: extern const Color GREY; alpar@2174: /// Dark red color constant alpar@2174: extern const Color DARK_RED; alpar@2174: /// Dark green color constant alpar@2174: extern const Color DARK_GREEN; alpar@2174: /// Drak blue color constant alpar@2174: extern const Color DARK_BLUE; alpar@2174: /// Dark yellow color constant alpar@2174: extern const Color DARK_YELLOW; alpar@2174: /// Dark magenta color constant alpar@2174: extern const Color DARK_MAGENTA; alpar@2174: /// Dark cyan color constant alpar@2174: extern const Color DARK_CYAN; alpar@2174: deba@2398: ///Map ints to different \ref Color "Color"s alpar@1971: deba@2398: ///This map assigns one of the predefined \ref Color "Color"s to deba@2398: ///each int. It is possible to change the colors as well as deba@2398: ///their number. The integer range is cyclically mapped to the deba@2398: ///provided set of colors. deba@2398: /// deba@2398: ///This is a true \ref concepts::ReferenceMap "reference map", so deba@2398: ///you can also change the actual colors. alpar@1971: deba@2398: class Palette : public MapBase deba@2398: { deba@2398: std::vector colors; deba@2398: public: deba@2398: ///Constructor alpar@1971: deba@2398: ///Constructor deba@2398: ///\param num the number of the allocated colors. If it is \c -1, deba@2398: ///the default color configuration is set up (26 color plus the deba@2398: ///white). If \c num is less then 26/27 then the default color deba@2398: ///list is cut. Otherwise the color list is filled repeatedly with deba@2398: ///the default color list. (The colors can be changed later on.) deba@2398: ///\param have_white indicates whether white is amongst the deba@2398: ///provided color (\c true) or not (\c false). If it is true, deba@2398: ///white will be assigned to \c 0. deba@2398: Palette(int num=-1,bool have_white=false) deba@2398: { deba@2398: if (num==0) return; deba@2398: do { deba@2398: if(have_white) colors.push_back(Color(1,1,1)); deba@2398: deba@2398: colors.push_back(Color(0,0,0)); deba@2398: colors.push_back(Color(1,0,0)); deba@2398: colors.push_back(Color(0,1,0)); deba@2398: colors.push_back(Color(0,0,1)); deba@2398: colors.push_back(Color(1,1,0)); deba@2398: colors.push_back(Color(1,0,1)); deba@2398: colors.push_back(Color(0,1,1)); deba@2398: deba@2398: colors.push_back(Color(.5,0,0)); deba@2398: colors.push_back(Color(0,.5,0)); deba@2398: colors.push_back(Color(0,0,.5)); deba@2398: colors.push_back(Color(.5,.5,0)); deba@2398: colors.push_back(Color(.5,0,.5)); deba@2398: colors.push_back(Color(0,.5,.5)); deba@2398: deba@2398: colors.push_back(Color(.5,.5,.5)); deba@2398: colors.push_back(Color(1,.5,.5)); deba@2398: colors.push_back(Color(.5,1,.5)); deba@2398: colors.push_back(Color(.5,.5,1)); deba@2398: colors.push_back(Color(1,1,.5)); deba@2398: colors.push_back(Color(1,.5,1)); deba@2398: colors.push_back(Color(.5,1,1)); deba@2398: deba@2398: colors.push_back(Color(1,.5,0)); deba@2398: colors.push_back(Color(.5,1,0)); deba@2398: colors.push_back(Color(1,0,.5)); deba@2398: colors.push_back(Color(0,1,.5)); deba@2398: colors.push_back(Color(0,.5,1)); deba@2398: colors.push_back(Color(.5,0,1)); deba@2398: } while(int(colors.size())=0) colors.resize(num); deba@2398: } deba@2398: ///\e deba@2398: Color &operator[](int i) deba@2398: { deba@2398: return colors[i%colors.size()]; deba@2398: } deba@2398: ///\e deba@2398: const Color &operator[](int i) const deba@2398: { deba@2398: return colors[i%colors.size()]; deba@2398: } deba@2398: ///\e deba@2398: void set(int i,const Color &c) deba@2398: { deba@2398: colors[i%colors.size()]=c; deba@2398: } deba@2398: ///\e deba@2398: void add(const Color &c) deba@2398: { deba@2398: colors.push_back(c); deba@2398: } deba@2398: deba@2398: ///Sets the number of the exiting colors. deba@2398: void resize(int s) { colors.resize(s);} deba@2398: ///Returns the number of the existing colors. deba@2398: int size() const { return int(colors.size());} deba@2398: }; deba@2398: deba@2398: ///Returns a visible distinct \ref Color deba@2398: deba@2398: ///Returns a \ref Color which is as different from the given parameter deba@2398: ///as it is possible. deba@2398: inline Color distantColor(const Color &c) alpar@1971: { deba@2398: return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0); deba@2398: } deba@2398: ///Returns black for light colors and white for the dark ones. alpar@1971: deba@2398: ///Returns black for light colors and white for the dark ones. deba@2398: inline Color distantBW(const Color &c){ deba@2398: return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; alpar@1971: } alpar@1971: deba@2398: /// @} alpar@2174: alpar@1971: } //END OF NAMESPACE LEMON alpar@1971: alpar@1971: #endif // LEMON_COLOR_H