alpar@1971: /* -*- C++ -*- alpar@1971: * alpar@1971: * This file is a part of LEMON, a generic C++ optimization library alpar@1971: * alpar@2391: * Copyright (C) 2003-2007 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@2179: #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@2230: /// \addtogroup misc deba@2230: /// @{ alpar@2174: alpar@1971: ///Data structure representing RGB colors. alpar@1971: alpar@1971: ///Data structure representing RGB colors. alpar@1971: class Color alpar@1971: { alpar@1971: double _r,_g,_b; alpar@1971: public: alpar@1971: ///Default constructor alpar@1971: Color() {} alpar@1971: ///Constructor alpar@1971: Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; alpar@2159: ///Set the red component alpar@1971: double & red() {return _r;} alpar@2159: ///Return the red component alpar@1971: const double & red() const {return _r;} alpar@2159: ///Set the green component alpar@1971: double & green() {return _g;} alpar@2159: ///Return the green component alpar@1971: const double & green() const {return _g;} alpar@2159: ///Set the blue component alpar@1971: double & blue() {return _b;} alpar@2159: ///Return the blue component alpar@1971: const double & blue() const {return _b;} alpar@1971: ///Set the color components alpar@1971: void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; alpar@1971: }; 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: alpar@2179: ///Map ints to different \ref Color "Color"s alpar@1971: alpar@1971: ///This map assigns one of the predefined \ref Color "Color"s alpar@1971: ///to each int. It is possible to change the colors as well as their alpar@1971: ///number. The integer range is cyclically mapped to the provided set of colors. alpar@1971: /// alpar@2260: ///This is a true \ref concepts::ReferenceMap "reference map", so you can also alpar@1971: ///change the actual colors. alpar@1971: alpar@2172: class Palette : public MapBase alpar@1971: { alpar@1971: std::vector colors; alpar@1971: public: alpar@1971: ///Constructor alpar@1971: alpar@1971: ///Constructor alpar@1971: ///\param have_white indicates whether white is alpar@1971: ///amongst the provided color (\c true) or not (\c false). If it is true, alpar@1971: ///white will be assigned to \c 0. alpar@2174: ///\param num the number of the allocated colors. If it is \c 0, alpar@2174: ///the default color configuration is set up (26 color plus the white). alpar@1971: ///If \c num is less then 26/27 then the default color list is cut. Otherwise alpar@1971: ///the color list is filled repeatedly with the default color list. alpar@1971: ///(The colors can be changed later on.) alpar@2172: Palette(bool have_white=false,int num=0) alpar@1971: { alpar@1971: do { alpar@1971: if(have_white) colors.push_back(Color(1,1,1)); alpar@1971: alpar@1971: colors.push_back(Color(0,0,0)); alpar@1971: colors.push_back(Color(1,0,0)); alpar@1971: colors.push_back(Color(0,1,0)); alpar@1971: colors.push_back(Color(0,0,1)); alpar@1971: colors.push_back(Color(1,1,0)); alpar@1971: colors.push_back(Color(1,0,1)); alpar@1971: colors.push_back(Color(0,1,1)); alpar@1971: alpar@1971: colors.push_back(Color(.5,0,0)); alpar@1971: colors.push_back(Color(0,.5,0)); alpar@1971: colors.push_back(Color(0,0,.5)); alpar@1971: colors.push_back(Color(.5,.5,0)); alpar@1971: colors.push_back(Color(.5,0,.5)); alpar@1971: colors.push_back(Color(0,.5,.5)); alpar@1971: alpar@1971: colors.push_back(Color(.5,.5,.5)); alpar@1971: colors.push_back(Color(1,.5,.5)); alpar@1971: colors.push_back(Color(.5,1,.5)); alpar@1971: colors.push_back(Color(.5,.5,1)); alpar@1971: colors.push_back(Color(1,1,.5)); alpar@1971: colors.push_back(Color(1,.5,1)); alpar@1971: colors.push_back(Color(.5,1,1)); alpar@1971: alpar@1971: colors.push_back(Color(1,.5,0)); alpar@1971: colors.push_back(Color(.5,1,0)); alpar@1971: colors.push_back(Color(1,0,.5)); alpar@1971: colors.push_back(Color(0,1,.5)); alpar@1971: colors.push_back(Color(0,.5,1)); alpar@1971: colors.push_back(Color(.5,0,1)); alpar@1971: } while(int(colors.size())0) colors.resize(num); alpar@1971: } alpar@1971: ///\e alpar@1971: Color &operator[](int i) alpar@1971: { alpar@1971: return colors[i%colors.size()]; alpar@1971: } alpar@1971: ///\e alpar@1971: const Color &operator[](int i) const alpar@1971: { alpar@1971: return colors[i%colors.size()]; alpar@1971: } alpar@1971: ///\e alpar@1971: void set(int i,const Color &c) alpar@1971: { alpar@1971: colors[i%colors.size()]=c; alpar@1971: } alpar@1971: ///Sets the number of the exiting colors. alpar@1971: void resize(int s) { colors.resize(s);} alpar@1971: ///Returns the number of the existing colors. alpar@1971: std::size_t size() const { return colors.size();} alpar@1971: }; alpar@1971: alpar@1971: ///Returns a visible distinct \ref Color alpar@1971: alpar@1971: ///Returns a \ref Color which is as different from the given parameter alpar@1971: ///as it is possible. alpar@1971: inline Color distantColor(const Color &c) alpar@1971: { alpar@1971: return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0); alpar@1971: } alpar@1971: ///Returns black for light colors and white for the dark ones. alpar@1971: alpar@1971: ///Returns black for light colors and white for the dark ones. alpar@1971: inline Color distantBW(const Color &c){ alpar@2174: return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; alpar@1971: } alpar@1971: alpar@2174: /// @} alpar@2174: alpar@1971: } //END OF NAMESPACE LEMON alpar@1971: alpar@1971: #endif // LEMON_COLOR_H