color_names.cpp
Go to the documentation of this file.
00001 
00022 #include "color_names.hpp"
00023 #include <QRegularExpression>
00024 
00025 static QRegularExpression regex_qcolor ("^(?:(?:#[[:xdigit:]]{3})|(?:#[[:xdigit:]]{6})|(?:[[:alpha:]]+))$");
00026 static QRegularExpression regex_func_rgb (R"(^rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$)");
00027 static QRegularExpression regex_hex_rgba ("^#[[:xdigit:]]{8}$");
00028 static QRegularExpression regex_func_rgba (R"(^rgba?\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$)");
00029 
00030 namespace color_widgets {
00031 
00032 
00033 QString stringFromColor(const QColor& color, bool alpha)
00034 {
00035     if ( !alpha || color.alpha() == 255 )
00036         return color.name();
00037     return color.name()+QString("%1").arg(color.alpha(), 2, 16, QChar('0'));
00038 }
00039 
00040 QColor colorFromString(const QString& string, bool alpha)
00041 {
00042     QString xs = string.trimmed();
00043     QRegularExpressionMatch match;
00044 
00045     match = regex_qcolor.match(xs);
00046     if ( match.hasMatch() )
00047     {
00048         return QColor(xs);
00049     }
00050 
00051     match = regex_func_rgb.match(xs);
00052     if ( match.hasMatch() )
00053     {
00054         return QColor(
00055             match.captured(1).toInt(),
00056             match.captured(2).toInt(),
00057             match.captured(3).toInt()
00058         );
00059     }
00060 
00061     if ( alpha )
00062     {
00063         match = regex_hex_rgba.match(xs);
00064         if ( match.hasMatch() )
00065         {
00066             return QColor(
00067                 xs.mid(1,2).toInt(nullptr,16),
00068                 xs.mid(3,2).toInt(nullptr,16),
00069                 xs.mid(5,2).toInt(nullptr,16),
00070                 xs.mid(7,2).toInt(nullptr,16)
00071             );
00072         }
00073 
00074         match = regex_func_rgba.match(xs);
00075         if ( match.hasMatch() )
00076         {
00077             return QColor(
00078                 match.captured(1).toInt(),
00079                 match.captured(2).toInt(),
00080                 match.captured(3).toInt(),
00081                 match.captured(4).toInt()
00082             );
00083         }
00084     }
00085 
00086     return QColor();
00087 }
00088 
00089 } // namespace color_widgets


plotjuggler
Author(s): Davide Faconti
autogenerated on Fri Sep 1 2017 02:41:55