ROSE  0.11.22.0
Color.h
1 #ifndef ROSE_Color_H
2 #define ROSE_Color_H
3 
4 #include <algorithm>
5 #include <Sawyer/CommandLine.h>
6 #include <Sawyer/Map.h>
7 #include <string>
8 #include <rosedll.h>
9 
10 // Name space pollution cleanup
11 #ifdef _MSC_VER
12  #undef RGB
13 #endif
14 
15 namespace Rose {
16 
18 namespace Color {
19 
21 namespace Enabled { // done this way because ROSE must use only C++03
22 enum Flag {
23  OFF,
24  ON,
26 };
27 } // namespace
28 
32 namespace Theme { // done this way because ROSE must use only C++03
33 enum Flag {
36 };
37 } // namespace
38 
40 struct Colorization {
45  bool isEnabled() const;
46 
48  Colorization merge(const Colorization&) const;
49 };
50 
52 namespace Layer {
53 enum Flag {
54  NONE,
57 };
58 } // namespace
59 
62 protected:
65  : Sawyer::CommandLine::ValueParser(valueSaver) {}
66 
67 public:
70 
72  static Ptr instance() {
73  return Ptr(new ColorizationParser);
74  }
75 
77  static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver) {
78  return Ptr(new ColorizationParser(valueSaver));
79  }
80 
82  static std::string docString();
83 
88  static Colorization parse(const char *input, const char **rest);
89 
96  static Colorization parse(const std::string &input);
97 
98 private:
100  operator()(const char *input, const char **rest, const Sawyer::CommandLine::Location &loc) /*override*/;
101 };
102 
103 ColorizationParser::Ptr colorizationParser(Colorization &storage);
104 ColorizationParser::Ptr colorizationParser();
105 
106 // Used internally to merge colorization command-line switch arguments.
108 public:
110 
111  static Ptr instance() {
112  return Ptr(new ColorizationMerge);
113  }
114 
117 };
118 
124 typedef double Component;
125 
127 template<typename T>
128 T clip(T c) {
129  return std::max(T(0), std::min(c, T(1)));
130 }
131 
132 class HSV;
133 
135 class ROSE_UTIL_API RGB {
136  Component r_, g_, b_, a_;
137 
138 public:
143  RGB(): r_(1.0), g_(0.0), b_(0.0), a_(1.0) {} // bright red
144 
146  RGB(Component r, Component g, Component b, Component a=1.0): r_(clip(r)), g_(clip(g)), b_(clip(b)), a_(clip(a)) {}
147 
149  RGB(const HSV&); // implicit
150 
156  Component r() const { return r_; }
157  Component g() const { return g_; }
158  Component b() const { return b_; }
159  Component a() const { return a_; }
160  Component red() const { return r_; }
161  Component green() const { return g_; }
162  Component blue() const { return b_; }
163  Component alpha() const { return a_; }
167  std::string toHtml() const;
168 
170  std::string toAnsi(Layer::Flag) const;
171 };
172 
177 class ROSE_UTIL_API HSV {
178  Component h_, s_, v_, a_;
179 public:
180 
185  HSV(): h_(0.0), s_(1.0), v_(0.5), a_(1.0) {} // bright red
186 
188  HSV(Component h, Component s, Component v, Component a=1.0): h_(clip(h)), s_(clip(s)), v_(clip(v)), a_(clip(a)) {}
189 
191  HSV(const RGB&); // implicit
192 
198  Component h() const { return h_; }
199  Component s() const { return s_; }
200  Component v() const { return v_; }
201  Component a() const { return a_; }
202  Component hue() const { return h_; }
203  Component saturation() const { return s_; }
204  Component value() const { return v_; }
205  Component alpha() const { return a_; }
209  std::string toHtml() const;
210 
212  std::string toAnsi(Layer::Flag) const;
213 };
214 
216 // Gradients
217 
221 class ROSE_UTIL_API Gradient {
222 public:
224 
225 private:
226  ColorMap colors_;
227  HSV nanColor_;
228 
229 public:
233  Gradient() {}
234 
241  Gradient(const RGB &color) { colors_.insert(0.0, color); } // implicit
242  Gradient(const HSV &color) { colors_.insert(0.0, color); } // implicit
249  Gradient(const HSV &color1, const HSV &color2) {
250  colors_.insert(0.0, color1);
251  colors_.insert(1.0, color2);
252  }
253 
255  void clear() { colors_.clear(); }
256 
260  const HSV& nanColor() const { return nanColor_; }
261  void nanColor(const HSV &c) { nanColor_ = c; }
268  void insert(double where, const HSV &color) { colors_.insert(where, color); }
269 
276  HSV interpolate(double) const;
277  HSV operator()(double x) const { return interpolate(x); }
285  const ColorMap& colorMap() const { return colors_; }
286  ColorMap& colorMap() { return colors_; }
288 };
289 
291 // Operations
292 
296 HSV invertBrightness(const HSV&);
297 
303 ROSE_UTIL_API HSV darken(const HSV&, double amount);
304 
309 ROSE_UTIL_API HSV lighten(const HSV&, double amount);
310 
315 ROSE_UTIL_API HSV fade(const HSV&, double amount);
316 
321 HSV terminal(const HSV &color, const Colorization&);
322 
326 ROSE_UTIL_API std::string toHtml(const RGB&);
327 
335 ROSE_UTIL_API std::string toAnsi(const RGB&, Layer::Flag layer);
336 
337 // printing
338 std::ostream& operator<<(std::ostream&, const RGB&);
339 std::ostream& operator<<(std::ostream&, const HSV&);
340 std::ostream& operator<<(std::ostream&, const Gradient&);
341 
343 // Predefined colors
344 
345 extern const HSV HSV_CLEAR; // my favorite color ;-)
346 extern const HSV HSV_BLACK;
347 extern const HSV HSV_WHITE;
348 extern const HSV HSV_RED;
349 extern const HSV HSV_GREEN;
350 extern const HSV HSV_BLUE;
351 extern const HSV HSV_CYAN;
352 extern const HSV HSV_MAGENTA;
353 extern const HSV HSV_YELLOW;
354 extern const HSV HSV_GRAY;
355 
357 
359 enum AnsiColor { ANSI_CLEAR, ANSI_RED, ANSI_GREEN, ANSI_YELLOW, ANSI_BLUE, ANSI_MAGENTA, ANSI_CYAN, ANSI_GRAY };
360 
362 std::string colorName(AnsiColor);
363 
365 std::string ansiColorEscape(AnsiColor);
366 
367 } // namespace
368 } // namespace
369 
370 #endif
ROSE_UTIL_API HSV lighten(const HSV &, double amount)
Lighten a color.
std::vector< ParsedValue > ParsedValues
A vector of parsed values.
Control colored command output.
Definition: Color.h:40
bool isEnabled() const
True if color is enabled in this situation.
void nanColor(const HSV &c)
Color for NaN lookups.
Definition: Color.h:261
void insert(double where, const HSV &color)
Insert a point into the gradient.
Definition: Color.h:268
const ValueSaver::Ptr valueSaver() const
Property: functor responsible for saving a parsed value in user storage.
Force colored output.
Definition: Color.h:24
RGB()
Default constructed color.
Definition: Color.h:143
Colorization merge(const Colorization &) const
Merge this color and another to produce a result.
virtual Sawyer::CommandLine::ParsedValues operator()(const Sawyer::CommandLine::ParsedValues &prev, const Sawyer::CommandLine::ParsedValues &cur)
Called when a switch's value is about to be stored into the ParserResult.
HSV operator()(double x) const
Return a color for the specified position.
Definition: Color.h:277
ColorMap & colorMap()
Reference to color map.
Definition: Color.h:286
static Colorization parse(const char *input, const char **rest)
Parse a colorized output specification from a C string.
Colors in RGB space.
Definition: Color.h:135
Component green() const
Component of color.
Definition: Color.h:161
Component blue() const
Component of color.
Definition: Color.h:162
Component red() const
Component of color.
Definition: Color.h:160
Gradient()
Default constructor.
Definition: Color.h:233
HSV()
Default constructed color.
Definition: Color.h:185
Base class for value agumentors.
Colors in HSV space.
Definition: Color.h:177
Main namespace for the ROSE library.
Map & clear()
Remove all nodes.
Definition: Sawyer/Map.h:680
HSV terminal(const HSV &color, const Colorization &)
Adjust color for terminal.
HSV invertBrightness(const HSV &)
Invert the brightness.
AnsiColor
ANSI color names for terminal output.
Definition: Color.h:359
Use colored output if standard output is a terminal.
Definition: Color.h:25
const ColorMap & colorMap() const
Reference to color map.
Definition: Color.h:285
Disable colored output.
Definition: Color.h:23
static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver)
Allocating constructor.
Definition: Color.h:77
HSV(Component h, Component s, Component v, Component a=1.0)
Construct a color from components.
Definition: Color.h:188
Sawyer::Optional< Enabled::Flag > enabled
Whether colored output is enabled.
Definition: Color.h:41
void clear()
Remove all points from the gradient.
Definition: Color.h:255
Information about a parsed switch value.
Component r() const
Component of color.
Definition: Color.h:156
ROSE_UTIL_API std::string toAnsi(const RGB &, Layer::Flag layer)
Create an ANSI color escape.
static Ptr instance()
Allocating constructor.
Definition: Color.h:72
Gradient(const HSV &color)
Construct a one-color gradient.
Definition: Color.h:242
Component hue() const
Component of color.
Definition: Color.h:202
static std::string docString()
Documentation for parser.
double Component
Type for color components.
Definition: Color.h:124
ROSE_UTIL_API HSV darken(const HSV &, double amount)
Darken a color.
Foreground colors.
Definition: Color.h:55
Light text on dark background.
Definition: Color.h:35
Parses an output color specification.
Definition: Color.h:61
Component value() const
Component of color.
Definition: Color.h:204
Component g() const
Component of color.
Definition: Color.h:157
Component a() const
Component of color.
Definition: Color.h:201
Component alpha() const
Component of color.
Definition: Color.h:163
Sawyer::Optional< Theme::Flag > theme
The color theme.
Definition: Color.h:42
ROSE_UTIL_API HSV fade(const HSV &, double amount)
Make a color less saturated.
Sawyer::SharedPointer< ColorizationParser > Ptr
Shared ownership pointer to a OutputParser.
Definition: Color.h:69
std::string ansiColorEscape(AnsiColor)
ANSI color escape for terminal output.
Gradient(const RGB &color)
Construct a one-color gradient.
Definition: Color.h:241
Component b() const
Component of color.
Definition: Color.h:158
Component a() const
Component of color.
Definition: Color.h:159
Background colors.
Definition: Color.h:56
Dark text on light background.
Definition: Color.h:34
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition: Sawyer/Map.h:594
Position within a command-line.
Base class parsing a value from input.
ROSE_UTIL_API std::string toHtml(const RGB &)
Create an HTML color string.
Component h() const
Component of color.
Definition: Color.h:198
Gradient(const HSV &color1, const HSV &color2)
Construct a mapping with two colors.
Definition: Color.h:249
Component saturation() const
Component of color.
Definition: Color.h:203
T clip(T c)
Clip a floating point value between 0 and 1.
Definition: Color.h:128
Component s() const
Component of color.
Definition: Color.h:199
Mapping from floating point to color.
Definition: Color.h:221
const HSV & nanColor() const
Color for NaN lookups.
Definition: Color.h:260
std::string colorName(AnsiColor)
Convert a color enum to a string.
Component v() const
Component of color.
Definition: Color.h:200
Component alpha() const
Component of color.
Definition: Color.h:205
RGB(Component r, Component g, Component b, Component a=1.0)
Construct a color specified by components.
Definition: Color.h:146
No specific layer.
Definition: Color.h:54