1 /**
2 * @file
3 * @brief Source file for EffectBase class
4 * @author Jonathan Thomas <jonathan@openshot.org>
5 *
6 * @ref License
7 */
8
9 /* LICENSE
10 *
11 * Copyright (c) 2008-2019 OpenShot Studios, LLC
12 * <http://www.openshotstudios.com/>. This file is part of
13 * OpenShot Library (libopenshot), an open-source project dedicated to
14 * delivering high quality video editing and animation solutions to the
15 * world. For more information visit <http://www.openshot.org/>.
16 *
17 * OpenShot Library (libopenshot) is free software: you can redistribute it
18 * and/or modify it under the terms of the GNU Lesser General Public License
19 * as published by the Free Software Foundation, either version 3 of the
20 * License, or (at your option) any later version.
21 *
22 * OpenShot Library (libopenshot) is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public License
28 * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31 #include "Color.h"
32 #include "Exceptions.h"
33
34 using namespace openshot;
35
36 // Constructor which takes R,G,B,A
Color(unsigned char Red,unsigned char Green,unsigned char Blue,unsigned char Alpha)37 Color::Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha)
38 {
39 // Set initial points
40 red.AddPoint(1, (float)Red);
41 green.AddPoint(1, (float)Green);
42 blue.AddPoint(1, (float)Blue);
43 alpha.AddPoint(1, (float)Alpha);
44 }
45
46 // Constructor which takes 4 existing Keyframe curves
Color(Keyframe Red,Keyframe Green,Keyframe Blue,Keyframe Alpha)47 Color::Color(Keyframe Red, Keyframe Green, Keyframe Blue, Keyframe Alpha)
48 {
49 // Assign existing keyframes
50 red = Red;
51 green = Green;
52 blue = Blue;
53 alpha = Alpha;
54 }
55
56 // Constructor which takes a HEX color code
Color(std::string color_hex)57 Color::Color(std::string color_hex)
58 {
59 // Create a QColor from hex
60 QColor color(QString::fromStdString(color_hex));
61 red.AddPoint(1, color.red());
62 green.AddPoint(1, color.green());
63 blue.AddPoint(1, color.blue());
64 alpha.AddPoint(1, color.alpha());
65 }
66
67 // Get the HEX value of a color at a specific frame
GetColorHex(int64_t frame_number)68 std::string Color::GetColorHex(int64_t frame_number) {
69
70 int r = red.GetInt(frame_number);
71 int g = green.GetInt(frame_number);
72 int b = blue.GetInt(frame_number);
73 int a = alpha.GetInt(frame_number);
74
75 return QColor( r,g,b,a ).name().toStdString();
76 }
77
78 // Get the RGBA values of a color at a specific frame
GetColorRGBA(int64_t frame_number)79 std::vector<int> Color::GetColorRGBA(int64_t frame_number) {
80 std::vector<int> rgba;
81 rgba.push_back(red.GetInt(frame_number));
82 rgba.push_back(green.GetInt(frame_number));
83 rgba.push_back(blue.GetInt(frame_number));
84 rgba.push_back(alpha.GetInt(frame_number));
85
86 return rgba;
87 }
88
89 // Get the distance between 2 RGB pairs (alpha is ignored)
GetDistance(long R1,long G1,long B1,long R2,long G2,long B2)90 long Color::GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
91 {
92 long rmean = ( R1 + R2 ) / 2;
93 long r = R1 - R2;
94 long g = G1 - G2;
95 long b = B1 - B2;
96 return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
97 }
98
99 // Generate JSON string of this object
Json() const100 std::string Color::Json() const {
101
102 // Return formatted string
103 return JsonValue().toStyledString();
104 }
105
106 // Generate Json::Value for this object
JsonValue() const107 Json::Value Color::JsonValue() const {
108
109 // Create root json object
110 Json::Value root;
111 root["red"] = red.JsonValue();
112 root["green"] = green.JsonValue();
113 root["blue"] = blue.JsonValue();
114 root["alpha"] = alpha.JsonValue();
115
116 // return JsonValue
117 return root;
118 }
119
120 // Load JSON string into this object
SetJson(const std::string value)121 void Color::SetJson(const std::string value) {
122
123 // Parse JSON string into JSON objects
124 try
125 {
126 const Json::Value root = openshot::stringToJson(value);
127 // Set all values that match
128 SetJsonValue(root);
129 }
130 catch (const std::exception& e)
131 {
132 // Error parsing JSON (or missing keys)
133 throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
134 }
135 }
136
137 // Load Json::Value into this object
SetJsonValue(const Json::Value root)138 void Color::SetJsonValue(const Json::Value root) {
139
140 // Set data from Json (if key is found)
141 if (!root["red"].isNull())
142 red.SetJsonValue(root["red"]);
143 if (!root["green"].isNull())
144 green.SetJsonValue(root["green"]);
145 if (!root["blue"].isNull())
146 blue.SetJsonValue(root["blue"]);
147 if (!root["alpha"].isNull())
148 alpha.SetJsonValue(root["alpha"]);
149 }
150