IMP logo
IMP Reference Guide  2.6.0
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-2016 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/Value.h>
13 #include <IMP/showable_macros.h>
14 #include <IMP/comparison_macros.h>
15 #include <IMP/check_macros.h>
16 #include <IMP/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 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  const boost::array<double, 3> &get_rgb() const { return c_; }
71 #endif
72  //!@}
73 
74  void show(std::ostream &out, std::string delim) const {
75  out << get_red() << delim << get_green() << delim << get_blue();
76  }
77 
78  IMP_SHOWABLE_INLINE(Color, show(out, " "););
79 
80  IMP_COMPARISONS(Color);
81 };
82 
83 /** Produce a color that is attempted to be contrasting with the
84  i-1 previous colors. Currently, they recycle after 11, but they
85  can be darkened instead. Just ask.
86  */
87 IMPDISPLAYEXPORT Color get_display_color(unsigned int i);
88 
89 #ifndef SWIG
90 #ifndef IMP_DOXYGEN
91 struct SpacesIO {
92  const Color &v_;
93  explicit SpacesIO(const Color &v) : v_(v) {}
94 };
95 
96 struct CommasIO {
97  const Color &v_;
98  explicit CommasIO(const Color &v) : v_(v) {}
99 };
100 inline std::ostream &operator<<(std::ostream &out, const SpacesIO &s) {
101  s.v_.show(out, " ");
102  return out;
103 }
104 inline std::ostream &operator<<(std::ostream &out, const CommasIO &s) {
105  s.v_.show(out, ", ");
106  return out;
107 }
108 
109 //! Use this before outputting to delimited vector entries with a space
110 /** std::cout << spaces_io(v);
111  produces "1.0 2.0 3.0"
112  \see Color
113  */
114 inline SpacesIO spaces_io(const Color &v) { return SpacesIO(v); }
115 
116 //! Use this before outputting to delimited vector entries with a comma
117 /** std::cout << commas_io(v);
118  produces "1.0, 2.0, 3.0"
119  \see Color
120  */
121 inline CommasIO commas_io(const Color &v) { return CommasIO(v); }
122 
123 //! Multiply a color by a value less than 1
124 /** \unstable{Color multiplication}
125  */
126 inline Color operator*(double d, Color c) {
127  IMP_USAGE_CHECK(d <= 1 && d >= 0, "Colors can only fade with multiplication");
128  return Color(c.get_red() * d, c.get_green() * d, c.get_blue() * d);
129 }
130 
131 //! Multiply a color by a value less than 1
132 /** \unstable{Color multiplication}
133  */
134 inline Color operator*(Color c, double d) { return d * c; }
135 #endif
136 
137 #endif // SWIG
138 
139 //! Return a color interpolated between a and b in RGB space
140 /** If f is 0, then a is returned, if f is 1, b is returned.
141  \see Color
142  */
143 inline Color get_interpolated_rgb(const Color &a, const Color &b, double f) {
144  return Color((1 - f) * a.get_red() + f * b.get_red(),
145  (1 - f) * a.get_green() + f * b.get_green(),
146  (1 - f) * a.get_blue() + f * b.get_blue());
147 }
148 
149 /** Return a number suitable for being passed to a color
150  map that is gotten by linearly scaling between the
151  passed min and max (and truncating values that are out
152  of range).
153 */
154 IMPDISPLAYEXPORT double get_linear_color_map_value(double min, double max,
155  double value);
156 
157 /** \name Colormaps
158  These functions map a number in the interval [0,1]
159  to a color using some color map.
160 @{
161  */
162 //! Return the color for f from the jet color map
163 /** \see Color
164  */
165 IMPDISPLAYEXPORT Color get_jet_color(double f);
166 
167 //! Return the color for f from the hot color map
168 /** \see Color
169  */
170 IMPDISPLAYEXPORT Color get_hot_color(double f);
171 
172 //! Return the color for f from the RGB color map
173 /** \see Color
174  */
175 IMPDISPLAYEXPORT Color get_rgb_color(double f);
176 
177 //! Return the a grayscale value for f
178 /** \see Color
179  */
180 IMPDISPLAYEXPORT Color get_gray_color(double f);
181 
182 //! Return colors using the gnuplot default color map
183 /** \see Color
184  */
185 IMPDISPLAYEXPORT Color get_gnuplot_color(double f);
186 /** @} */
187 
189 
190 IMPDISPLAY_END_NAMESPACE
191 
192 #endif /* IMPDISPLAY_COLOR_H */
Represent an RGB color.
Definition: Color.h:24
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Various general useful macros for IMP.
Color get_rgb_color(double f)
Return the color for f from the RGB color map.
#define IMP_COMPARISONS(Name)
Implement comparison in a class using a compare function.
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:143
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
Base for a simple primitive-like type.
Definition: Value.h:21
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
int compare(const VectorD< D > &a, const VectorD< D > &b)
lexicographic comparison of two vectors
Definition: VectorD.h:179
Color get_display_color(unsigned int i)
Color get_gnuplot_color(double f)
Return colors using the gnuplot default color map.
std::ostream & show(Hierarchy h, std::ostream &out=std::cout)
Print the hierarchy using a given decorator to display each node.
Exception definitions and assertions.
Basic types used by IMP.
Color get_gray_color(double f)
Return the a grayscale value for f.
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
Definition: check_macros.h:168
Various general useful macros for IMP.
VectorD< D > operator*(double s, VectorD< D > o)
Definition: VectorD.h:193
Color get_jet_color(double f)
Return the color for f from the jet color map.
Various general useful macros for IMP.