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
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2013-12-06 00:40:26 -06:00
|
|
|
|
2021-04-19 20:57:47 -04:00
|
|
|
#include <cmath>
|
|
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "Color.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 22:50:01 -03:00
|
|
|
// Get the RGBA values of a color at a specific frame
|
|
|
|
|
std::vector<int> Color::GetColorRGBA(int64_t frame_number) {
|
|
|
|
|
std::vector<int> rgba;
|
|
|
|
|
rgba.push_back(red.GetInt(frame_number));
|
|
|
|
|
rgba.push_back(green.GetInt(frame_number));
|
|
|
|
|
rgba.push_back(blue.GetInt(frame_number));
|
|
|
|
|
rgba.push_back(alpha.GetInt(frame_number));
|
|
|
|
|
|
|
|
|
|
return rgba;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// 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
|
|
|
}
|