From c11808878fe2903be9d324543fd0330f5279aff8 Mon Sep 17 00:00:00 2001 From: johan Date: Fri, 13 Dec 2013 00:01:48 +0100 Subject: [PATCH] Hyperion-remote changed to support multiple colors in the setColor command Former-commit-id: f0acda054056d6fa937e9cbd2b22686979911bc9 --- src/hyperion-remote/CustomParameter.h | 40 +++++++++++++++++-------- src/hyperion-remote/JsonConnection.cpp | 13 ++++---- src/hyperion-remote/JsonConnection.h | 2 +- src/hyperion-remote/hyperion-remote.cpp | 2 +- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/src/hyperion-remote/CustomParameter.h b/src/hyperion-remote/CustomParameter.h index c1ff96d4..91c3a614 100644 --- a/src/hyperion-remote/CustomParameter.h +++ b/src/hyperion-remote/CustomParameter.h @@ -11,7 +11,7 @@ #include "ColorTransformValues.h" /// Data parameter for a color -typedef vlofgren::PODParameter ColorParameter; +typedef vlofgren::PODParameter> ColorParameter; /// Data parameter for an image typedef vlofgren::PODParameter ImageParameter; @@ -21,40 +21,54 @@ typedef vlofgren::PODParameter TransformParameter; namespace vlofgren { /// - /// Translates a string (as passed on the commandline) to a color + /// Translates a string (as passed on the commandline) to a vector of colors /// /// @param[in] s The string (as passed on the commandline) /// - /// @return The translated color + /// @return The translated colors /// /// @throws Parameter::ParameterRejected If the string did not result in a color /// template<> - QColor ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected) + std::vector ColorParameter::validate(const std::string& s) throw (Parameter::ParameterRejected) { // Check if we can create the color by name QColor color(s.c_str()); if (color.isValid()) { - return color; + return std::vector{color}; } // check if we can create the color by hex RRGGBB value - if (s.length() == 6 && isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]) && isxdigit(s[4]) && isxdigit(s[5])) + if (s.length() >= 6u && (s.length()%6) == 0u && std::count_if(s.begin(), s.end(), isxdigit) == s.length()) { bool ok = true; - int rgb[3]; - for (int i = 0; i < 3 && ok; ++i) + std::vector colors; + + for (size_t j = 0; j < s.length()/6; ++j) { - QString colorComponent(s.substr(2*i, 2).c_str()); - rgb[i] = colorComponent.toInt(&ok, 16); + 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; + } } // check if all components parsed succesfully if (ok) { - color.setRgb(rgb[0], rgb[1], rgb[2]); - return color; + return colors; } } @@ -65,7 +79,7 @@ namespace vlofgren { } throw Parameter::ParameterRejected(errorMessage.str()); - return color; + return std::vector{color}; } template<> diff --git a/src/hyperion-remote/JsonConnection.cpp b/src/hyperion-remote/JsonConnection.cpp index 5014f97c..652a3dfa 100644 --- a/src/hyperion-remote/JsonConnection.cpp +++ b/src/hyperion-remote/JsonConnection.cpp @@ -39,18 +39,21 @@ JsonConnection::~JsonConnection() _socket.close(); } -void JsonConnection::setColor(QColor color, int priority, int duration) +void JsonConnection::setColor(std::vector colors, int priority, int duration) { - std::cout << "Set color to " << color.red() << " " << color.green() << " " << color.blue() << std::endl; + std::cout << "Set color to " << colors[0].red() << " " << colors[0].green() << " " << colors[0].blue() << (colors.size() > 1 ? " + ..." : "") << std::endl; // create command Json::Value command; command["command"] = "color"; command["priority"] = priority; Json::Value & rgbValue = command["color"]; - rgbValue[0] = color.red(); - rgbValue[1] = color.green(); - rgbValue[2] = color.blue(); + for (const QColor & color : colors) + { + rgbValue.append(color.red()); + rgbValue.append(color.green()); + rgbValue.append(color.blue()); + } if (duration > 0) { command["duration"] = duration; diff --git a/src/hyperion-remote/JsonConnection.h b/src/hyperion-remote/JsonConnection.h index cf9ce415..472c5565 100644 --- a/src/hyperion-remote/JsonConnection.h +++ b/src/hyperion-remote/JsonConnection.h @@ -41,7 +41,7 @@ public: /// @param priority The priority /// @param duration The duration in milliseconds /// - void setColor(QColor color, int priority, int duration); + void setColor(std::vector color, int priority, int duration); /// /// Set the leds according to the given image (assume the image is stretched to the display size) diff --git a/src/hyperion-remote/hyperion-remote.cpp b/src/hyperion-remote/hyperion-remote.cpp index 3bf01942..f7dfffe6 100644 --- a/src/hyperion-remote/hyperion-remote.cpp +++ b/src/hyperion-remote/hyperion-remote.cpp @@ -40,7 +40,7 @@ int main(int argc, char * argv[]) StringParameter & argAddress = parameters.add ('a', "address" , QString("Set the address of the hyperion server [default: %1]").arg(defaultServerAddress).toAscii().constData()); IntParameter & argPriority = parameters.add ('p', "priority" , QString("Use to the provided priority channel (the lower the number, the higher the priority) [default: %1]").arg(defaultPriority).toAscii().constData()); IntParameter & argDuration = parameters.add ('d', "duration" , "Specify how long the leds should be switched on in millseconds [default: infinity]"); - ColorParameter & argColor = parameters.add ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name)"); + ColorParameter & argColor = parameters.add ('c', "color" , "Set all leds to a constant color (either RRGGBB hex value or a color name. The color may be repeated multiple time like: RRGGBBRRGGBB)"); ImageParameter & argImage = parameters.add ('i', "image" , "Set the leds to the colors according to the given image file"); StringParameter & argEffect = parameters.add ('e', "effect" , "Enable the effect with the given name"); StringParameter & argEffectArgs = parameters.add (0x0, "effectArgs", "Arguments to use in combination with the specified effect. Should be a Json object string.");