IMP  2.0.1
The Integrative Modeling Platform
Color.h
Go to the documentation of this file.
1 /**
2  * \file IMP/display/Color.h
3  * \brief Represent a color
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPDISPLAY_COLOR_H
9 #define IMPDISPLAY_COLOR_H
10 
11 #include <IMP/display/display_config.h>
12 #include <IMP/base/Value.h>
15 #include <IMP/base/check_macros.h>
16 #include <IMP/base/value_macros.h>
17 #include <boost/array.hpp>
18 
19 IMPDISPLAY_BEGIN_NAMESPACE
20 
21 //! Represent an RGB color
22 /**
23  */
24 class IMPDISPLAYEXPORT Color: public base::Value
25 {
26  boost::array<double, 3> c_;
27  int compare(double a, double b) const {
28  if (a< b) return -1;
29  else if (a > b) return 1;
30  else return 0;
31  }
32  int compare(const Color &o) const {
33  for (unsigned int i=0; i< 3; ++i) {
34  int c= compare(c_[i], o.c_[i]);
35  if (c != 0) return c;
36  }
37  return 0;
38  }
39 public:
40 
41  Color();
42 
43  /** Components must be between 0 and 1
44  */
45  Color(double r, double g, double b);
46 
47  /** Components must be between 0 and 1
48  */
49  template <class It>
50  Color(It b, It e){
51  std::copy(b,e, c_.begin());
52  }
53 
54  ~Color();
55  /** @name Component access
56  @{
57  */
58  double get_red() const {
59  return c_[0];
60  }
61 
62  double get_green() const {
63  return c_[1];
64  }
65  double get_blue() const {
66  return c_[2];
67  }
68 #ifndef SWIG
69  typedef const double *ComponentIterator;
70  ComponentIterator components_begin() const {
71  return c_.begin();
72  }
73  ComponentIterator components_end() const {
74  return c_.end();
75  }
76 #endif
77  //!@}
78 
79  void show(std::ostream &out, std::string delim) const {
80  out << get_red() << delim << get_green() << delim << get_blue();
81  }
82 
83  IMP_SHOWABLE_INLINE(Color, show(out, " "););
84 
85  IMP_COMPARISONS(Color);
86 };
87 
88 
89 /** Produce a color that is attempted to be contrasting with the
90  i-1 previous colors. Currently, they recycle after 11, but they
91  can be darkened instead. Just ask.
92  */
93 IMPDISPLAYEXPORT Color get_display_color(unsigned int i);
94 
95 #ifndef SWIG
96 #ifndef IMP_DOXYGEN
97 struct SpacesIO
98 {
99  const Color &v_;
100  explicit SpacesIO(const Color &v): v_(v){}
101 };
102 
103 struct CommasIO
104 {
105  const Color &v_;
106  explicit CommasIO(const Color &v): v_(v){}
107 };
108 inline std::ostream &operator<<(std::ostream &out, const SpacesIO &s)
109 {
110  s.v_.show(out, " ");
111  return out;
112 }
113 inline std::ostream &operator<<(std::ostream &out, const CommasIO &s)
114 {
115  s.v_.show(out, ", ");
116  return out;
117 }
118 
119 //! Use this before outputing to delimited vector entries with a space
120 /** std::cout << spaces_io(v);
121  produces "1.0 2.0 3.0"
122  \relatesalso Color
123  */
124 inline SpacesIO spaces_io(const Color &v) {
125  return SpacesIO(v);
126 }
127 
128 
129 
130 
131 //! Use this before outputing to delimited vector entries with a comma
132 /** std::cout << commas_io(v);
133  produces "1.0, 2.0, 3.0"
134  \relatesalso Color
135  */
136 inline CommasIO commas_io(const Color &v) {
137  return CommasIO(v);
138 }
139 
140 //! Multiply a color by a value less than 1
141 /** \unstable{Color multiplication}
142  */
143 inline Color operator*(double d, Color c) {
144  IMP_USAGE_CHECK(d <=1 && d>=0, "Colors can only fade with multiplication");
145  return Color(c.get_red()*d, c.get_green()*d, c.get_blue()*d);
146 }
147 
148 //! Multiply a color by a value less than 1
149 /** \unstable{Color multiplication}
150  */
151 inline Color operator*(Color c, double d) {
152  return d*c;
153 }
154 #endif
155 
156 #endif // SWIG
157 
158 //! Return a color interpolated between a and b in RGB space
159 /** If f is 0, then a is returned, if f is 1, b is returned.
160  \relatesalso Color
161  */
163  const Color &b,
164  double f) {
165  return Color((1-f)*a.get_red()+f*b.get_red(),
166  (1-f)*a.get_green()+f*b.get_green(),
167  (1-f)*a.get_blue()+f*b.get_blue());
168 }
169 
170 /** Return a number suitable for being passed to a color
171  map that is gotten by linearly scaling between the
172  passed min and max (and truncating values that are out
173  of range).
174 */
175 IMPDISPLAYEXPORT double get_linear_color_map_value(double min,
176  double max,
177  double value);
178 
179 /** \name Colormaps
180  These functions map a number in the interval [0,1]
181  to a color using some color map.
182 @{
183  */
184 //! Return the color for f from the jet color map
185 /** \relatesalso Color
186  */
187 IMPDISPLAYEXPORT Color get_jet_color(double f);
188 
189 //! Return the color for f from the hot color map
190 /** \relatesalso Color
191  */
192 IMPDISPLAYEXPORT Color get_hot_color(double f);
193 
194 //! Return the color for f from the rgb color map
195 /** \relatesalso Color
196  */
197 IMPDISPLAYEXPORT Color get_rgb_color(double f);
198 
199 
200 //! Return the a greyscale value for f
201 /** \relatesalso Color
202  */
203 IMPDISPLAYEXPORT Color get_grey_color(double f);
204 
205 //! Return colors using the gnuplot default color map
206 /** \relatesalso Color
207  */
208 IMPDISPLAYEXPORT Color get_gnuplot_color(double f);
209 /** @} */
210 
212 
213 IMPDISPLAY_END_NAMESPACE
214 
215 #endif /* IMPDISPLAY_COLOR_H */