ROSE 0.11.145.192
Color.h
1#ifndef ROSE_Color_H
2#define ROSE_Color_H
3#include <RoseFirst.h>
4
5#include <algorithm>
6#include <Sawyer/CommandLine.h>
7#include <Sawyer/Map.h>
8#include <string>
9#include <rosedll.h>
10
11// Name space pollution cleanup
12#ifdef _MSC_VER
13 #undef RGB
14#endif
15
16namespace Rose {
17
19namespace Color {
20
22enum class Enabled {
23 OFF,
24 ON,
25 AUTO
26};
27
31enum class Theme {
34};
35
47
49enum class Layer {
50 NONE,
53};
54
62protected:
66
67public:
70
72 static Ptr instance() {
73 return Ptr(new ColorizationParser);
74 }
75
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
98private:
100 operator()(const char *input, const char **rest, const Sawyer::CommandLine::Location &loc) /*override*/;
101};
102
103ColorizationParser::Ptr colorizationParser(Colorization &storage);
104ColorizationParser::Ptr colorizationParser();
105
106// Used internally to merge colorization command-line switch arguments.
108public:
110
111 static Ptr instance() {
112 return Ptr(new ColorizationMerge);
113 }
114
117};
118
123typedef double Component;
124
126template<typename T>
127T clip(T c) {
128 return std::max(T(0), std::min(c, T(1)));
129}
130
131class HSV;
132
134class ROSE_UTIL_API RGB {
135 Component r_, g_, b_, a_;
136
137public:
142 RGB(): r_(1.0), g_(0.0), b_(0.0), a_(1.0) {} // bright red
143
147 RGB(Component r, Component g, Component b, Component a=1.0): r_(clip(r)), g_(clip(g)), b_(clip(b)), a_(clip(a)) {}
148
150 RGB(const HSV&); // implicit
151
159 Component r() const { return r_; }
160 Component g() const { return g_; }
161 Component b() const { return b_; }
162 Component a() const { return a_; }
163 Component red() const { return r_; }
164 Component green() const { return g_; }
165 Component blue() const { return b_; }
166 Component alpha() const { return a_; }
170 std::string toHtml() const;
171
173 std::string toAnsi(Layer) const;
174};
175
180class ROSE_UTIL_API HSV {
181 Component h_, s_, v_, a_;
182public:
183
188 HSV(): h_(0.0), s_(1.0), v_(0.5), a_(1.0) {} // bright red
189
193 HSV(Component h, Component s, Component v, Component a=1.0): h_(clip(h)), s_(clip(s)), v_(clip(v)), a_(clip(a)) {}
194
196 HSV(const RGB&); // implicit
197
205 Component h() const { return h_; }
206 Component s() const { return s_; }
207 Component v() const { return v_; }
208 Component a() const { return a_; }
209 Component hue() const { return h_; }
210 Component saturation() const { return s_; }
211 Component value() const { return v_; }
212 Component alpha() const { return a_; }
216 std::string toHtml() const;
217
219 std::string toAnsi(Layer) const;
220};
221
223// Gradients
224
228class ROSE_UTIL_API Gradient {
229public:
231
232private:
233 ColorMap colors_;
234 HSV nanColor_;
235
236public:
241
248 Gradient(const RGB &color) { colors_.insert(0.0, color); } // implicit
249 Gradient(const HSV &color) { colors_.insert(0.0, color); } // implicit
256 Gradient(const HSV &color1, const HSV &color2) {
257 colors_.insert(0.0, color1);
258 colors_.insert(1.0, color2);
259 }
260
262 void clear() { colors_.clear(); }
263
267 const HSV& nanColor() const { return nanColor_; }
268 void nanColor(const HSV &c) { nanColor_ = c; }
275 void insert(double where, const HSV &color) { colors_.insert(where, color); }
276
283 HSV interpolate(double) const;
284 HSV operator()(double x) const { return interpolate(x); }
292 const ColorMap& colorMap() const { return colors_; }
293 ColorMap& colorMap() { return colors_; }
295};
296
298// Operations
299
304
310ROSE_UTIL_API HSV darken(const HSV&, double amount);
311
316ROSE_UTIL_API HSV lighten(const HSV&, double amount);
317
322ROSE_UTIL_API HSV fade(const HSV&, double amount);
323
328HSV terminal(const HSV &color, const Colorization&);
329
333ROSE_UTIL_API std::string toHtml(const RGB&);
334
342ROSE_UTIL_API std::string toAnsi(const RGB&, Layer);
343
344// printing
345std::ostream& operator<<(std::ostream&, const RGB&);
346std::ostream& operator<<(std::ostream&, const HSV&);
347std::ostream& operator<<(std::ostream&, const Gradient&);
348
350// Predefined colors
351
352extern const HSV HSV_CLEAR; // my favorite color ;-)
353extern const HSV HSV_BLACK;
354extern const HSV HSV_WHITE;
355extern const HSV HSV_RED;
356extern const HSV HSV_GREEN;
357extern const HSV HSV_BLUE;
358extern const HSV HSV_CYAN;
359extern const HSV HSV_MAGENTA;
360extern const HSV HSV_YELLOW;
361extern const HSV HSV_GRAY;
364
366enum class AnsiColor {
367 CLEAR,
368 RED,
369 GREEN,
370 YELLOW,
371 BLUE,
372 MAGENTA,
373 CYAN,
374 GRAY
375};
376
378std::string colorName(AnsiColor);
379
382
383} // namespace
384} // namespace
385
386#endif
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.
Parses an output color specification.
Definition Color.h:61
static Ptr instance()
Allocating constructor.
Definition Color.h:72
Sawyer::SharedPointer< ColorizationParser > Ptr
Shared ownership pointer to a ColorizationParser.
Definition Color.h:69
static Colorization parse(const char *input, const char **rest)
Parse a colorized output specification from a C string.
static Colorization parse(const std::string &input)
Parse a colorized output specification from a C++ string.
static Ptr instance(const Sawyer::CommandLine::ValueSaver::Ptr &valueSaver)
Allocating constructor.
Definition Color.h:77
static std::string docString()
Documentation for parser.
Mapping from floating point to color.
Definition Color.h:228
HSV operator()(double x) const
Return a color for the specified position.
Definition Color.h:284
Gradient(const HSV &color)
Construct a one-color gradient.
Definition Color.h:249
const ColorMap & colorMap() const
Reference to color map.
Definition Color.h:292
Gradient(const RGB &color)
Construct a one-color gradient.
Definition Color.h:248
void insert(double where, const HSV &color)
Insert a point into the gradient.
Definition Color.h:275
const HSV & nanColor() const
Color for NaN lookups.
Definition Color.h:267
ColorMap & colorMap()
Reference to color map.
Definition Color.h:293
void clear()
Remove all points from the gradient.
Definition Color.h:262
void nanColor(const HSV &c)
Color for NaN lookups.
Definition Color.h:268
Gradient(const HSV &color1, const HSV &color2)
Construct a mapping with two colors.
Definition Color.h:256
HSV interpolate(double) const
Return a color for the specified position.
Gradient()
Default constructor.
Definition Color.h:240
Colors in HSV space.
Definition Color.h:180
Component v() const
Component of color.
Definition Color.h:207
Component hue() const
Component of color.
Definition Color.h:209
HSV(Component h, Component s, Component v, Component a=1.0)
Construct a color from components.
Definition Color.h:193
Component value() const
Component of color.
Definition Color.h:211
HSV()
Default constructed color.
Definition Color.h:188
Component h() const
Component of color.
Definition Color.h:205
std::string toHtml() const
Convert to HTML string.
Component alpha() const
Component of color.
Definition Color.h:212
HSV(const RGB &)
Convert an RGB color to HSV space.
Component a() const
Component of color.
Definition Color.h:208
Component s() const
Component of color.
Definition Color.h:206
std::string toAnsi(Layer) const
Convert to ANSI color escape.
Component saturation() const
Component of color.
Definition Color.h:210
Colors in RGB space.
Definition Color.h:134
RGB(const HSV &)
Convert an HSV color to RGB space.
RGB(Component r, Component g, Component b, Component a=1.0)
Construct a color specified by components.
Definition Color.h:147
Component blue() const
Component of color.
Definition Color.h:165
Component red() const
Component of color.
Definition Color.h:163
std::string toHtml() const
Convert to HTML string.
Component r() const
Component of color.
Definition Color.h:159
Component a() const
Component of color.
Definition Color.h:162
Component g() const
Component of color.
Definition Color.h:160
Component b() const
Component of color.
Definition Color.h:161
RGB()
Default constructed color.
Definition Color.h:142
Component alpha() const
Component of color.
Definition Color.h:166
Component green() const
Component of color.
Definition Color.h:164
std::string toAnsi(Layer) const
Convert to ANSI color escape.
Information about a parsed switch value.
Base class for value agumentors.
Base class parsing a value from input.
const ValueSaver::Ptr valueSaver() const
Property: functor responsible for saving a parsed value in user storage.
Container associating values with keys.
Definition Sawyer/Map.h:72
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition Sawyer/Map.h:646
Map & clear()
Remove all nodes.
Definition Sawyer/Map.h:732
Holds a value or nothing.
Definition Optional.h:56
ROSE_UTIL_API HSV lighten(const HSV &, double amount)
Lighten a color.
const HSV HSV_MAGENTA
Magenta.
const HSV HSV_RED
Red.
std::string colorName(AnsiColor)
Convert a color enum to a string.
const HSV HSV_WHITE
White.
const HSV HSV_GREEN
Green.
AnsiColor
ANSI color names for terminal output.
Definition Color.h:366
const HSV HSV_YELLOW
Yellow.
double Component
Type for color components.
Definition Color.h:123
const HSV HSV_GRAY
Gray.
const HSV HSV_BLUE
Blue.
Layer
Layer to which color applies.
Definition Color.h:49
@ BACKGROUND
Background colors.
@ NONE
No specific layer.
@ FOREGROUND
Foreground colors.
const HSV HSV_BLACK
Black.
ROSE_UTIL_API HSV darken(const HSV &, double amount)
Darken a color.
const HSV HSV_CYAN
Cyan.
Enabled
Whether colored output is enabled.
Definition Color.h:22
@ OFF
Disable colored output.
@ ON
Force colored output.
@ AUTO
Use colored output if standard output is a terminal.
Theme
Color theme.
Definition Color.h:31
@ DARK_ON_LIGHT
Dark text on light background.
@ LIGHT_ON_DARK
Light text on dark background.
ROSE_UTIL_API HSV fade(const HSV &, double amount)
Make a color less saturated.
ROSE_UTIL_API std::string toHtml(const RGB &)
Create an HTML color string.
HSV invertBrightness(const HSV &)
Invert the brightness.
ROSE_UTIL_API std::string toAnsi(const RGB &, Layer)
Create an ANSI color escape.
HSV terminal(const HSV &color, const Colorization &)
Adjust color for terminal.
T clip(T c)
Clip a floating point value between 0 and 1.
Definition Color.h:127
std::string ansiColorEscape(AnsiColor)
ANSI color escape for terminal output.
The ROSE library.
std::vector< ParsedValue > ParsedValues
A vector of parsed values.
Control colored command output.
Definition Color.h:37
Sawyer::Optional< Theme > theme
The color theme.
Definition Color.h:39
Sawyer::Optional< Enabled > enabled
Whether colored output is enabled.
Definition Color.h:38
bool isEnabled() const
True if color is enabled in this situation.
Colorization merge(const Colorization &) const
Merge this color and another to produce a result.
Position within a command-line.