OpenShot Library | libopenshot  0.2.5
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() const {
89 
90  // Return formatted string
91  return JsonValue().toStyledString();
92 }
93 
94 // Generate Json::Value for this object
95 Json::Value Color::JsonValue() const {
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(const std::string value) {
110 
111  // Parse JSON string into JSON objects
112  try
113  {
114  const Json::Value root = openshot::stringToJson(value);
115  // Set all values that match
116  SetJsonValue(root);
117  }
118  catch (const std::exception& e)
119  {
120  // Error parsing JSON (or missing keys)
121  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
122  }
123 }
124 
125 // Load Json::Value into this object
126 void Color::SetJsonValue(const Json::Value root) {
127 
128  // Set data from Json (if key is found)
129  if (!root["red"].isNull())
130  red.SetJsonValue(root["red"]);
131  if (!root["green"].isNull())
132  green.SetJsonValue(root["green"]);
133  if (!root["blue"].isNull())
134  blue.SetJsonValue(root["blue"]);
135  if (!root["alpha"].isNull())
136  alpha.SetJsonValue(root["alpha"]);
137 }
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:33
openshot::Color::Color
Color()
Default constructor.
Definition: Color.h:54
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::Keyframe::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: KeyFrame.cpp:362
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:78
openshot::Keyframe::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: KeyFrame.cpp:329
openshot::Keyframe::AddPoint
void AddPoint(Point p)
Add a new point on the key-frame. Each point has a primary coordinate, a left handle,...
Definition: KeyFrame.cpp:125
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:64
openshot::Color::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: Color.cpp:126
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:205
openshot::Color::green
openshot::Keyframe green
Curve representing the green value (0 - 255)
Definition: Color.h:49
openshot::Color::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: Color.cpp:95
openshot::Keyframe::GetInt
int GetInt(int64_t index) const
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:286
openshot::Color::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: Color.cpp:109
a
#define a
openshot::Color::alpha
openshot::Keyframe alpha
Curve representing the alpha value (0 - 255)
Definition: Color.h:51
openshot::Color::red
openshot::Keyframe red
Curve representing the red value (0 - 255)
Definition: Color.h:48
openshot::Color::GetColorHex
std::string GetColorHex(int64_t frame_number)
Get the HEX value of a color at a specific frame.
Definition: Color.cpp:67
openshot::Color::Json
std::string Json() const
Get and Set JSON methods.
Definition: Color.cpp:88
openshot::Color::blue
openshot::Keyframe blue
Curve representing the red value (0 - 255)
Definition: Color.h:50