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
|
2021-08-09 09:53:12 -04:00
|
|
|
Color::Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha) :
|
|
|
|
|
red(static_cast<double>(Red)),
|
|
|
|
|
green(static_cast<double>(Green)),
|
|
|
|
|
blue(static_cast<double>(Blue)),
|
|
|
|
|
alpha(static_cast<double>(Alpha)) { }
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// Constructor which takes 4 existing Keyframe curves
|
2021-08-09 09:53:12 -04:00
|
|
|
Color::Color(Keyframe Red, Keyframe Green, Keyframe Blue, Keyframe Alpha) :
|
|
|
|
|
red(Red), green(Green), blue(Blue), alpha(Alpha) { }
|
|
|
|
|
|
|
|
|
|
// Constructor which takes a QColor
|
|
|
|
|
Color::Color(QColor qcolor) :
|
|
|
|
|
red(qcolor.red()),
|
|
|
|
|
green(qcolor.green()),
|
|
|
|
|
blue(qcolor.blue()),
|
|
|
|
|
alpha(qcolor.alpha()) { }
|
|
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// Constructor which takes a HEX color code
|
2019-08-04 22:48:57 -04:00
|
|
|
Color::Color(std::string color_hex)
|
2021-08-09 09:53:12 -04:00
|
|
|
: Color::Color(QString::fromStdString(color_hex)) {}
|
|
|
|
|
|
|
|
|
|
Color::Color(const char* color_hex)
|
|
|
|
|
: Color::Color(QString(color_hex)) {}
|
2015-06-01 00:20:14 -07:00
|
|
|
|
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-08-09 09:53:12 -04:00
|
|
|
// Get RGBA values for a specific frame as an integer vector
|
2021-03-27 22:50:01 -03:00
|
|
|
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
|
|
|
}
|