Color: C++11

This commit is contained in:
Paul Beckingham
2016-02-03 19:39:38 -05:00
parent 4d2c97f2c3
commit 1579705fec
2 changed files with 17 additions and 23 deletions

View File

@@ -26,8 +26,6 @@
#include <cmake.h>
#include <Color.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <algorithm>
@@ -143,10 +141,9 @@ Color::Color (const std::string& spec)
bool bg = false;
int index;
std::string word;
for (auto& it : words)
for (auto& word : words)
{
word = Lexer::lowerCase (Lexer::trim (it));
word = Lexer::lowerCase (Lexer::trim (word));
if (word == "bold") fg_value |= _COLOR_BOLD;
else if (word == "bright") bg_value |= _COLOR_BRIGHT;
@@ -176,9 +173,9 @@ Color::Color (const std::string& spec)
else if (! word.compare (0, 4, "grey", 4) ||
! word.compare (0, 4, "gray", 4))
{
index = atoi (word.substr (4).c_str ());
index = strtol (word.substr (4).c_str (), nullptr, 10);
if (index < 0 || index > 23)
throw format (STRING_COLOR_UNRECOGNIZED, it);
throw format (STRING_COLOR_UNRECOGNIZED, word);
if (bg)
{
@@ -197,18 +194,18 @@ Color::Color (const std::string& spec)
// rgbRGB, where 0 <= R,G,B <= 5.
else if (! word.compare (0, 3, "rgb", 3))
{
index = atoi (word.substr (3).c_str ());
index = strtol (word.substr (3).c_str (), nullptr, 10);
if (word.length () != 6 ||
index < 0 || index > 555)
throw format (STRING_COLOR_UNRECOGNIZED, it);
throw format (STRING_COLOR_UNRECOGNIZED, word);
int r = atoi (word.substr (3, 1).c_str ());
int g = atoi (word.substr (4, 1).c_str ());
int b = atoi (word.substr (5, 1).c_str ());
int r = strtol (word.substr (3, 1).c_str (), nullptr, 10);
int g = strtol (word.substr (4, 1).c_str (), nullptr, 10);
int b = strtol (word.substr (5, 1).c_str (), nullptr, 10);
if (r < 0 || r > 5 ||
g < 0 || g > 5 ||
b < 0 || b > 5)
throw format (STRING_COLOR_UNRECOGNIZED, it);
throw format (STRING_COLOR_UNRECOGNIZED, word);
index = 16 + r*36 + g*6 + b;
@@ -229,9 +226,9 @@ Color::Color (const std::string& spec)
// colorN, where 0 <= N <= 255.
else if (! word.compare (0, 5, "color", 5))
{
index = atoi (word.substr (5).c_str ());
index = strtol (word.substr (5).c_str (), nullptr, 10);
if (index < 0 || index > 255)
throw format (STRING_COLOR_UNRECOGNIZED, it);
throw format (STRING_COLOR_UNRECOGNIZED, word);
upgrade ();
@@ -249,7 +246,7 @@ Color::Color (const std::string& spec)
}
}
else if (word != "")
throw format (STRING_COLOR_UNRECOGNIZED, it);
throw format (STRING_COLOR_UNRECOGNIZED, word);
}
// Now combine the fg and bg into a single color.
@@ -409,7 +406,7 @@ void Color::upgrade ()
}
////////////////////////////////////////////////////////////////////////////////
std::string Color::colorize (const std::string& input)
std::string Color::colorize (const std::string& input) const
{
std::string result;
_colorize (result, input);
@@ -428,7 +425,7 @@ std::string Color::colorize (const std::string& input)
//
// 256 fg \033[38;5;Nm
// 256 bg \033[48;5;Nm
void Color::_colorize (std::string &result, const std::string& input)
void Color::_colorize (std::string &result, const std::string& input) const
{
if (!nontrivial ())
{

View File

@@ -29,7 +29,6 @@
#include <string>
////////////////////////////////////////////////////////////////////////////////
#define _COLOR_INVERSE 0x00400000 // Inverse attribute.
#define _COLOR_256 0x00200000 // 256-color mode.
#define _COLOR_HASBG 0x00100000 // Has background color (all values taken).
@@ -57,9 +56,9 @@ public:
void upgrade ();
void blend (const Color&);
std::string colorize (const std::string&);
std::string colorize (const std::string&) const;
static std::string colorize (const std::string&, const std::string&);
void _colorize (std::string&, const std::string&);
void _colorize (std::string&, const std::string&) const;
static std::string strip (const std::string&);
bool nontrivial () const;
@@ -74,5 +73,3 @@ private:
};
#endif
////////////////////////////////////////////////////////////////////////////////