]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/Color.h
first push
[gen-shell.git] / src / libshared / src / Color.h
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #ifndef INCLUDED_COLOR
28 #define INCLUDED_COLOR
29
30 #include <string>
31
32 #define _COLOR_INVERSE   0x00400000  // Inverse attribute.
33 #define _COLOR_256       0x00200000  // 256-color mode.
34 #define _COLOR_HASBG     0x00100000  // Has background color (all values taken).
35 #define _COLOR_HASFG     0x00080000  // Has foreground color (all values taken).
36 #define _COLOR_UNDERLINE 0x00040000  // General underline attribute.
37 #define _COLOR_BOLD      0x00020000  // 16-color bold attribute.
38 #define _COLOR_BRIGHT    0x00010000  // 16-color bright background attribute.
39 #define _COLOR_BG        0x0000FF00  // 8-bit background color index.
40 #define _COLOR_FG        0x000000FF  // 8-bit foreground color index.
41
42 class Color
43 {
44 public:
45   enum color_id {nocolor = 0, black, red, green, yellow, blue, magenta, cyan, white};
46
47   Color ();
48   Color (const Color&);
49   Color (unsigned int);                         // 256 | INVERSE | UNDERLINE | BOLD | BRIGHT | (BG << 8) | FG
50   Color (const std::string&);                   // "red on bright black"
51   Color (color_id);                             // fg.
52   Color (color_id, color_id, bool, bool, bool); // fg, bg, underline, bold, bright
53   operator std::string () const;
54   operator int () const;
55
56   void upgrade ();
57   void blend (const Color&);
58
59   std::string colorize (const std::string&) const;
60   static std::string colorize (const std::string&, const std::string&);
61   void _colorize (std::string&, const std::string&) const;
62   static std::string strip (const std::string&);
63
64   std::string code () const;
65   std::string end () const;
66
67   bool nontrivial () const;
68
69 private:
70   int find (const std::string&);
71   std::string fg () const;
72   std::string bg () const;
73
74 private:
75   unsigned int _value;
76 };
77
78 #endif