color_names.cpp
Go to the documentation of this file.
1 
22 #include "color_names.hpp"
23 #include <QRegularExpression>
24 
25 static QRegularExpression regex_qcolor ("^(?:(?:#[[:xdigit:]]{3})|(?:#[[:xdigit:]]{6})|(?:[[:alpha:]]+))$");
26 static QRegularExpression regex_func_rgb (R"(^rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$)");
27 static QRegularExpression regex_hex_rgba ("^#[[:xdigit:]]{8}$");
28 static QRegularExpression regex_func_rgba (R"(^rgba?\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$)");
29 
30 namespace color_widgets {
31 
32 
33 QString stringFromColor(const QColor& color, bool alpha)
34 {
35  if ( !alpha || color.alpha() == 255 )
36  return color.name();
37  return color.name()+QString("%1").arg(color.alpha(), 2, 16, QChar('0'));
38 }
39 
40 QColor colorFromString(const QString& string, bool alpha)
41 {
42  QString xs = string.trimmed();
43  QRegularExpressionMatch match;
44 
45  match = regex_qcolor.match(xs);
46  if ( match.hasMatch() )
47  {
48  return QColor(xs);
49  }
50 
51  match = regex_func_rgb.match(xs);
52  if ( match.hasMatch() )
53  {
54  return QColor(
55  match.captured(1).toInt(),
56  match.captured(2).toInt(),
57  match.captured(3).toInt()
58  );
59  }
60 
61  if ( alpha )
62  {
63  match = regex_hex_rgba.match(xs);
64  if ( match.hasMatch() )
65  {
66  return QColor(
67  xs.mid(1,2).toInt(nullptr,16),
68  xs.mid(3,2).toInt(nullptr,16),
69  xs.mid(5,2).toInt(nullptr,16),
70  xs.mid(7,2).toInt(nullptr,16)
71  );
72  }
73 
74  match = regex_func_rgba.match(xs);
75  if ( match.hasMatch() )
76  {
77  return QColor(
78  match.captured(1).toInt(),
79  match.captured(2).toInt(),
80  match.captured(3).toInt(),
81  match.captured(4).toInt()
82  );
83  }
84  }
85 
86  return QColor();
87 }
88 
89 } // namespace color_widgets
static QRegularExpression regex_func_rgba(R"(^rgba?\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$)")
static QRegularExpression regex_hex_rgba("^#[[:xdigit:]]{8}$")
static QRegularExpression regex_func_rgb(R"(^rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$)")
QColor colorFromString(const QString &string, bool alpha=true)
Convert a string into a color.
Definition: color_names.cpp:40
static QRegularExpression regex_qcolor("^(?:(?:#[[:xdigit:]]{3})|(?:#[[:xdigit:]]{6})|(?:[[:alpha:]]+))$")
QString stringFromColor(const QColor &color, bool alpha=true)
Convert a color into a string.
Definition: color_names.cpp:33


plotjuggler
Author(s): Davide Faconti
autogenerated on Sat Jul 6 2019 03:44:17