Color: Add Color(QColor) ctor, with unit test

This commit is contained in:
FeRD (Frank Dana)
2021-08-09 09:53:12 -04:00
parent b9833b71ab
commit b1d98d8de1
4 changed files with 73 additions and 62 deletions

View File

@@ -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<double>(Red)),
green(static_cast<double>(Green)),
blue(static_cast<double>(Blue)),
alpha(static_cast<double>(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<int> Color::GetColorRGBA(int64_t frame_number) {
std::vector<int> rgba;
rgba.push_back(red.GetInt(frame_number));

View File

@@ -14,53 +14,56 @@
#define OPENSHOT_COLOR_H
#include "KeyFrame.h"
#include <QtGui/QColor>
#include <QColor>
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; ///<Curve representing the red value (0 - 255)
openshot::Keyframe green; ///<Curve representing the green value (0 - 255)
openshot::Keyframe blue; ///<Curve representing the red value (0 - 255)
openshot::Keyframe alpha; ///<Curve representing the alpha value (0 - 255)
public:
openshot::Keyframe red; ///<Curve representing the red value (0 - 255)
openshot::Keyframe green; ///<Curve representing the green value (0 - 255)
openshot::Keyframe blue; ///<Curve representing the red value (0 - 255)
openshot::Keyframe alpha; ///<Curve representing the alpha value (0 - 255)
/// Default constructor
Color() {};
/// Default constructor
Color() {};
/// Constructor which takes a HEX color code
Color(std::string color_hex);
/// Constructor which takes a QColor
explicit Color(QColor);
/// Constructor which takes R,G,B,A
Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha);
/// Constructor which takes a hex string ("#rrggbb")
explicit Color(std::string color_hex);
explicit Color(const char* color_hex);
/// Constructor which takes 4 existing Keyframe curves
Color(openshot::Keyframe Red, openshot::Keyframe Green, openshot::Keyframe Blue, openshot::Keyframe Alpha);
/// Constructor which takes R,G,B,A
Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha);
/// Get the HEX value of a color at a specific frame
std::string GetColorHex(int64_t frame_number);
/// Constructor which takes 4 existing Keyframe curves
Color(openshot::Keyframe Red, openshot::Keyframe Green, openshot::Keyframe Blue, openshot::Keyframe Alpha);
// Get the RGBA values of a color at a specific frame
std::vector<int> 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<int> 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

View File

@@ -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

View File

@@ -15,6 +15,8 @@
#include <catch2/catch.hpp>
#include <QColor>
#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");