OpenShot Library | libopenshot  0.4.0
Color.cpp
Go to the documentation of this file.
1 
9 // Copyright (c) 2008-2019 OpenShot Studios, LLC
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #include <cmath>
14 
15 #include "Color.h"
16 #include "Exceptions.h"
17 
18 using namespace openshot;
19 
20 // Constructor which takes R,G,B,A
21 Color::Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha) :
22  red(static_cast<double>(Red)),
23  green(static_cast<double>(Green)),
24  blue(static_cast<double>(Blue)),
25  alpha(static_cast<double>(Alpha)) { }
26 
27 // Constructor which takes 4 existing Keyframe curves
28 Color::Color(Keyframe Red, Keyframe Green, Keyframe Blue, Keyframe Alpha) :
29  red(Red), green(Green), blue(Blue), alpha(Alpha) { }
30 
31 // Constructor which takes a QColor
32 Color::Color(QColor qcolor) :
33  red(qcolor.red()),
34  green(qcolor.green()),
35  blue(qcolor.blue()),
36  alpha(qcolor.alpha()) { }
37 
38 
39 // Constructor which takes a HEX color code
40 Color::Color(std::string color_hex)
41  : Color::Color(QString::fromStdString(color_hex)) {}
42 
43 Color::Color(const char* color_hex)
44  : Color::Color(QString(color_hex)) {}
45 
46 // Get the HEX value of a color at a specific frame
47 std::string Color::GetColorHex(int64_t frame_number) {
48 
49  int r = red.GetInt(frame_number);
50  int g = green.GetInt(frame_number);
51  int b = blue.GetInt(frame_number);
52  int a = alpha.GetInt(frame_number);
53 
54  return QColor( r,g,b,a ).name().toStdString();
55 }
56 
57 // Get RGBA values for a specific frame as an integer vector
58 std::vector<int> Color::GetColorRGBA(int64_t frame_number) {
59  std::vector<int> rgba;
60  rgba.push_back(red.GetInt(frame_number));
61  rgba.push_back(green.GetInt(frame_number));
62  rgba.push_back(blue.GetInt(frame_number));
63  rgba.push_back(alpha.GetInt(frame_number));
64 
65  return rgba;
66 }
67 
68 // Get the distance between 2 RGB pairs (alpha is ignored)
69 long Color::GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
70 {
71  long rmean = ( R1 + R2 ) / 2;
72  long r = R1 - R2;
73  long g = G1 - G2;
74  long b = B1 - B2;
75  return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
76 }
77 
78 // Generate JSON string of this object
79 std::string Color::Json() const {
80 
81  // Return formatted string
82  return JsonValue().toStyledString();
83 }
84 
85 // Generate Json::Value for this object
86 Json::Value Color::JsonValue() const {
87 
88  // Create root json object
89  Json::Value root;
90  root["red"] = red.JsonValue();
91  root["green"] = green.JsonValue();
92  root["blue"] = blue.JsonValue();
93  root["alpha"] = alpha.JsonValue();
94 
95  // return JsonValue
96  return root;
97 }
98 
99 // Load JSON string into this object
100 void Color::SetJson(const std::string value) {
101 
102  // Parse JSON string into JSON objects
103  try
104  {
105  const Json::Value root = openshot::stringToJson(value);
106  // Set all values that match
107  SetJsonValue(root);
108  }
109  catch (const std::exception& e)
110  {
111  // Error parsing JSON (or missing keys)
112  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
113  }
114 }
115 
116 // Load Json::Value into this object
117 void Color::SetJsonValue(const Json::Value root) {
118 
119  // Set data from Json (if key is found)
120  if (!root["red"].isNull())
121  red.SetJsonValue(root["red"]);
122  if (!root["green"].isNull())
123  green.SetJsonValue(root["green"]);
124  if (!root["blue"].isNull())
125  blue.SetJsonValue(root["blue"]);
126  if (!root["alpha"].isNull())
127  alpha.SetJsonValue(root["alpha"]);
128 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::Color::Color
Color()
Default constructor.
Definition: Color.h:36
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:372
openshot::Color::GetDistance
static long GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
Get the distance between 2 RGB pairs. (0=identical colors, 10=very close colors, 760=very different c...
Definition: Color.cpp:69
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:339
openshot::Color
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::Color::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Color.cpp:117
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::Color::green
openshot::Keyframe green
Curve representing the green value (0 - 255)
Definition: Color.h:31
openshot::Color::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Color.cpp:86
openshot::Keyframe::GetInt
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:282
openshot::Color::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Color.cpp:100
openshot::Color::GetColorRGBA
std::vector< int > GetColorRGBA(int64_t frame_number)
Definition: Color.cpp:58
Color.h
Header file for Color class.
openshot::Color::alpha
openshot::Keyframe alpha
Curve representing the alpha value (0 - 255)
Definition: Color.h:33
openshot::Color::red
openshot::Keyframe red
Curve representing the red value (0 - 255)
Definition: Color.h:30
openshot::Color::GetColorHex
std::string GetColorHex(int64_t frame_number)
Get the HEX value of a color at a specific frame.
Definition: Color.cpp:47
openshot::Color::Json
std::string Json() const
Generate JSON string of this object.
Definition: Color.cpp:79
openshot::Color::blue
openshot::Keyframe blue
Curve representing the red value (0 - 255)
Definition: Color.h:32
Exceptions.h
Header file for all Exception classes.