From ab26593cddf2fc0004da8048e28216792ed433ef Mon Sep 17 00:00:00 2001 From: fluxgen Date: Sun, 12 Feb 2006 08:05:34 +0000 Subject: added static validColorString function, returns true if the color string is in valid color format --- src/FbTk/Color.cc | 17 +++++++++++++---- src/FbTk/Color.hh | 5 ++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/FbTk/Color.cc b/src/FbTk/Color.cc index 7d050d3..dd80bd3 100644 --- a/src/FbTk/Color.cc +++ b/src/FbTk/Color.cc @@ -31,11 +31,9 @@ using namespace std; namespace { -unsigned char maxValue(unsigned short colval) { - if (colval == 65535) - return 0xFF; - return static_cast(colval/0xFF); +inline unsigned char maxValue(unsigned short colval) { + return colval == 65535 ? 0xFF : static_cast(colval/0xFF); } }; @@ -106,6 +104,17 @@ bool Color::setFromString(const char *color_string, int screen) { return true; } +bool Color::validColorString(const char *color_string, int screen) { + XColor color; + Display *disp = App::instance()->display(); + Colormap colm = DefaultColormap(disp, screen); + // trim white space + string color_string_tmp = color_string; + StringUtil::removeFirstWhitespace(color_string_tmp); + StringUtil::removeTrailingWhitespace(color_string_tmp); + + return XParseColor(disp, colm, color_string_tmp.c_str(), &color) != 0; +} Color &Color::operator = (const Color &col_copy) { // check for aliasing diff --git a/src/FbTk/Color.hh b/src/FbTk/Color.hh index b17dd95..7536d89 100644 --- a/src/FbTk/Color.hh +++ b/src/FbTk/Color.hh @@ -54,7 +54,10 @@ public: inline unsigned short green() const { return m_green; } inline unsigned short blue() const { return m_blue; } inline unsigned long pixel() const { return m_pixel; } - + + /// @return true if the color name in color_string is resolved, else false + static bool validColorString(const char *color_string, int screen); + private: void free(); void copy(const Color &col); -- cgit v0.11.2