IMP  2.1.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  boost::array<double, 3> c_;
26  int compare(double a, double b) const {
27  if (a < b)
28  return -1;
29  else if (a > b)
30  return 1;
31  else
32  return 0;
33  }
34  int compare(const Color &o) const {
35  for (unsigned int i = 0; i < 3; ++i) {
36  int c = compare(c_[i], o.c_[i]);
37  if (c != 0) return c;
38  }
39  return 0;
40  }
41 
42  public:
43  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Color);
44 
45  Color();
46 
47  /** Components must be between 0 and 1
48  */
49  Color(double r, double g, double b);
50 
51  /** Components must be between 0 and 1
52  */
53  template <class It>
54  Color(It b, It e) {
55  std::copy(b, e, c_.begin());
56  }
57 
58  ~Color();
59  /** @name Component access
60  @{
61  */
62  double get_red() const { return c_[0]; }
63 
64  double get_green() const { return c_[1]; }
65  double get_blue() const { return c_[2]; }
66 #ifndef SWIG
67  typedef const double *ComponentIterator;
68  ComponentIterator components_begin() const { return c_.begin(); }
69  ComponentIterator components_end() const { return c_.end(); }
70 #endif
71  //!@}
72 
73  void show(std::ostream &out, std::string delim) const {
74  out << get_red() << delim << get_green() << delim << get_blue();
75  }
76 
77  IMP_SHOWABLE_INLINE(Color, show(out, " "););
78 
79  IMP_COMPARISONS(Color);
80 };
81 
82 /** Produce a color that is attempted to be contrasting with the
83  i-1 previous colors. Currently, they recycle after 11, but they
84  can be darkened instead. Just ask.
85  */
86 IMPDISPLAYEXPORT Color get_display_color(unsigned int i);
87 
88 #ifndef SWIG
89 #ifndef IMP_DOXYGEN
90 struct SpacesIO {
91  const Color &v_;
92  explicit SpacesIO(const Color &v) : v_(v) {}
93 };
94 
95 struct CommasIO {
96  const Color &v_;
97  explicit CommasIO(const Color &v) : v_(v) {}
98 };
99 inline std::ostream &operator<<(std::ostream &out, const SpacesIO &s) {
100  s.v_.show(out, " ");
101  return out;
102 }
103 inline std::ostream &operator<<(std::ostream &out, const CommasIO &s) {
104  s.v_.show(out, ", ");
105  return out;
106 }
107 
108 //! Use this before outputing to delimited vector entries with a space
109 /** std::cout << spaces_io(v);
110  produces "1.0 2.0 3.0"
111  See Color
112  */
113 inline SpacesIO spaces_io(const Color &v) { return SpacesIO(v); }
114 
115 //! Use this before outputing to delimited vector entries with a comma
116 /** std::cout << commas_io(v);
117  produces "1.0, 2.0, 3.0"
118  See Color
119  */
120 inline CommasIO commas_io(const Color &v) { return CommasIO(v); }
121 
122 //! Multiply a color by a value less than 1
123 /** \unstable{Color multiplication}
124  */
125 inline Color operator*(double d, Color c) {
126  IMP_USAGE_CHECK(d <= 1 && d >= 0, "Colors can only fade with multiplication");
127  return Color(c.get_red() * d, c.get_green() * d, c.get_blue() * d);
128 }
129 
130 //! Multiply a color by a value less than 1
131 /** \unstable{Color multiplication}
132  */
133 inline Color operator*(Color c, double d) { return d * c; }
134 #endif
135 
136 #endif // SWIG
137 
138 //! Return a color interpolated between a and b in RGB space
139 /** If f is 0, then a is returned, if f is 1, b is returned.
140  See Color
141  */
142 inline Color get_interpolated_rgb(const Color &a, const Color &b, double f) {
143  return Color((1 - f) * a.get_red() + f * b.get_red(),
144  (1 - f) * a.get_green() + f * b.get_green(),
145  (1 - f) * a.get_blue() + f * b.get_blue());
146 }
147 
148 /** Return a number suitable for being passed to a color
149  map that is gotten by linearly scaling between the
150  passed min and max (and truncating values that are out
151  of range).
152 */
153 IMPDISPLAYEXPORT double get_linear_color_map_value(double min, double max,
154  double value);
155 
156 /** \name Colormaps
157  These functions map a number in the interval [0,1]
158  to a color using some color map.
159 @{
160  */
161 //! Return the color for f from the jet color map
162 /** See Color
163  */
164 IMPDISPLAYEXPORT Color get_jet_color(double f);
165 
166 //! Return the color for f from the hot color map
167 /** See Color
168  */
169 IMPDISPLAYEXPORT Color get_hot_color(double f);
170 
171 //! Return the color for f from the rgb color map
172 /** See Color
173  */
174 IMPDISPLAYEXPORT Color get_rgb_color(double f);
175 
176 //! Return the a greyscale value for f
177 /** See Color
178  */
179 IMPDISPLAYEXPORT Color get_grey_color(double f);
180 
181 //! Return colors using the gnuplot default color map
182 /** See Color
183  */
184 IMPDISPLAYEXPORT Color get_gnuplot_color(double f);
185 /** @} */
186 
188 
189 IMPDISPLAY_END_NAMESPACE
190 
191 #endif /* IMPDISPLAY_COLOR_H */
Represent an RGB color.
Definition: Color.h:24
Various general useful macros for IMP.
Color get_rgb_color(double f)
Return the color for f from the rgb color map.
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
double get_linear_color_map_value(double min, double max, double value)
Color get_interpolated_rgb(const Color &a, const Color &b, double f)
Return a color interpolated between a and b in RGB space.
Definition: Color.h:142
Color get_hot_color(double f)
Return the color for f from the hot color map.
Color(It b, It e)
Definition: Color.h:54
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Basic types used by IMP.
#define IMP_COMPARISONS(Name)
Implement comparison in a class using a compare function.
VectorD< D > operator*(double s, const VectorD< D > &o)
Definition: VectorD.h:385
Color get_grey_color(double f)
Return the a greyscale value for f.
int compare(const VectorD< D > &a, const VectorD< D > &b)
lexicographic comparison of two vectors
Definition: VectorD.h:371
Color get_display_color(unsigned int i)
Color get_gnuplot_color(double f)
Return colors using the gnuplot default color map.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
Various general useful macros for IMP.
Various general useful macros for IMP.
Color get_jet_color(double f)
Return the color for f from the jet color map.
Exception definitions and assertions.