USGS

Isis 3.0 Object Programmers' Reference

Home

Color.cpp
1 #include "Color.h"
2 
3 #include <QColor>
4 #include <QString>
5 
6 #include "IException.h"
7 
8 namespace Isis {
9  QColor Color::fromRGBAString(QString string) {
10  QColor result;
11 
12  if (colorRGBAFormat().exactMatch(string)) {
13  result = QColor(string.mid(1, 2).toInt(NULL, 16), string.mid(3, 2).toInt(NULL, 16),
14  string.mid(5, 2).toInt(NULL, 16), string.mid(7, 2).toInt(NULL, 16));
15  }
16 
17  return result;
18  }
19 
20 
21  QString Color::toRGBAString(QColor color) {
22  QString result;
23 
24  if (color.isValid()) {
25  result = QString("#%1%2%3%4")
26  .arg(color.red(), 2, 16, QChar('0'))
27  .arg(color.green(), 2, 16, QChar('0'))
28  .arg(color.blue(), 2, 16, QChar('0'))
29  .arg(color.alpha(), 2, 16, QChar('0'));
30  }
31  else {
32  throw IException(IException::Unknown,
33  "Can not convert an invalid color to an RGBA string. There is no string representation "
34  "of an invalid color.",
35  _FILEINFO_);
36  }
37 
38  return result;
39  }
40 
41 
42  QRegExp Color::colorRGBAFormat() {
43  return QRegExp("^#[0-9a-fA-F]{8}$");
44  }
45 }