2013-08-11 21:49:11 +02:00
|
|
|
#pragma once
|
|
|
|
|
2014-02-07 15:28:14 +01:00
|
|
|
// STL includes
|
|
|
|
#include <algorithm>
|
|
|
|
|
2013-08-13 20:10:19 +02:00
|
|
|
// Qt includes
|
2013-08-11 21:49:11 +02:00
|
|
|
#include <QColor>
|
|
|
|
#include <QImage>
|
|
|
|
|
2013-08-13 20:10:19 +02:00
|
|
|
// getoptPlusPLus includes
|
2013-08-11 21:49:11 +02:00
|
|
|
#include <getoptPlusPlus/getoptpp.h>
|
|
|
|
|
2013-08-13 20:10:19 +02:00
|
|
|
// hyperion-remote includes
|
2013-08-13 19:45:17 +02:00
|
|
|
#include "ColorTransformValues.h"
|
2013-08-11 21:49:11 +02:00
|
|
|
|
2013-08-31 14:36:54 +02:00
|
|
|
/// Data parameter for a color
|
2013-12-13 00:01:48 +01:00
|
|
|
typedef vlofgren::PODParameter<std::vector<QColor>> ColorParameter;
|
2013-08-31 14:36:54 +02:00
|
|
|
|
|
|
|
/// Data parameter for an image
|
2013-08-11 21:49:11 +02:00
|
|
|
typedef vlofgren::PODParameter<QImage> ImageParameter;
|
|
|
|
|
2013-08-31 14:36:54 +02:00
|
|
|
/// Data parameter for color transform values (list of three values)
|
|
|
|
typedef vlofgren::PODParameter<ColorTransformValues> TransformParameter;
|
2013-08-13 20:10:19 +02:00
|
|
|
|
2013-08-11 21:49:11 +02:00
|
|
|
namespace vlofgren {
|
2013-09-09 22:35:28 +02:00
|
|
|
///
|
2013-12-13 00:01:48 +01:00
|
|
|
/// Translates a string (as passed on the commandline) to a vector of colors
|
2013-09-09 22:35:28 +02:00
|
|
|
///
|
|
|
|
/// @param[in] s The string (as passed on the commandline)
|
|
|
|
///
|
2013-12-13 00:01:48 +01:00
|
|
|
/// @return The translated colors
|
2013-09-09 22:35:28 +02:00
|
|
|
///
|
|
|
|
/// @throws Parameter::ParameterRejected If the string did not result in a color
|
|
|
|
///
|
2013-08-13 20:10:19 +02:00
|
|
|
template<>
|
2013-12-13 00:01:48 +01:00
|
|
|
std::vector<QColor> ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
|
2013-08-13 20:10:19 +02:00
|
|
|
{
|
|
|
|
// Check if we can create the color by name
|
|
|
|
QColor color(s.c_str());
|
|
|
|
if (color.isValid())
|
|
|
|
{
|
2013-12-13 00:01:48 +01:00
|
|
|
return std::vector<QColor>{color};
|
2013-08-13 20:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if we can create the color by hex RRGGBB value
|
2013-12-15 21:15:34 +01:00
|
|
|
if (s.length() >= 6u && (s.length()%6u) == 0u && std::count_if(s.begin(), s.end(), isxdigit) == int(s.length()))
|
2013-08-13 20:10:19 +02:00
|
|
|
{
|
|
|
|
bool ok = true;
|
2013-12-13 00:01:48 +01:00
|
|
|
std::vector<QColor> colors;
|
|
|
|
|
|
|
|
for (size_t j = 0; j < s.length()/6; ++j)
|
2013-08-13 20:10:19 +02:00
|
|
|
{
|
2013-12-13 00:01:48 +01:00
|
|
|
int rgb[3];
|
|
|
|
for (int i = 0; i < 3 && ok; ++i)
|
|
|
|
{
|
|
|
|
QString colorComponent(s.substr(6*j+2*i, 2).c_str());
|
|
|
|
rgb[i] = colorComponent.toInt(&ok, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
color.setRgb(rgb[0], rgb[1], rgb[2]);
|
|
|
|
colors.push_back(color);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-08-13 20:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if all components parsed succesfully
|
|
|
|
if (ok)
|
|
|
|
{
|
2013-12-13 00:01:48 +01:00
|
|
|
return colors;
|
2013-08-13 20:10:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream errorMessage;
|
|
|
|
errorMessage << "Invalid color. A color is specified by a six lettered RRGGBB hex value or one of the following names:";
|
|
|
|
foreach (const QString & colorname, QColor::colorNames()) {
|
|
|
|
errorMessage << "\n " << colorname.toStdString();
|
|
|
|
}
|
|
|
|
throw Parameter::ParameterRejected(errorMessage.str());
|
|
|
|
|
2013-12-13 00:01:48 +01:00
|
|
|
return std::vector<QColor>{color};
|
2013-08-13 20:10:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
QImage ImageParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
|
|
|
|
{
|
|
|
|
QImage image(s.c_str());
|
|
|
|
|
|
|
|
if (image.isNull())
|
|
|
|
{
|
|
|
|
std::stringstream errorMessage;
|
|
|
|
errorMessage << "File " << s << " could not be opened as an image";
|
|
|
|
throw Parameter::ParameterRejected(errorMessage.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
ColorTransformValues TransformParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
|
|
|
|
{
|
|
|
|
ColorTransformValues transform;
|
|
|
|
|
|
|
|
// s should be split in 3 parts
|
|
|
|
// seperators are either a ',' or a space
|
|
|
|
QStringList components = QString(s.c_str()).split(" ", QString::SkipEmptyParts);
|
|
|
|
|
|
|
|
if (components.size() == 3)
|
|
|
|
{
|
|
|
|
bool ok1, ok2, ok3;
|
|
|
|
transform.valueRed = components[0].toDouble(&ok1);
|
|
|
|
transform.valueGreen = components[1].toDouble(&ok2);
|
|
|
|
transform.valueBlue = components[2].toDouble(&ok3);
|
|
|
|
|
|
|
|
if (ok1 && ok2 && ok3)
|
|
|
|
{
|
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stringstream errorMessage;
|
|
|
|
errorMessage << "Argument " << s << " can not be parsed to 3 double values";
|
|
|
|
throw Parameter::ParameterRejected(errorMessage.str());
|
|
|
|
|
|
|
|
return transform;
|
|
|
|
}
|
2013-08-11 21:49:11 +02:00
|
|
|
}
|