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