2013-12-06 00:40:26 -06:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for EffectBase class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-12-06 00:40:26 -06:00
|
|
|
*
|
2019-06-11 06:48:32 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
2014-03-29 18:49:22 -05:00
|
|
|
* <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/>.
|
2013-12-06 00:40:26 -06:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-12-06 00:40:26 -06:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* 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
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-12-06 00:40:26 -06:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
2013-12-06 00:40:26 -06:00
|
|
|
*/
|
|
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "Color.h"
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Constructor which takes a HEX color code
|
2019-08-04 22:48:57 -04:00
|
|
|
Color::Color(std::string color_hex)
|
2015-06-01 00:20:14 -07:00
|
|
|
{
|
|
|
|
|
// 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());
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-17 00:21:57 -06:00
|
|
|
// Get the HEX value of a color at a specific frame
|
2019-08-04 22:48:57 -04:00
|
|
|
std::string Color::GetColorHex(int64_t frame_number) {
|
2015-02-17 00:21:57 -06:00
|
|
|
|
|
|
|
|
int r = red.GetInt(frame_number);
|
|
|
|
|
int g = green.GetInt(frame_number);
|
|
|
|
|
int b = blue.GetInt(frame_number);
|
2015-06-01 00:20:14 -07:00
|
|
|
int a = alpha.GetInt(frame_number);
|
2015-02-17 00:21:57 -06:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
return QColor( r,g,b,a ).name().toStdString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the distance between 2 RGB pairs (alpha is ignored)
|
|
|
|
|
long Color::GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
|
|
|
|
|
{
|
|
|
|
|
long rmean = ( R1 + R2 ) / 2;
|
|
|
|
|
long r = R1 - R2;
|
|
|
|
|
long g = G1 - G2;
|
|
|
|
|
long b = B1 - B2;
|
|
|
|
|
return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
|
2015-02-17 00:21:57 -06:00
|
|
|
}
|
|
|
|
|
|
2013-12-06 00:40:26 -06:00
|
|
|
// Generate JSON string of this object
|
2019-12-27 08:51:51 -05:00
|
|
|
std::string Color::Json() const {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Return formatted string
|
|
|
|
|
return JsonValue().toStyledString();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Generate Json::Value for this object
|
|
|
|
|
Json::Value Color::JsonValue() const {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Create root json object
|
|
|
|
|
Json::Value root;
|
|
|
|
|
root["red"] = red.JsonValue();
|
|
|
|
|
root["green"] = green.JsonValue();
|
|
|
|
|
root["blue"] = blue.JsonValue();
|
2015-06-01 00:20:14 -07:00
|
|
|
root["alpha"] = alpha.JsonValue();
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// return JsonValue
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load JSON string into this object
|
2019-12-27 08:51:51 -05:00
|
|
|
void Color::SetJson(const std::string value) {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Parse JSON string into JSON objects
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-27 08:51:51 -05:00
|
|
|
const Json::Value root = openshot::stringToJson(value);
|
2013-12-06 00:40:26 -06:00
|
|
|
// Set all values that match
|
2013-12-07 21:09:55 -06:00
|
|
|
SetJsonValue(root);
|
2013-12-06 00:40:26 -06:00
|
|
|
}
|
2019-07-03 12:58:02 -04:00
|
|
|
catch (const std::exception& e)
|
2013-12-06 00:40:26 -06:00
|
|
|
{
|
|
|
|
|
// Error parsing JSON (or missing keys)
|
2019-08-27 15:47:39 -04:00
|
|
|
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
|
2013-12-06 00:40:26 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Load Json::Value into this object
|
|
|
|
|
void Color::SetJsonValue(const Json::Value root) {
|
2013-12-06 00:40:26 -06:00
|
|
|
|
|
|
|
|
// Set data from Json (if key is found)
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["red"].isNull())
|
2013-12-07 21:09:55 -06:00
|
|
|
red.SetJsonValue(root["red"]);
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["green"].isNull())
|
2013-12-07 21:09:55 -06:00
|
|
|
green.SetJsonValue(root["green"]);
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["blue"].isNull())
|
2013-12-07 21:09:55 -06:00
|
|
|
blue.SetJsonValue(root["blue"]);
|
2015-06-01 00:20:14 -07:00
|
|
|
if (!root["alpha"].isNull())
|
2015-10-01 18:51:59 -05:00
|
|
|
alpha.SetJsonValue(root["alpha"]);
|
2013-12-06 00:40:26 -06:00
|
|
|
}
|