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