From b1d98d8de10baf063fc52283c2a28e86e496b07b Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Mon, 9 Aug 2021 09:53:12 -0400 Subject: [PATCH] Color: Add Color(QColor) ctor, with unit test --- src/Color.cpp | 45 +++++++++++++---------------- src/Color.h | 73 +++++++++++++++++++++++++----------------------- src/KeyFrame.cpp | 2 +- tests/Color.cpp | 15 +++++++++- 4 files changed, 73 insertions(+), 62 deletions(-) diff --git a/src/Color.cpp b/src/Color.cpp index 4b0f227b..7cc15f38 100644 --- a/src/Color.cpp +++ b/src/Color.cpp @@ -18,35 +18,30 @@ using namespace openshot; // Constructor which takes R,G,B,A -Color::Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha) -{ - // Set initial points - red.AddPoint(1, (float)Red); - green.AddPoint(1, (float)Green); - blue.AddPoint(1, (float)Blue); - alpha.AddPoint(1, (float)Alpha); -} +Color::Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha) : + red(static_cast(Red)), + green(static_cast(Green)), + blue(static_cast(Blue)), + alpha(static_cast(Alpha)) { } // Constructor which takes 4 existing Keyframe curves -Color::Color(Keyframe Red, Keyframe Green, Keyframe Blue, Keyframe Alpha) -{ - // Assign existing keyframes - red = Red; - green = Green; - blue = Blue; - alpha = Alpha; -} +Color::Color(Keyframe Red, Keyframe Green, Keyframe Blue, Keyframe Alpha) : + red(Red), green(Green), blue(Blue), alpha(Alpha) { } + +// Constructor which takes a QColor +Color::Color(QColor qcolor) : + red(qcolor.red()), + green(qcolor.green()), + blue(qcolor.blue()), + alpha(qcolor.alpha()) { } + // Constructor which takes a HEX color code Color::Color(std::string color_hex) -{ - // Create a QColor from hex - QColor color(QString::fromStdString(color_hex)); - red.AddPoint(1, color.red()); - green.AddPoint(1, color.green()); - blue.AddPoint(1, color.blue()); - alpha.AddPoint(1, color.alpha()); -} + : Color::Color(QString::fromStdString(color_hex)) {} + +Color::Color(const char* color_hex) + : Color::Color(QString(color_hex)) {} // Get the HEX value of a color at a specific frame std::string Color::GetColorHex(int64_t frame_number) { @@ -59,7 +54,7 @@ std::string Color::GetColorHex(int64_t frame_number) { return QColor( r,g,b,a ).name().toStdString(); } -// Get the RGBA values of a color at a specific frame +// Get RGBA values for a specific frame as an integer vector std::vector Color::GetColorRGBA(int64_t frame_number) { std::vector rgba; rgba.push_back(red.GetInt(frame_number)); diff --git a/src/Color.h b/src/Color.h index 57806138..4337db0e 100644 --- a/src/Color.h +++ b/src/Color.h @@ -14,53 +14,56 @@ #define OPENSHOT_COLOR_H #include "KeyFrame.h" -#include +#include namespace openshot { - /** - * @brief This class represents a color (used on the timeline and clips) - * - * Colors are represented by 4 curves, representing red, green, blue, and alpha. The curves - * can be used to animate colors over time. - */ - class Color{ +/** + * @brief This class represents a color (used on the timeline and clips) + * + * Colors are represented by 4 curves, representing red, green, blue, and alpha. The curves + * can be used to animate colors over time. + */ +class Color{ - public: - openshot::Keyframe red; /// GetColorRGBA(int64_t frame_number); - - /// Get the distance between 2 RGB pairs. (0=identical colors, 10=very close colors, 760=very different colors) - static long GetDistance(long R1, long G1, long B1, long R2, long G2, long B2); + /// Get the HEX value of a color at a specific frame + std::string GetColorHex(int64_t frame_number); - // Get and Set JSON methods - std::string Json() const; ///< Generate JSON string of this object - Json::Value JsonValue() const; ///< Generate Json::Value for this object - void SetJson(const std::string value); ///< Load JSON string into this object - void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object - }; + // Get the RGBA values of a color at a specific frame + std::vector GetColorRGBA(int64_t frame_number); + /// Get the distance between 2 RGB pairs. (0=identical colors, 10=very close colors, 760=very different colors) + static long GetDistance(long R1, long G1, long B1, long R2, long G2, long B2); -} + // Get and Set JSON methods + std::string Json() const; ///< Generate JSON string of this object + Json::Value JsonValue() const; ///< Generate Json::Value for this object + void SetJson(const std::string value); ///< Load JSON string into this object + void SetJsonValue(const Json::Value root); ///< Load Json::Value into this object +}; + +} // namespace openshot #endif diff --git a/src/KeyFrame.cpp b/src/KeyFrame.cpp index 8e278c00..1f42988a 100644 --- a/src/KeyFrame.cpp +++ b/src/KeyFrame.cpp @@ -114,7 +114,7 @@ int64_t SearchBetweenPoints(Point const & left, Point const & right, int64_t con // Constructor which sets the default point & coordinate at X=1 Keyframe::Keyframe(double value) { // Add initial point - AddPoint(Point(value)); + AddPoint(Point(1, value)); } // Constructor which takes a vector of Points diff --git a/tests/Color.cpp b/tests/Color.cpp index d8f8c847..5dc16d4f 100644 --- a/tests/Color.cpp +++ b/tests/Color.cpp @@ -15,6 +15,8 @@ #include +#include + #include "Color.h" #include "Exceptions.h" #include "KeyFrame.h" @@ -83,7 +85,18 @@ TEST_CASE( "HEX_Value", "[libopenshot][color]" ) } -TEST_CASE( "HEX_Constructor", "[libopenshot][color]" ) +TEST_CASE( "QColor ctor", "[libopenshot][color]" ) +{ + QColor qc(Qt::red); + openshot::Color c(qc); + + CHECK(c.red.GetLong(1) == Approx(255.0).margin(0.0001)); + CHECK(c.green.GetLong(1) == Approx(0.0).margin(0.0001)); + CHECK(c.blue.GetLong(1) == Approx(0.0).margin(0.0001)); + CHECK(c.alpha.GetLong(1) == Approx(255.0).margin(0.0001)); +} + +TEST_CASE( "std::string construction", "[libopenshot][color]" ) { // Color openshot::Color c("#4586db");