2015-02-05 00:12:34 -06:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Unit tests for openshot::Color
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2015-02-05 00:12:34 -06:00
|
|
|
|
2020-12-26 21:51:24 -05:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
#include <catch2/catch.hpp>
|
|
|
|
|
|
2021-08-09 09:53:12 -04:00
|
|
|
#include <QColor>
|
|
|
|
|
|
2020-12-26 21:51:24 -05:00
|
|
|
#include "Color.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2020-12-26 21:51:24 -05:00
|
|
|
#include "KeyFrame.h"
|
|
|
|
|
#include "Json.h"
|
2015-02-05 00:12:34 -06:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "default constructor", "[libopenshot][color]" )
|
2015-02-05 00:12:34 -06:00
|
|
|
{
|
|
|
|
|
// Create an empty color
|
2020-11-04 04:01:34 -05:00
|
|
|
openshot::Color c1;
|
2015-02-05 00:12:34 -06:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c1.red.GetValue(0) == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(c1.green.GetValue(0) == Approx(0.0f).margin(0.00001));
|
|
|
|
|
CHECK(c1.blue.GetValue(0) == Approx(0.0f).margin(0.00001));
|
2015-02-05 00:12:34 -06:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Keyframe constructor", "[libopenshot][color]" )
|
2020-11-04 04:01:34 -05:00
|
|
|
{
|
|
|
|
|
std::vector<openshot::Keyframe> kfs{0, 0, 0, 0};
|
|
|
|
|
int64_t i(0);
|
|
|
|
|
for (auto& kf : kfs) {
|
|
|
|
|
kf.AddPoint(100, ++i * 20);
|
|
|
|
|
}
|
|
|
|
|
auto c = openshot::Color(kfs[0], kfs[1], kfs[2], kfs[3]);
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c.red.GetLong(100) == Approx(20).margin(0.01));
|
|
|
|
|
CHECK(c.green.GetLong(100) == Approx(40).margin(0.01));
|
|
|
|
|
CHECK(c.blue.GetLong(100) == Approx(60).margin(0.01));
|
|
|
|
|
CHECK(c.alpha.GetLong(100) == Approx(80).margin(0.01));
|
2020-11-04 04:01:34 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Animate_Colors", "[libopenshot][color]" )
|
2015-02-05 00:12:34 -06:00
|
|
|
{
|
|
|
|
|
// Create an empty color
|
2020-11-04 04:01:34 -05:00
|
|
|
openshot::Color c1;
|
2015-02-05 00:12:34 -06:00
|
|
|
|
|
|
|
|
// Set starting color (on frame 0)
|
2015-06-01 00:20:14 -07:00
|
|
|
c1.red.AddPoint(1, 0);
|
|
|
|
|
c1.green.AddPoint(1, 120);
|
|
|
|
|
c1.blue.AddPoint(1, 255);
|
2015-02-05 00:12:34 -06:00
|
|
|
|
|
|
|
|
// Set ending color (on frame 1000)
|
2015-06-01 00:20:14 -07:00
|
|
|
c1.red.AddPoint(1000, 0);
|
|
|
|
|
c1.green.AddPoint(1000, 255);
|
|
|
|
|
c1.blue.AddPoint(1000, 65);
|
2015-02-05 00:12:34 -06:00
|
|
|
|
|
|
|
|
// Check the color at frame 500
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c1.red.GetLong(500) == Approx(0).margin(0.01));
|
|
|
|
|
CHECK(c1.green.GetLong(500) == Approx(187).margin(0.01));
|
|
|
|
|
CHECK(c1.blue.GetLong(500) == Approx(160).margin(0.01));
|
2015-02-05 00:12:34 -06:00
|
|
|
}
|
2015-02-17 00:21:57 -06:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "HEX_Value", "[libopenshot][color]" )
|
2015-02-17 00:21:57 -06:00
|
|
|
{
|
|
|
|
|
// Color
|
|
|
|
|
openshot::Color c;
|
|
|
|
|
c.red = openshot::Keyframe(0);
|
|
|
|
|
c.red.AddPoint(100, 255);
|
|
|
|
|
c.green = openshot::Keyframe(0);
|
|
|
|
|
c.green.AddPoint(100, 255);
|
|
|
|
|
c.blue = openshot::Keyframe(0);
|
|
|
|
|
c.blue.AddPoint(100, 255);
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c.GetColorHex(1) == "#000000");
|
|
|
|
|
CHECK(c.GetColorHex(50) == "#7d7d7d");
|
|
|
|
|
CHECK(c.GetColorHex(100) == "#ffffff");
|
2015-02-17 00:21:57 -06:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 09:53:12 -04:00
|
|
|
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]" )
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
// Color
|
|
|
|
|
openshot::Color c("#4586db");
|
|
|
|
|
c.red.AddPoint(100, 255);
|
|
|
|
|
c.green.AddPoint(100, 255);
|
|
|
|
|
c.blue.AddPoint(100, 255);
|
2015-02-17 00:21:57 -06:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c.GetColorHex(1) == "#4586db");
|
|
|
|
|
CHECK(c.GetColorHex(50) == "#a0c1ed");
|
|
|
|
|
CHECK(c.GetColorHex(100) == "#ffffff");
|
2015-06-01 00:20:14 -07:00
|
|
|
}
|
2015-02-17 00:21:57 -06:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Distance", "[libopenshot][color]" )
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
// Color
|
|
|
|
|
openshot::Color c1("#040a0c");
|
|
|
|
|
openshot::Color c2("#0c0c04");
|
|
|
|
|
openshot::Color c3("#000000");
|
|
|
|
|
openshot::Color c4("#ffffff");
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(
|
|
|
|
|
openshot::Color::GetDistance(
|
|
|
|
|
c1.red.GetInt(1), c1.blue.GetInt(1), c1.green.GetInt(1),
|
|
|
|
|
c2.red.GetInt(1), c2.blue.GetInt(1), c2.green.GetInt(1)
|
|
|
|
|
) == Approx(19.0f).margin(0.001));
|
|
|
|
|
CHECK(
|
|
|
|
|
openshot::Color::GetDistance(
|
|
|
|
|
c3.red.GetInt(1), c3.blue.GetInt(1), c3.green.GetInt(1),
|
|
|
|
|
c4.red.GetInt(1), c4.blue.GetInt(1), c4.green.GetInt(1)
|
|
|
|
|
) == Approx(764.0f).margin(0.001));
|
2015-06-01 00:20:14 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "RGBA_Constructor", "[libopenshot][color]" )
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
// Color
|
|
|
|
|
openshot::Color c(69, 134, 219, 255);
|
|
|
|
|
c.red.AddPoint(100, 255);
|
|
|
|
|
c.green.AddPoint(100, 255);
|
|
|
|
|
c.blue.AddPoint(100, 255);
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c.GetColorHex(1) == "#4586db");
|
|
|
|
|
CHECK(c.GetColorHex(50) == "#a0c1ed");
|
|
|
|
|
CHECK(c.GetColorHex(100) == "#ffffff");
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// Color with alpha
|
|
|
|
|
openshot::Color c1(69, 134, 219, 128);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c1.GetColorHex(1) == "#4586db");
|
|
|
|
|
CHECK(c1.alpha.GetInt(1) == 128);
|
2015-06-01 00:20:14 -07:00
|
|
|
}
|
2020-11-04 04:01:34 -05:00
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "Json", "[libopenshot][color]" )
|
2020-11-04 04:01:34 -05:00
|
|
|
{
|
|
|
|
|
openshot::Color c(128, 128, 128, 0);
|
|
|
|
|
openshot::Color c1;
|
|
|
|
|
c1.red.AddPoint(1, 128);
|
|
|
|
|
c1.green.AddPoint(1, 128);
|
|
|
|
|
c1.blue.AddPoint(1, 128);
|
|
|
|
|
c1.alpha.AddPoint(1, 0);
|
|
|
|
|
// Check that JSON produced is identical
|
|
|
|
|
auto j = c.Json();
|
|
|
|
|
auto j1 = c1.Json();
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(j1 == j);
|
2020-11-04 04:01:34 -05:00
|
|
|
// Check Json::Value representation
|
|
|
|
|
auto jv = c.JsonValue();
|
|
|
|
|
auto jv_string = jv.toStyledString();
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(j1 == jv_string);
|
2020-11-04 04:01:34 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-09 04:09:36 -04:00
|
|
|
TEST_CASE( "SetJson", "[libopenshot][color]" ) {
|
2020-11-04 07:08:08 -05:00
|
|
|
const std::string json_input = R"json(
|
2020-11-04 04:01:34 -05:00
|
|
|
{
|
|
|
|
|
"red": { "Points": [ { "co": { "X": 1.0, "Y": 0.0 }, "interpolation": 0 } ] },
|
|
|
|
|
"green": { "Points": [ { "co": { "X": 1.0, "Y": 128.0 }, "interpolation": 0 } ] },
|
|
|
|
|
"blue": { "Points": [ { "co": { "X": 1.0, "Y": 64.0 }, "interpolation": 0 } ] },
|
|
|
|
|
"alpha": { "Points": [ { "co": { "X": 1.0, "Y": 192.0 }, "interpolation": 0 } ] }
|
|
|
|
|
}
|
|
|
|
|
)json";
|
|
|
|
|
openshot::Color c;
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK_THROWS_AS(c.SetJson("}{"), openshot::InvalidJSON);
|
2020-11-04 04:01:34 -05:00
|
|
|
c.SetJson(json_input);
|
2021-04-09 04:09:36 -04:00
|
|
|
CHECK(c.red.GetLong(10) == Approx(0).margin(0.01));
|
|
|
|
|
CHECK(c.green.GetLong(10) == Approx(128).margin(0.01));
|
|
|
|
|
CHECK(c.blue.GetLong(10) == Approx(64).margin(0.01));
|
|
|
|
|
CHECK(c.alpha.GetLong(10) == Approx(192).margin(0.01));
|
2020-11-04 04:01:34 -05:00
|
|
|
}
|