ROSE  0.9.9.139
Color.h
1 #ifndef ROSE_Color_H
2 #define ROSE_Color_H
3 
4 #include <algorithm>
5 #include <Sawyer/Map.h>
6 #include <string>
7 #include <rosedll.h>
8 
9 namespace Rose {
10 
12 namespace Color {
13 
19 typedef double Component;
20 
21 
23 template<typename T>
24 T clip(T c) {
25  return std::max(T(0), std::min(c, T(1)));
26 }
27 
28 class HSV;
29 #ifdef _MSC_VER
30  #undef RGB
31 #endif
32 
33 class ROSE_UTIL_API RGB {
34  Component r_, g_, b_, a_;
35 
36 public:
41  RGB(): r_(1.0), g_(0.0), b_(0.0), a_(1.0) {} // bright red
42 
44  RGB(Component r, Component g, Component b, Component a=1.0): r_(clip(r)), g_(clip(g)), b_(clip(b)), a_(clip(a)) {}
45 
47  RGB(const HSV&); // implicit
48 
54  Component r() const { return r_; }
55  Component g() const { return g_; }
56  Component b() const { return b_; }
57  Component a() const { return a_; }
58  Component red() const { return r_; }
59  Component green() const { return g_; }
60  Component blue() const { return b_; }
61  Component alpha() const { return a_; }
65  std::string toHtml() const;
66 };
67 
72 class ROSE_UTIL_API HSV {
73  Component h_, s_, v_, a_;
74 public:
75 
80  HSV(): h_(0.0), s_(1.0), v_(0.5), a_(1.0) {} // bright red
81 
83  HSV(Component h, Component s, Component v, Component a=1.0): h_(clip(h)), s_(clip(s)), v_(clip(v)), a_(clip(a)) {}
84 
86  HSV(const RGB&); // implicit
87 
93  Component h() const { return h_; }
94  Component s() const { return s_; }
95  Component v() const { return v_; }
96  Component a() const { return a_; }
97  Component hue() const { return h_; }
98  Component saturation() const { return s_; }
99  Component value() const { return v_; }
100  Component alpha() const { return a_; }
104  std::string toHtml() const;
105 };
106 
108 // Gradients
109 
113 class ROSE_UTIL_API Gradient {
114 public:
116 
117 private:
118  ColorMap colors_;
119  HSV nanColor_;
120 
121 public:
125  Gradient() {}
126 
133  Gradient(const RGB &color) { colors_.insert(0.0, color); } // implicit
134  Gradient(const HSV &color) { colors_.insert(0.0, color); } // implicit
141  Gradient(const HSV &color1, const HSV &color2) {
142  colors_.insert(0.0, color1);
143  colors_.insert(1.0, color2);
144  }
145 
147  void clear() { colors_.clear(); }
148 
152  const HSV& nanColor() const { return nanColor_; }
153  void nanColor(const HSV &c) { nanColor_ = c; }
160  void insert(double where, const HSV &color) { colors_.insert(where, color); }
161 
168  HSV interpolate(double) const;
169  HSV operator()(double x) const { return interpolate(x); }
177  const ColorMap& colorMap() const { return colors_; }
178  ColorMap& colorMap() { return colors_; }
180 };
181 
183 // Operations
184 
188 HSV invertBrightness(const HSV&);
189 
195 ROSE_UTIL_API HSV darken(const HSV&, double amount);
196 
201 ROSE_UTIL_API HSV lighten(const HSV&, double amount);
202 
207 ROSE_UTIL_API HSV fade(const HSV&, double amount);
208 
212 ROSE_UTIL_API std::string toHtml(const RGB&);
213 
214 // printing
215 std::ostream& operator<<(std::ostream&, const RGB&);
216 std::ostream& operator<<(std::ostream&, const HSV&);
217 std::ostream& operator<<(std::ostream&, const Gradient&);
218 
220 // Predefined colors
221 
222 extern const HSV clear; // my favorite color ;-)
223 extern const HSV black;
224 extern const HSV white;
225 extern const HSV red;
226 extern const HSV green;
227 extern const HSV blue;
228 extern const HSV cyan;
229 extern const HSV magenta;
230 extern const HSV yellow;
231 extern const HSV gray;
232 
233 } // namespace
234 } // namespace
235 
236 #endif
ROSE_UTIL_API HSV lighten(const HSV &, double amount)
Lighten a color.
void nanColor(const HSV &c)
Color for NaN lookups.
Definition: Color.h:153
void insert(double where, const HSV &color)
Insert a point into the gradient.
Definition: Color.h:160
RGB()
Default constructed color.
Definition: Color.h:41
HSV operator()(double x) const
Return a color for the specified position.
Definition: Color.h:169
ColorMap & colorMap()
Reference to color map.
Definition: Color.h:178
Colors in RGB space.
Definition: Color.h:33
Component green() const
Component of color.
Definition: Color.h:59
Component blue() const
Component of color.
Definition: Color.h:60
Component red() const
Component of color.
Definition: Color.h:58
Gradient()
Default constructor.
Definition: Color.h:125
HSV()
Default constructed color.
Definition: Color.h:80
Colors in HSV space.
Definition: Color.h:72
Main namespace for the ROSE library.
Map & clear()
Remove all nodes.
Definition: Sawyer/Map.h:616
HSV invertBrightness(const HSV &)
Invert the brightness.
const ColorMap & colorMap() const
Reference to color map.
Definition: Color.h:177
HSV(Component h, Component s, Component v, Component a=1.0)
Construct a color from components.
Definition: Color.h:83
void clear()
Remove all points from the gradient.
Definition: Color.h:147
Component r() const
Component of color.
Definition: Color.h:54
Gradient(const HSV &color)
Construct a one-color gradient.
Definition: Color.h:134
Component hue() const
Component of color.
Definition: Color.h:97
double Component
Type for color components.
Definition: Color.h:19
ROSE_UTIL_API HSV darken(const HSV &, double amount)
Darken a color.
Component value() const
Component of color.
Definition: Color.h:99
Component g() const
Component of color.
Definition: Color.h:55
Component a() const
Component of color.
Definition: Color.h:96
Component alpha() const
Component of color.
Definition: Color.h:61
ROSE_UTIL_API HSV fade(const HSV &, double amount)
Make a color less saturated.
Gradient(const RGB &color)
Construct a one-color gradient.
Definition: Color.h:133
Component b() const
Component of color.
Definition: Color.h:56
Component a() const
Component of color.
Definition: Color.h:57
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition: Sawyer/Map.h:530
ROSE_UTIL_API std::string toHtml(const RGB &)
Create an HTML color string.
Component h() const
Component of color.
Definition: Color.h:93
Gradient(const HSV &color1, const HSV &color2)
Construct a mapping with two colors.
Definition: Color.h:141
Component saturation() const
Component of color.
Definition: Color.h:98
T clip(T c)
Clip a floating point value between 0 and 1.
Definition: Color.h:24
Component s() const
Component of color.
Definition: Color.h:94
Mapping from floating point to color.
Definition: Color.h:113
const HSV & nanColor() const
Color for NaN lookups.
Definition: Color.h:152
Component v() const
Component of color.
Definition: Color.h:95
Component alpha() const
Component of color.
Definition: Color.h:100
RGB(Component r, Component g, Component b, Component a=1.0)
Construct a color specified by components.
Definition: Color.h:44