3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
23 #include<lemon/math.h>
24 #include<lemon/maps.h>
29 ///\brief Tools to manage RGB colors.
31 ///\author Alpar Juttner
39 ///Data structure representing RGB colors.
41 ///Data structure representing RGB colors.
46 ///Default constructor
49 Color(double r,double g,double b) :_r(r),_g(g),_b(b) {};
50 ///Set the red component
51 double & red() {return _r;}
52 ///Return the red component
53 const double & red() const {return _r;}
54 ///Set the green component
55 double & green() {return _g;}
56 ///Return the green component
57 const double & green() const {return _g;}
58 ///Set the blue component
59 double & blue() {return _b;}
60 ///Return the blue component
61 const double & blue() const {return _b;}
62 ///Set the color components
63 void set(double r,double g,double b) { _r=r;_g=g;_b=b; };
66 /// White color constant
67 extern const Color WHITE;
68 /// Black color constant
69 extern const Color BLACK;
70 /// Red color constant
71 extern const Color RED;
72 /// Green color constant
73 extern const Color GREEN;
74 /// Blue color constant
75 extern const Color BLUE;
76 /// Yellow color constant
77 extern const Color YELLOW;
78 /// Magenta color constant
79 extern const Color MAGENTA;
80 /// Cyan color constant
81 extern const Color CYAN;
82 /// Grey color constant
83 extern const Color GREY;
84 /// Dark red color constant
85 extern const Color DARK_RED;
86 /// Dark green color constant
87 extern const Color DARK_GREEN;
88 /// Drak blue color constant
89 extern const Color DARK_BLUE;
90 /// Dark yellow color constant
91 extern const Color DARK_YELLOW;
92 /// Dark magenta color constant
93 extern const Color DARK_MAGENTA;
94 /// Dark cyan color constant
95 extern const Color DARK_CYAN;
97 ///Map <tt>int</tt>s to different \ref Color "Color"s
99 ///This map assigns one of the predefined \ref Color "Color"s to
100 ///each <tt>int</tt>. It is possible to change the colors as well as
101 ///their number. The integer range is cyclically mapped to the
102 ///provided set of colors.
104 ///This is a true \ref concepts::ReferenceMap "reference map", so
105 ///you can also change the actual colors.
107 class Palette : public MapBase<int,Color>
109 std::vector<Color> colors;
114 ///\param num the number of the allocated colors. If it is \c -1,
115 ///the default color configuration is set up (26 color plus the
116 ///white). If \c num is less then 26/27 then the default color
117 ///list is cut. Otherwise the color list is filled repeatedly with
118 ///the default color list. (The colors can be changed later on.)
119 ///\param have_white indicates whether white is amongst the
120 ///provided color (\c true) or not (\c false). If it is true,
121 ///white will be assigned to \c 0.
122 Palette(bool have_white=false,int num=-1)
126 if(have_white) colors.push_back(Color(1,1,1));
128 colors.push_back(Color(0,0,0));
129 colors.push_back(Color(1,0,0));
130 colors.push_back(Color(0,1,0));
131 colors.push_back(Color(0,0,1));
132 colors.push_back(Color(1,1,0));
133 colors.push_back(Color(1,0,1));
134 colors.push_back(Color(0,1,1));
136 colors.push_back(Color(.5,0,0));
137 colors.push_back(Color(0,.5,0));
138 colors.push_back(Color(0,0,.5));
139 colors.push_back(Color(.5,.5,0));
140 colors.push_back(Color(.5,0,.5));
141 colors.push_back(Color(0,.5,.5));
143 colors.push_back(Color(.5,.5,.5));
144 colors.push_back(Color(1,.5,.5));
145 colors.push_back(Color(.5,1,.5));
146 colors.push_back(Color(.5,.5,1));
147 colors.push_back(Color(1,1,.5));
148 colors.push_back(Color(1,.5,1));
149 colors.push_back(Color(.5,1,1));
151 colors.push_back(Color(1,.5,0));
152 colors.push_back(Color(.5,1,0));
153 colors.push_back(Color(1,0,.5));
154 colors.push_back(Color(0,1,.5));
155 colors.push_back(Color(0,.5,1));
156 colors.push_back(Color(.5,0,1));
157 } while(int(colors.size())<num);
158 // colors.push_back(Color(1,1,1));
159 if(num>=0) colors.resize(num);
162 Color &operator[](int i)
164 return colors[i%colors.size()];
167 const Color &operator[](int i) const
169 return colors[i%colors.size()];
172 void set(int i,const Color &c)
174 colors[i%colors.size()]=c;
177 void add(const Color &c)
182 ///Sets the number of the exiting colors.
183 void resize(int s) { colors.resize(s);}
184 ///Returns the number of the existing colors.
185 int size() const { return int(colors.size());}
188 ///Returns a visible distinct \ref Color
190 ///Returns a \ref Color which is as different from the given parameter
191 ///as it is possible.
192 inline Color distantColor(const Color &c)
194 return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0);
196 ///Returns black for light colors and white for the dark ones.
198 ///Returns black for light colors and white for the dark ones.
199 inline Color distantBW(const Color &c){
200 return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK;
205 } //END OF NAMESPACE LEMON
207 #endif // LEMON_COLOR_H