IMP logo
IMP Reference Guide  2.22.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-2022 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 <array>
18 #include <cereal/access.hpp>
19 
20 IMPDISPLAY_BEGIN_NAMESPACE
21 
22 //! Represent an RGB color
23 /**
24  */
25 class IMPDISPLAYEXPORT Color : public Value {
26  std::array<double, 3> c_;
27 
28  friend class cereal::access;
29 
30  template<class Archive> void serialize(Archive &ar) {
31  ar(c_[0], c_[1], c_[2]);
32  }
33 
34  int compare(double a, double b) const {
35  if (a < b)
36  return -1;
37  else if (a > b)
38  return 1;
39  else
40  return 0;
41  }
42  int compare(const Color &o) const {
43  for (unsigned int i = 0; i < 3; ++i) {
44  int c = compare(c_[i], o.c_[i]);
45  if (c != 0) return c;
46  }
47  return 0;
48  }
49 
50  public:
51  IMP_CXX11_DEFAULT_COPY_CONSTRUCTOR(Color);
52 
53  Color();
54 
55  /** Components must be between 0 and 1
56  */
57  Color(double r, double g, double b);
58 
59  /** Components must be between 0 and 1
60  */
61  template <class It>
62  Color(It b, It e) {
63  std::copy(b, e, c_.begin());
64  }
65 
66  ~Color();
67  /** @name Component access
68  @{
69  */
70  double get_red() const { return c_[0]; }
71 
72  double get_green() const { return c_[1]; }
73  double get_blue() const { return c_[2]; }
74 #ifndef SWIG
75  typedef std::array<double, 3>::const_iterator ComponentIterator;
76  ComponentIterator components_begin() const { return c_.begin(); }
77  ComponentIterator components_end() const { return c_.end(); }
78  const std::array<double, 3> &get_rgb() const { return c_; }
79 #endif
80  //!@}
81 
82  void show(std::ostream &out, std::string delim) const {
83  out << get_red() << delim << get_green() << delim << get_blue();
84  }
85 
86  IMP_SHOWABLE_INLINE(Color, show(out, " "););
87 
88  IMP_COMPARISONS(Color);
89 };
90 
91 /** Produce a color that is attempted to be contrasting with the
92  i-1 previous colors. Currently, they recycle after 11, but they
93  can be darkened instead. Just ask.
94  */
95 IMPDISPLAYEXPORT Color get_display_color(unsigned int i);
96 
97 #ifndef SWIG
98 #ifndef IMP_DOXYGEN
99 struct SpacesIO {
100  const Color &v_;
101  explicit SpacesIO(const Color &v) : v_(v) {}
102 };
103 
104 struct CommasIO {
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  s.v_.show(out, " ");
110  return out;
111 }
112 inline std::ostream &operator<<(std::ostream &out, const CommasIO &s) {
113  s.v_.show(out, ", ");
114  return out;
115 }
116 
117 //! Use this before outputting to delimited vector entries with a space
118 /** std::cout << spaces_io(v);
119  produces "1.0 2.0 3.0"
120  \see Color
121  */
122 inline SpacesIO spaces_io(const Color &v) { return SpacesIO(v); }
123 
124 //! Use this before outputting to delimited vector entries with a comma
125 /** std::cout << commas_io(v);
126  produces "1.0, 2.0, 3.0"
127  \see Color
128  */
129 inline CommasIO commas_io(const Color &v) { return CommasIO(v); }
130 
131 //! Multiply a color by a value less than 1
132 /** \unstable{Color multiplication}
133  */
134 inline Color operator*(double d, Color c) {
135  IMP_USAGE_CHECK(d <= 1 && d >= 0, "Colors can only fade with multiplication");
136  return Color(c.get_red() * d, c.get_green() * d, c.get_blue() * d);
137 }
138 
139 //! Multiply a color by a value less than 1
140 /** \unstable{Color multiplication}
141  */
142 inline Color operator*(Color c, double d) { return d * c; }
143 #endif
144 
145 #endif // SWIG
146 
147 //! Return a color interpolated between a and b in RGB space
148 /** If f is 0, then a is returned, if f is 1, b is returned.
149  \see Color
150  */
151 inline Color get_interpolated_rgb(const Color &a, const Color &b, double f) {
152  return Color((1 - f) * a.get_red() + f * b.get_red(),
153  (1 - f) * a.get_green() + f * b.get_green(),
154  (1 - f) * a.get_blue() + f * b.get_blue());
155 }
156 
157 /** Return a number suitable for being passed to a color
158  map that is gotten by linearly scaling between the
159  passed min and max (and truncating values that are out
160  of range).
161 */
162 IMPDISPLAYEXPORT double get_linear_color_map_value(double min, double max,
163  double value);
164 
165 /** \name Colormaps
166  These functions map a number in the interval [0,1]
167  to a color using some color map.
168 @{
169  */
170 //! Return the color for f from the jet color map
171 /** \see Color
172  */
173 IMPDISPLAYEXPORT Color get_jet_color(double f);
174 
175 //! Return the color for f from the hot color map
176 /** \see Color
177  */
178 IMPDISPLAYEXPORT Color get_hot_color(double f);
179 
180 //! Return the color for f from the RGB color map
181 /** \see Color
182  */
183 IMPDISPLAYEXPORT Color get_rgb_color(double f);
184 
185 //! Return the a grayscale value for f
186 /** \see Color
187  */
188 IMPDISPLAYEXPORT Color get_gray_color(double f);
189 
190 //! Return colors using the gnuplot default color map
191 /** \see Color
192  */
193 IMPDISPLAYEXPORT Color get_gnuplot_color(double f);
194 /** @} */
195 
197 
198 IMPDISPLAY_END_NAMESPACE
199 
200 #endif /* IMPDISPLAY_COLOR_H */
Represent an RGB color.
Definition: Color.h:25
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
Helper macros for implementing comparisons of IMP objects.
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:151
Color get_hot_color(double f)
Return the color for f from the hot color map.
A more IMP-like version of the std::vector.
Definition: Vector.h:50
Color(It b, It e)
Definition: Color.h:62
Base class for a simple primitive-like type.
Definition: Value.h:23
#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:166
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.
Helper macros for throwing and handling exceptions.
Base class for a simple primitive-like type.
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
Macros to help in implementing Value objects.
VectorD< D > operator*(double s, VectorD< D > o)
Definition: VectorD.h:180
Color get_jet_color(double f)
Return the color for f from the jet color map.
Macros to help with objects that can be printed to a stream.