OpenShot Library | libopenshot  0.2.4
Color.cpp
Go to the documentation of this file.
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 "../include/Color.h"
32 
33 using namespace openshot;
34 
35 // Constructor which takes R,G,B,A
36 Color::Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha)
37 {
38  // Set initial points
39  red.AddPoint(1, (float)Red);
40  green.AddPoint(1, (float)Green);
41  blue.AddPoint(1, (float)Blue);
42  alpha.AddPoint(1, (float)Alpha);
43 }
44 
45 // Constructor which takes 4 existing Keyframe curves
47 {
48  // Assign existing keyframes
49  red = Red;
50  green = Green;
51  blue = Blue;
52  alpha = Alpha;
53 }
54 
55 // Constructor which takes a HEX color code
56 Color::Color(std::string color_hex)
57 {
58  // Create a QColor from hex
59  QColor color(QString::fromStdString(color_hex));
60  red.AddPoint(1, color.red());
61  green.AddPoint(1, color.green());
62  blue.AddPoint(1, color.blue());
63  alpha.AddPoint(1, color.alpha());
64 }
65 
66 // Get the HEX value of a color at a specific frame
67 std::string Color::GetColorHex(int64_t frame_number) {
68 
69  int r = red.GetInt(frame_number);
70  int g = green.GetInt(frame_number);
71  int b = blue.GetInt(frame_number);
72  int a = alpha.GetInt(frame_number);
73 
74  return QColor( r,g,b,a ).name().toStdString();
75 }
76 
77 // Get the distance between 2 RGB pairs (alpha is ignored)
78 long Color::GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
79 {
80  long rmean = ( R1 + R2 ) / 2;
81  long r = R1 - R2;
82  long g = G1 - G2;
83  long b = B1 - B2;
84  return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
85 }
86 
87 // Generate JSON string of this object
88 std::string Color::Json() {
89 
90  // Return formatted string
91  return JsonValue().toStyledString();
92 }
93 
94 // Generate Json::JsonValue for this object
95 Json::Value Color::JsonValue() {
96 
97  // Create root json object
98  Json::Value root;
99  root["red"] = red.JsonValue();
100  root["green"] = green.JsonValue();
101  root["blue"] = blue.JsonValue();
102  root["alpha"] = alpha.JsonValue();
103 
104  // return JsonValue
105  return root;
106 }
107 
108 // Load JSON string into this object
109 void Color::SetJson(std::string value) {
110 
111  // Parse JSON string into JSON objects
112  Json::Value root;
113  Json::CharReaderBuilder rbuilder;
114  Json::CharReader* reader(rbuilder.newCharReader());
115 
116  std::string errors;
117  bool success = reader->parse( value.c_str(),
118  value.c_str() + value.size(), &root, &errors );
119  delete reader;
120 
121  if (!success)
122  // Raise exception
123  throw InvalidJSON("JSON could not be parsed (or is invalid)");
124 
125  try
126  {
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::JsonValue into this object
138 void Color::SetJsonValue(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 }
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Color.cpp:95
Color()
Default constructor.
Definition: Color.h:54
void SetJson(std::string value)
Load JSON string into this object.
Definition: Color.cpp:109
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:286
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:374
openshot::Keyframe blue
Curve representing the red value (0 - 255)
Definition: Color.h:50
void AddPoint(Point p)
Add a new point on the key-frame. Each point has a primary coordinate, a left handle, and a right handle.
Definition: KeyFrame.cpp:125
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:78
openshot::Keyframe green
Curve representing the green value (0 - 255)
Definition: Color.h:49
std::string GetColorHex(int64_t frame_number)
Get the HEX value of a color at a specific frame.
Definition: Color.cpp:67
This namespace is the default namespace for all code in the openshot library.
Json::Value JsonValue() const
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:329
Exception for invalid JSON.
Definition: Exceptions.h:205
std::string Json()
Get and Set JSON methods.
Definition: Color.cpp:88
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Color.cpp:138
openshot::Keyframe red
Curve representing the red value (0 - 255)
Definition: Color.h:48
openshot::Keyframe alpha
Curve representing the alpha value (0 - 255)
Definition: Color.h:51
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64