OpenShot Library | libopenshot  0.1.9
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2014 OpenShot Studios, LLC
9  * <http://www.openshotstudios.com/>. This file is part of
10  * OpenShot Library (libopenshot), an open-source project dedicated to
11  * delivering high quality video editing and animation solutions to the
12  * world. For more information visit <http://www.openshot.org/>.
13  *
14  * OpenShot Library (libopenshot) is free software: you can redistribute it
15  * and/or modify it under the terms of the GNU Lesser General Public License
16  * as published by the Free Software Foundation, either version 3 of the
17  * License, or (at your option) any later version.
18  *
19  * OpenShot Library (libopenshot) is distributed in the hope that it will be
20  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
26  */
27 
28 #include "../include/Color.h"
29 
30 using namespace openshot;
31 
32 // Constructor which takes R,G,B,A
33 Color::Color(unsigned char Red, unsigned char Green, unsigned char Blue, unsigned char Alpha)
34 {
35  // Set initial points
36  red.AddPoint(1, (float)Red);
37  green.AddPoint(1, (float)Green);
38  blue.AddPoint(1, (float)Blue);
39  alpha.AddPoint(1, (float)Alpha);
40 }
41 
42 // Constructor which takes 4 existing Keyframe curves
44 {
45  // Assign existing keyframes
46  red = Red;
47  green = Green;
48  blue = Blue;
49  alpha = Alpha;
50 }
51 
52 // Constructor which takes a HEX color code
53 Color::Color(string color_hex)
54 {
55  // Create a QColor from hex
56  QColor color(QString::fromStdString(color_hex));
57  red.AddPoint(1, color.red());
58  green.AddPoint(1, color.green());
59  blue.AddPoint(1, color.blue());
60  alpha.AddPoint(1, color.alpha());
61 }
62 
63 // Get the HEX value of a color at a specific frame
64 string Color::GetColorHex(int64_t frame_number) {
65 
66  int r = red.GetInt(frame_number);
67  int g = green.GetInt(frame_number);
68  int b = blue.GetInt(frame_number);
69  int a = alpha.GetInt(frame_number);
70 
71  return QColor( r,g,b,a ).name().toStdString();
72 }
73 
74 // Get the distance between 2 RGB pairs (alpha is ignored)
75 long Color::GetDistance(long R1, long G1, long B1, long R2, long G2, long B2)
76 {
77  long rmean = ( R1 + R2 ) / 2;
78  long r = R1 - R2;
79  long g = G1 - G2;
80  long b = B1 - B2;
81  return sqrt((((512+rmean)*r*r)>>8) + 4*g*g + (((767-rmean)*b*b)>>8));
82 }
83 
84 // Generate JSON string of this object
85 string Color::Json() {
86 
87  // Return formatted string
88  return JsonValue().toStyledString();
89 }
90 
91 // Generate Json::JsonValue for this object
92 Json::Value Color::JsonValue() {
93 
94  // Create root json object
95  Json::Value root;
96  root["red"] = red.JsonValue();
97  root["green"] = green.JsonValue();
98  root["blue"] = blue.JsonValue();
99  root["alpha"] = alpha.JsonValue();
100 
101  // return JsonValue
102  return root;
103 }
104 
105 // Load JSON string into this object
106 void Color::SetJson(string value) {
107 
108  // Parse JSON string into JSON objects
109  Json::Value root;
110  Json::Reader reader;
111  bool success = reader.parse( value, root );
112  if (!success)
113  // Raise exception
114  throw InvalidJSON("JSON could not be parsed (or is invalid)", "");
115 
116  try
117  {
118  // Set all values that match
119  SetJsonValue(root);
120  }
121  catch (exception e)
122  {
123  // Error parsing JSON (or missing keys)
124  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)", "");
125  }
126 }
127 
128 // Load Json::JsonValue into this object
129 void Color::SetJsonValue(Json::Value root) {
130 
131  // Set data from Json (if key is found)
132  if (!root["red"].isNull())
133  red.SetJsonValue(root["red"]);
134  if (!root["green"].isNull())
135  green.SetJsonValue(root["green"]);
136  if (!root["blue"].isNull())
137  blue.SetJsonValue(root["blue"]);
138  if (!root["alpha"].isNull())
139  alpha.SetJsonValue(root["alpha"]);
140 }
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: Color.cpp:92
Keyframe green
Curve representing the green value (0 - 255)
Definition: Color.h:46
Color()
Default constructor.
Definition: Color.h:51
Json::Value JsonValue()
Generate Json::JsonValue for this object.
Definition: KeyFrame.cpp:321
Keyframe alpha
Curve representing the alpha value (0 - 255)
Definition: Color.h:48
Keyframe red
Curve representing the red value (0 - 255)
Definition: Color.h:45
string GetColorHex(int64_t frame_number)
Get the HEX value of a color at a specific frame.
Definition: Color.cpp:64
void SetJson(string value)
Load JSON string into this object.
Definition: Color.cpp:106
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: KeyFrame.cpp:362
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:72
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:75
Keyframe blue
Curve representing the red value (0 - 255)
Definition: Color.h:47
int GetInt(int64_t index)
Get the rounded INT value at a specific index.
Definition: KeyFrame.cpp:248
This namespace is the default namespace for all code in the openshot library.
Exception for invalid JSON.
Definition: Exceptions.h:152
string Json()
Get and Set JSON methods.
Definition: Color.cpp:85
void SetJsonValue(Json::Value root)
Load Json::JsonValue into this object.
Definition: Color.cpp:129
A Keyframe is a collection of Point instances, which is used to vary a number or property over time...
Definition: KeyFrame.h:64