ROSE  0.11.96.11
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:
66 
67 public:
70 
72  static Ptr instance() {
73  return Ptr(new ColorizationParser);
74  }
75 
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::Color::Gradient::colorMap
ColorMap & colorMap()
Reference to color map.
Definition: Color.h:286
Sawyer::CommandLine::ParsedValue
Information about a parsed switch value.
Definition: util/Sawyer/CommandLine.h:505
Rose::Color::Layer::BACKGROUND
@ BACKGROUND
Background colors.
Definition: Color.h:56
Rose::Color::Colorization::enabled
Sawyer::Optional< Enabled::Flag > enabled
Whether colored output is enabled.
Definition: Color.h:41
Rose::Color::Gradient::operator()
HSV operator()(double x) const
Return a color for the specified position.
Definition: Color.h:277
Rose::Color::terminal
HSV terminal(const HSV &color, const Colorization &)
Adjust color for terminal.
Rose::Color::ColorizationMerge
Definition: Color.h:107
Sawyer::Optional< Enabled::Flag >
Rose::Color::Gradient::clear
void clear()
Remove all points from the gradient.
Definition: Color.h:255
Rose::Color::HSV::HSV
HSV()
Default constructed color.
Definition: Color.h:185
Rose::Color::RGB::blue
Component blue() const
Component of color.
Definition: Color.h:162
Rose::Color::RGB::green
Component green() const
Component of color.
Definition: Color.h:161
Rose::Color::HSV::value
Component value() const
Component of color.
Definition: Color.h:204
Rose::Color::Gradient::nanColor
void nanColor(const HSV &c)
Color for NaN lookups.
Definition: Color.h:261
Rose::Color::toHtml
ROSE_UTIL_API std::string toHtml(const RGB &)
Create an HTML color string.
Rose::Color::Colorization::merge
Colorization merge(const Colorization &) const
Merge this color and another to produce a result.
Rose::Color::HSV::saturation
Component saturation() const
Component of color.
Definition: Color.h:203
Rose::Color::ColorizationParser::parse
static Colorization parse(const char *input, const char **rest)
Parse a colorized output specification from a C string.
Rose::Color::Layer::Flag
Flag
Definition: Color.h:53
Rose::Color::HSV::alpha
Component alpha() const
Component of color.
Definition: Color.h:205
Rose::Color::HSV::hue
Component hue() const
Component of color.
Definition: Color.h:202
Rose::Color::Theme::Flag
Flag
Definition: Color.h:33
Rose::Color::darken
ROSE_UTIL_API HSV darken(const HSV &, double amount)
Darken a color.
Rose::Color::invertBrightness
HSV invertBrightness(const HSV &)
Invert the brightness.
Rose::Color::RGB
Colors in RGB space.
Definition: Color.h:135
Rose::Color::Enabled::ON
@ ON
Force colored output.
Definition: Color.h:24
Rose::Color::Gradient
Mapping from floating point to color.
Definition: Color.h:221
Rose::Color::HSV::v
Component v() const
Component of color.
Definition: Color.h:200
Sawyer::Container::Map< double, HSV >
Rose::Color::ColorizationParser::instance
static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver)
Allocating constructor.
Definition: Color.h:77
Rose::Color::Theme::LIGHT_ON_DARK
@ LIGHT_ON_DARK
Light text on dark background.
Definition: Color.h:35
Rose::Color::RGB::r
Component r() const
Component of color.
Definition: Color.h:156
Rose::Color::HSV
Colors in HSV space.
Definition: Color.h:177
Sawyer::CommandLine::ParsedValues
std::vector< ParsedValue > ParsedValues
A vector of parsed values.
Definition: util/Sawyer/CommandLine.h:638
Rose::Color::RGB::RGB
RGB()
Default constructed color.
Definition: Color.h:143
Rose::Color::Colorization::isEnabled
bool isEnabled() const
True if color is enabled in this situation.
Rose::Color::RGB::RGB
RGB(Component r, Component g, Component b, Component a=1.0)
Construct a color specified by components.
Definition: Color.h:146
Rose::Color::Gradient::colorMap
const ColorMap & colorMap() const
Reference to color map.
Definition: Color.h:285
Rose::Color::Enabled::Flag
Flag
Definition: Color.h:22
Rose::Color::RGB::b
Component b() const
Component of color.
Definition: Color.h:158
Sawyer::Container::Map::insert
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition: Sawyer/Map.h:603
Rose::Color::RGB::g
Component g() const
Component of color.
Definition: Color.h:157
Sawyer::CommandLine::ValueParser
Base class parsing a value from input.
Definition: util/Sawyer/CommandLine.h:699
Rose::Color::colorName
std::string colorName(AnsiColor)
Convert a color enum to a string.
Rose::Color::Gradient::Gradient
Gradient(const HSV &color)
Construct a one-color gradient.
Definition: Color.h:242
Rose::Color::Gradient::insert
void insert(double where, const HSV &color)
Insert a point into the gradient.
Definition: Color.h:268
Rose::Color::Colorization
Control colored command output.
Definition: Color.h:40
Rose::Color::ansiColorEscape
std::string ansiColorEscape(AnsiColor)
ANSI color escape for terminal output.
Rose::Color::Enabled::OFF
@ OFF
Disable colored output.
Definition: Color.h:23
Rose::Color::Gradient::Gradient
Gradient(const RGB &color)
Construct a one-color gradient.
Definition: Color.h:241
Sawyer::CommandLine::ValueAugmenter
Base class for value agumentors.
Definition: util/Sawyer/CommandLine.h:1896
Rose::Color::toAnsi
ROSE_UTIL_API std::string toAnsi(const RGB &, Layer::Flag layer)
Create an ANSI color escape.
Rose::Color::Gradient::Gradient
Gradient()
Default constructor.
Definition: Color.h:233
Rose::Color::Gradient::Gradient
Gradient(const HSV &color1, const HSV &color2)
Construct a mapping with two colors.
Definition: Color.h:249
Rose::Color::RGB::a
Component a() const
Component of color.
Definition: Color.h:159
Rose::Color::ColorizationParser::instance
static Ptr instance()
Allocating constructor.
Definition: Color.h:72
Rose::Color::Theme::DARK_ON_LIGHT
@ DARK_ON_LIGHT
Dark text on light background.
Definition: Color.h:34
Rose::Color::RGB::red
Component red() const
Component of color.
Definition: Color.h:160
Sawyer::SharedPointer< ValueSaver >
Rose::Color::HSV::HSV
HSV(Component h, Component s, Component v, Component a=1.0)
Construct a color from components.
Definition: Color.h:188
Rose::Color::Enabled::AUTO
@ AUTO
Use colored output if standard output is a terminal.
Definition: Color.h:25
Rose::Color::ColorizationParser
Parses an output color specification.
Definition: Color.h:61
Rose::Color::ColorizationParser::docString
static std::string docString()
Documentation for parser.
Rose
Main namespace for the ROSE library.
Definition: BinaryTutorial.dox:3
Rose::Color::Layer::FOREGROUND
@ FOREGROUND
Foreground colors.
Definition: Color.h:55
Rose::Color::Component
double Component
Type for color components.
Definition: Color.h:124
Sawyer::CommandLine::ValueParser::valueSaver
const ValueSaver::Ptr valueSaver() const
Property: functor responsible for saving a parsed value in user storage.
Definition: util/Sawyer/CommandLine.h:741
Rose::Color::fade
ROSE_UTIL_API HSV fade(const HSV &, double amount)
Make a color less saturated.
Rose::Color::HSV::a
Component a() const
Component of color.
Definition: Color.h:201
Rose::Color::Layer::NONE
@ NONE
No specific layer.
Definition: Color.h:54
Rose::Color::RGB::alpha
Component alpha() const
Component of color.
Definition: Color.h:163
Sawyer::CommandLine::Location
Position within a command-line.
Definition: util/Sawyer/CommandLine.h:216
Rose::Color::AnsiColor
AnsiColor
ANSI color names for terminal output.
Definition: Color.h:359
Rose::Color::HSV::s
Component s() const
Component of color.
Definition: Color.h:199
Rose::Color::Colorization::theme
Sawyer::Optional< Theme::Flag > theme
The color theme.
Definition: Color.h:42
Rose::Color::ColorizationParser::Ptr
Sawyer::SharedPointer< ColorizationParser > Ptr
Shared ownership pointer to a ColorizationParser.
Definition: Color.h:69
Rose::Color::ColorizationMerge::operator()
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.
Sawyer::Container::Map::clear
Map & clear()
Remove all nodes.
Definition: Sawyer/Map.h:689
Rose::Color::Gradient::nanColor
const HSV & nanColor() const
Color for NaN lookups.
Definition: Color.h:260
Rose::Color::lighten
ROSE_UTIL_API HSV lighten(const HSV &, double amount)
Lighten a color.
Rose::Color::clip
T clip(T c)
Clip a floating point value between 0 and 1.
Definition: Color.h:128
Rose::Color::HSV::h
Component h() const
Component of color.
Definition: Color.h:198