File: Color.cpp

package info (click to toggle)
libopenshot 0.2.7%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,720 kB
  • sloc: cpp: 25,009; python: 93; makefile: 18; ruby: 5; sh: 2
file content (189 lines) | stat: -rw-r--r-- 5,581 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * @file
 * @brief Unit tests for openshot::Color
 * @author Jonathan Thomas <jonathan@openshot.org>
 *
 * @ref License
 */

/* LICENSE
 *
 * Copyright (c) 2008-2019 OpenShot Studios, LLC
 * <http://www.openshotstudios.com/>. This file is part of
 * OpenShot Library (libopenshot), an open-source project dedicated to
 * delivering high quality video editing and animation solutions to the
 * world. For more information visit <http://www.openshot.org/>.
 *
 * OpenShot Library (libopenshot) is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * OpenShot Library (libopenshot) is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
 */

#include <string>
#include <vector>

#include <catch2/catch.hpp>

#include "Color.h"
#include "Exceptions.h"
#include "KeyFrame.h"
#include "Json.h"

TEST_CASE( "default constructor", "[libopenshot][color]" )
{
	// Create an empty color
	openshot::Color c1;

	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));
}

TEST_CASE( "Keyframe constructor", "[libopenshot][color]" )
{
	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]);

	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));
}

TEST_CASE( "Animate_Colors", "[libopenshot][color]" )
{
	// Create an empty color
	openshot::Color c1;

	// Set starting color (on frame 0)
	c1.red.AddPoint(1, 0);
	c1.green.AddPoint(1, 120);
	c1.blue.AddPoint(1, 255);

	// Set ending color (on frame 1000)
	c1.red.AddPoint(1000, 0);
	c1.green.AddPoint(1000, 255);
	c1.blue.AddPoint(1000, 65);

	// Check the color at frame 500
	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));
}

TEST_CASE( "HEX_Value", "[libopenshot][color]" )
{
	// 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);

	CHECK(c.GetColorHex(1) == "#000000");
	CHECK(c.GetColorHex(50) == "#7d7d7d");
	CHECK(c.GetColorHex(100) == "#ffffff");

}

TEST_CASE( "HEX_Constructor", "[libopenshot][color]" )
{
	// Color
	openshot::Color c("#4586db");
	c.red.AddPoint(100, 255);
	c.green.AddPoint(100, 255);
	c.blue.AddPoint(100, 255);

	CHECK(c.GetColorHex(1) == "#4586db");
	CHECK(c.GetColorHex(50) == "#a0c1ed");
	CHECK(c.GetColorHex(100) == "#ffffff");
}

TEST_CASE( "Distance", "[libopenshot][color]" )
{
	// Color
	openshot::Color c1("#040a0c");
	openshot::Color c2("#0c0c04");
	openshot::Color c3("#000000");
	openshot::Color c4("#ffffff");

	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));
}

TEST_CASE( "RGBA_Constructor", "[libopenshot][color]" )
{
	// Color
	openshot::Color c(69, 134, 219, 255);
	c.red.AddPoint(100, 255);
	c.green.AddPoint(100, 255);
	c.blue.AddPoint(100, 255);

	CHECK(c.GetColorHex(1) == "#4586db");
	CHECK(c.GetColorHex(50) == "#a0c1ed");
	CHECK(c.GetColorHex(100) == "#ffffff");

	// Color with alpha
	openshot::Color c1(69, 134, 219, 128);
	CHECK(c1.GetColorHex(1) == "#4586db");
	CHECK(c1.alpha.GetInt(1) == 128);
}

TEST_CASE( "Json", "[libopenshot][color]" )
{
	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();
	CHECK(j1 == j);
	// Check Json::Value representation
	auto jv = c.JsonValue();
	auto jv_string = jv.toStyledString();
	CHECK(j1 == jv_string);
}

TEST_CASE( "SetJson", "[libopenshot][color]" ) {
	const std::string json_input = R"json(
	{
		"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;
	CHECK_THROWS_AS(c.SetJson("}{"), openshot::InvalidJSON);
	c.SetJson(json_input);
	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));
}